iText Introduction
- iText - Adding a List
- iText - Adding a Paragraph
- iText - Adding an AreaBreak
- iText - Creating a PDF Document
- iText - Overview
iText Tables
- iText - Adding Lists to a Table
- iText - Nested Table
- iText - Adding Image to a Table
- Formatting the Borders of a Cell
- iText - Formatting Cell Contents
- iText - Adding a Table
iText Images
- iText - Rotating an Image
- iText - Scaling an Image
- iText - Setting Position of the Image
- iText - Adding Image to a PDF
iText Annotations
- iText - Circle Annotation
- iText - Markup Annotation
- iText - Line Annotation
- iText - Link Annotation
- iText - Text Annotation
iText Canvas
iText Miscellaneous
iText Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
iText - Adding an AreaBreak
In this chapter, we will see how to create a PDF document with AreaBreak using the iText pbrary.
Creating an AreaBreak
You can create an empty PDF Document by instantiating the Document class. While instantiating this class, you need to pass a PdfDocument object as a parameter, to its constructor. Then, to add an areabreak to the document, you need to instantiate the AreaBreak class and add this object to document using the add() method.
Following are the steps to create an empty PDF document with AreaBreak.
Step 1: Creating a PdfWriter object
The PdfWriter class represents the Doc Writer for a PDF, this class belongs to the package com.itextpdf.kernel.pdf. The constructor of this class accepts a string, representing the path of the file where the PDF is to be created.
Instantiate PdfWriter class by passing a string value representing the path where you need to create a PDF, to its constructor, as shown below.
// Creating a PdfWriter String dest = "C:/itextExamples/addingAreaBreak.pdf"; PdfWriter writer = new PdfWriter(dest);
When an object of this type is passed to a PdfDocument (class), then every element added to this document will be written to the file specified.
Step 2: Creating a PdfDocument object
The PdfDocument class is the class that represents the PDF Document in iText, this class belongs to the package com.itextpdf.kernel.pdf. To instantiate this class (in writing mode) you need to pass an object of the class PdfWriter to its constructor.
Instantiate the PdfDocument class by passing above created PdfWriter object to its constructor, as shown below.
// Creating a PdfDocument PdfDocument pdfDoc = new PdfDocument(writer);
Once a PdfDocument object is created you can add various elements pke page, font, file attachment, event handler using the respective methods provided by its class.
Step 3: Creating a Document object
The Document class of the package com.itextpdf.layout is the root element while creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument.
Instantiate the Document class by passing the object of the class PdfDocument created in the previous steps, as shown below.
// Creating a Document Document document = new Document(pdfDoc);
Step 4: Creating an Area Break object
The AreaBreak class belongs to the package com.itextpdf.layout.element. On instantiating this class, the current context area will be terminated and a new one will be created with the same size (in case we use default constructor).
Instantiate the AreaBreak class as shown below.
// Creating an Area Break AreaBreak aB = new AreaBreak();
Step 5: Adding AreaBreak
Add the areabreak object created in the previous step using the add() method of the Document class, as shown below.
// Adding area break to the PDF document.add(aB);
Step 6: Closing the Document
Close the document using the close() method of the Document class as shown below.
// Closing the document document.close();
Example
The following Java program demonstrates how to create a PDF document with AreaBreak using the iText pbrary. It creates a PDF document with the name addingAreaBreak.pdf, adds an areabreak to it, and saves it in the path C:/itextExamples/.
Save this code in a file with the name AddingAreaBreak.java.
import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.AreaBreak; pubpc class AddingAreaBreak { pubpc static void main(String args[]) throws Exception { // Creating a PdfWriter String dest = "C:/itextExamples/addingAreaBreak.pdf"; PdfWriter writer = new PdfWriter(dest); // Creating a PdfDocument PdfDocument pdf = new PdfDocument(writer); // Creating a Document by passing PdfDocument object to its constructor Document document = new Document(pdf); // Creating an Area Break AreaBreak aB = new AreaBreak(); // Adding area break to the PDF document.add(aB); // Closing the document document.close(); System.out.println("Pdf created"); } }
Compile and execute the saved Java file from the Command prompt using the following commands −
javac AddingAreaBreak.java java AddingAreaBreak
Upon execution, the above program creates a PDF document, displaying the following message.
Pdf Created
If you verify the specified path, you can find the created PDF document, as shown below.
Advertisements