Itext tutorial >
How to Create and work with PDF files in java - iText library tutorial in java
Creating first pdf and setting attributes using itext in java - iText tutorial >
Create the first Pdf Example Using iText library in java - iText java tutorial example
Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java
Creating TABLES in pdf in java - iText tutorial >
How to Create the Table In Pdf in java - iText java tutorial
How to do the Cell Alignment In Pdf generated table in Java program
How to Set Cell Background Colour In Pdf Table in java example
How to Set Cell Background Colour In Pdf Table in java - iText java tutorial
How to Create Images In Pdf generated Table in java itext
How to Create Nested Tables in Pdf in java - iText java tutorial
How to Create the Table With Multiple Rows In Pdf in java program
Creating the Table and set the Column Width And Spacing In Pdf in java
Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example
Creating LIST in pdf in java - iText tutorial >
Create Greek List In pdf in java using iText - iText java tutorial
How to Create Roman List In pdf in java - iText java tutorial
Create ZapfDingbats List In pdf in java - iText java tutorial program
How to Create Ordered List In pdf in java using iText - iText java tutorial
Creating the UnOrdered List In pdf in java - iText java tutorial example
How to Create Lists And SubLists In Pdf in java - iText java tutorial
Creating PASSWORD PROTECTED PDF in java - iText tutorial >
How to Create Password Protected Pdf in java - iText Example
Creating IMAGES in pdf in java - iText tutorial >
Adding the Images In Pdf in java example using iText - iText java tutorial
How to Add And Rotate Images In Pdf in java - iText java tutorial example
How can you Add the Images from URL In Pdf in java program
Adding WATERMARK in pdf in java - iText tutorial >
How to add TEXT as Watermark In Pdf in java - iText java tutorial
How to add IMAGE as Watermark In Pdf in iText in java example
Creating CHAPTER, SECTION, CHUNK, PARAGRAPH AND PHRASE in pdf in java - iText tutorial >
Create Chapter And Section In Pdf in java - iText java tutorial
Create Chunk In Pdf in java - iText java tutorial program
how to Create Paragraph In Pdf - iText java tutorial program
Create Left, center and right aligned Paragraph In Pdf - iText java tutorial
Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example
Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >
How to Set the Font Name, Size, Style and Colour In Pdf using itext in java
Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >
How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial
How To Set SuperScript And SubScript In Pdf in java - iText java tutorial
UnderLine Text In Pdf in java - iText java tutorial example
Strikethrough Text In Pdf in java - iText java tutorial program
HTML to PDF in java - iText tutorial >
Convert Html To Pdf in java using iText - iText java tutorial
Create new page, set page height and width in java - iText tutorial >
How To Create New Pages In Pdf Using Itext - iText java tutorial example
How to Find Pdf Page Height And Width in java - iText java tutorial program
HEADER and FOOTER in pdf in java - iText tutorial >
How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial
BAR AND PIE CHARTS in pdf in java - iText tutorial >
How to create Bar Chart In Pdf in java - iText java tutorial
Create Pie Chart In Pdf in java using itext - iText java tutorial
MODIFY/ EDIT pdf in java - iText tutorial >
How to Modify - Add Text To Existing PDF in java - iText java tutorial example
How To Read And Add Image To Existing Pdf in java Example - iText java tutorial
Solve common Exceptions in itext in java - iText tutorial >
How to solve the Document Has No Pages IOException in iText in java
Alternates to itext in java - iText tutorial >
What are alternatives to iText library for creating Pdf in java?
--------------------------------------------------------------------------------------------
Now, let’s start writing programs >
Itext tutorial >
Creating first pdf and setting attributes using itext in java - iText tutorial >
Follow these 6 steps >
1) Create FileOutputStream - the file in which created PDF will be stored
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
2) create Document (This document will represent the PDF document)
Document class is present in com.itextpdf.text.Document.
Document document = new Document();
3) Attach PdfWriter to document,whenever any PDF element is added to document, it will get written to FileOutputStream
PdfWriter.getInstance(document, fos);
4) open document
document.open();
5) add paragraph to document
document.add(new Paragraph("Hi!, This is my first PDF using iText"));
6) close document
document.close();
Download all jars required to execute program >
Or, you may download jars from here
Program/Example to Create the first Pdf Example Using iText library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How can you Create the first Pdf Example Using iText library - core java tutorial
*
*/
public class CreateFirstPdfUsingItextExample {
public static void main(String[] args) {
try {
String pdfFilePath = "e:/Create First Pdf Using Itext Example.pdf";
//Create FileOutputStream - the file in which created PDF will be stored
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
//create Document (This document will represent the PDF document)
Document document = new Document();
//Attach PdfWriter to document,
//whenever any PDF element is added to document,
//it will get written to FileOutputStream
PdfWriter.getInstance(document, fos);
document.open(); //open document
//add paragraph to document
document.add(new Paragraph("Hi!, This is my first PDF using iText"));
document.close(); //close document
fos.close();
System.out.println("PDF created in >> "+ pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* Output of above program
PDF created in >> e:/Create First Pdf Using Itext Example.pdf
*/
|
PDF formed after executing above java program will look like this >
- Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java - Using iText library - core java tutorial in Java
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* Setting Pdf Attributes Example Using iText library - core java tutorial
*
*/
public class SettingPdfAttributesExample {
public static void main(String[] args) {
try {
String pdfFilePath = "e:/Setting Pdf Attributes Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
document.add(new Paragraph("This is SettingPdfAttributesExample.pdf"));
/**
* Now, we will set PDF file attributes
*/
//Add PDF Title and creation date
document.addTitle("SettingPdfAttributesExample");
document.addCreationDate(); //It will set current date as creation date.
//Add PDF author and creator
document.addAuthor("Ankit Mittal");
document.addCreator("JavaMadeSoEasy.com");
//PDF Subject and language
document.addSubject("How can you Set Pdf file Attributes using iText.");
document.addLanguage("");
//add PDF header
document.addHeader("1", "myPDF");
document.addKeywords("coreJava, pdf, itext");
document.close();
fos.close();
System.out.println("PDF created in >> "+ pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* Output of above program
PDF created in >> e:/Setting Pdf Attributes Example.pdf
*/
|
PDF formed after executing above java program will look like this >
Go to Document properties, then
Title, Author, Creator, subject, Keywords and CreationDate can be seen here >
Creating TABLES in pdf in java - iText tutorial >
We will create PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then create cells ( com.itextpdf.text.pdf.PdfPCell) and then add those cells to table in java in pdf in itext.
Program/Example to How to Create the Table In Pdf in java - Using iText library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to Create the Table In Pdf Example Using
* iText library - core java tutorial
*
*/
public class CreatingTableInPdfExample {
public static void main(String[] args) {
try {
String pdfFilePath = "e:/Creating Table In Pdf Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
/**
* Now, we will create table in PDF file
*/
PdfPTable table = new PdfPTable(3); // Create 3 columns in table.
// Create cells
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
// Add cells in table
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
// Add table in document
document.add(table);
document.close();
fos.close();
System.out.println("PDF created in >> " + pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* Output of above program
PDF created in >> e:/Creating Table In Pdf Example.pdf
*/
|
PDF formed after executing above java program will look like this >
How to do the Cell Alignment In Pdf generated table in Java program
How to Set Cell Background Colour In Pdf Table in java example using iText library in java
We will create PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then create cells ( com.itextpdf.text.pdf.PdfPCell) and then use setBackgroundColor to set the Cell Background Colour In Pdf generated Table in java in pdf in itext in java.
Program/Example - How to Set Cell Background Colour In Pdf generated Table in java - Using iText library - core java tutorial in Java
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to set the Cell Background Colour In Pdf generated Table Example
* Using iText library - core java tutorial
*
*/
public class CellBackgroundColourInPdfTableExample {
public static void main(String[] args) {
try {
String pdfFilePath = "e:/Cell Background Colour In Pdf Table Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
PdfPTable table = new PdfPTable(2); // Create 2 columns in table
PdfPCell cell1 = new PdfPCell(new Paragraph(
"Cell 1 - GREEN Background"));
/* Set Background colour */
cell1.setBackgroundColor(BaseColor.GREEN);
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
table.addCell(cell1);
table.addCell(cell2);
document.add(table);
document.close();
fos.close();
System.out.println("PDF created in >> " + pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* Output of above program
PDF created in >> e:/Cell Background Colour In Pdf Table Example.pdf
*/
|
PDF formed after executing above java program will look like this >
How to Set Cell Background Colour In Pdf Table in java - iText java tutorial
How to Create Images In Pdf generated Table in java itext
How to Create Nested Tables in Pdf in java - iText java tutorial - Example using iText library in core java.
We will create top level PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then create top level table cells ( com.itextpdf.text.pdf.PdfPCell) and then create another table i.e. nested table and add that in top level table cells in java in pdf in itext in java.
Program/Example - How to Create Nested Tables in Pdf in java - Example - Using iText library - core java tutorial in Java
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to Create Nested Tables in Pdf - Example
* Using iText library - core java tutorial
*
*/
public class CreateNestedTablesPdfExample {
public static void main(String[] args) {
try {
String pdfFilePath = "e:/Create Nested Tables Pdf Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
PdfPTable topLevelTable = new PdfPTable(2); // Create 2 columns in
// TOP LEVEL TABLE
PdfPCell topLevelTablecell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell topLevelTablecell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPTable nestedTable = new PdfPTable(2); // Create 2 columns in
// NESTED TABLE
PdfPCell nestedTableCell1 = new PdfPCell(new Paragraph(
"Nested table - Cell 1"));
PdfPCell nestedTableCell2 = new PdfPCell(new Paragraph(
"Nested table - Cell 2"));
nestedTable.addCell(nestedTableCell1);
nestedTable.addCell(nestedTableCell2);
topLevelTablecell2.addElement(nestedTable); // add nested table in
// top level table cell
// Now, add both cells in top level table.
topLevelTable.addCell(topLevelTablecell1);
topLevelTable.addCell(topLevelTablecell2);
document.add(topLevelTable);
document.close();
fos.close();
System.out.println("PDF created in >> " + pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* Output of above program
PDF created in >> e:/Create Nested Tables Pdf Example.pdf
*/
|
PDF formed after executing above java program will look like this >
How to Create the Table With Multiple Rows In Pdf in java program
Creating the Table and set the Column Width And Spacing In Pdf in java
Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example
Creating LIST in pdf in java - iText tutorial >
First create GreekList (com.itextpdf.text.GreekList) and then add ListItems (com.itextpdf.text.ListItem) in GreekList using add method of GreekList (greekList.add(new ListItem("Item 1"))).
Program/Example how to Create Greek List In pdf in java - Using iText library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.GreekList;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to Create Greek List In Pdf Example -
* Using iText library - core java tutorial
*
*/
public class CreateGreekListInPdfExample {
public static void main(String[] args) {
try {
String pdfFilePath = "E:/Create Greek List In Pdf Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
document.add(new Paragraph("This is GreekList"));
// Create GREEK list - in PDF using iText library in java
GreekList greekList = new GreekList();
// Add items in GREEK list - in PDF using iText library in java
greekList.add(new ListItem("Item 1"));
greekList.add(new ListItem("Item 2"));
greekList.add(new ListItem("Item 3"));
greekList.add(new ListItem("Item 4"));
greekList.add(new ListItem("Item 5"));
greekList.add(new ListItem("Item 6"));
greekList.add(new ListItem("Item 7"));
greekList.add(new ListItem("Item 8"));
greekList.add(new ListItem("Item 9"));
greekList.add(new ListItem("Item 10"));
document.add(greekList);
document.close();
fos.close();
System.out.println("PDF created in >> " + pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Create Greek List In Pdf Example.pdf
*/
|
PDF formed after executing above java program will look like this >
How to Create Roman List In pdf in java - iText java tutorial
Create ZapfDingbats List In pdf in java - iText java tutorial program
How to Create Ordered List In pdf in java using iText - iText java tutorial
First create List (com.itextpdf.text.List) i.e. orderedList using (new List(true); Or new List(List.ORDERED);) and then add ListItems (com.itextpdf.text.ListItem) in orderedList using add method of List (orderedList.add(new ListItem("Item 1"))).
Program/Example how to Create Ordered List In pdf in java - Using iText library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to Create Ordered List In Pdf program -
* Using iText library - core java tutorial
*
*/
public class CreateOrderedListInPdfExample {
public static void main(String[] args) {
try {
String pdfFilePath = "E:/Create Ordered List In Pdf Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
document.add(new Paragraph("This is OrderedList"));
//Create ORDERED list - in PDF using iText library in java
List orderedList = new List(true); // Or // new List(List.ORDERED)
//Add items in ORDERED list - in PDF using iText library in java
orderedList.add(new ListItem("Item 1"));
orderedList.add(new ListItem("Item 2"));
orderedList.add(new ListItem("Item 3"));
orderedList.add(new ListItem("Item 4"));
orderedList.add(new ListItem("Item 5"));
orderedList.add(new ListItem("Item 6"));
orderedList.add(new ListItem("Item 7"));
orderedList.add(new ListItem("Item 8"));
document.add(orderedList);
document.close();
fos.close();
System.out.println("PDF created in >> "+ pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Create Ordered List In Pdf Example.pdf
*/
|
PDF formed after executing above java program will look like this >
Creating the UnOrdered List In pdf in java - iText java tutorial example
How to Create Lists And SubLists In Pdf in java - iText java tutorial with program and examples.
First create List (com.itextpdf.text.List) i.e. using (new List(true); Or new List(List.ORDERED);) you may make it unordered list as well depending on your requirement and before adding sublist in list add ListItems which will act as heading for sublist and then create and add sublist i.e. unordered list (again you may create sublist as ordered or unordered depending on your requirement) but remember to give pass second argument of List while defining sublist like (sublist = new List(List.UNORDERED, 20)), second argument passed as 20 here is space between between bullet and ListItem added in sublist.
Program/Example How to Create List And SubList In Pdf in java - Using iText library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to Create List And SubList In Pdf - Example Using iText library - core java tutorial
*
*/
public class CreateListAndSubListInPdfExample {
public static void main(String[] args) {
try {
String pdfFilePath = "E:/Create List and SubList In Pdf Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
document.add(new Paragraph("This is List and SubList example"));
//Create List and Sublist
List list = new List(List.ORDERED); //create List (OrderedList)
List sublist = null; //create SubList reference variable
list.add(new ListItem("Item 1"));
sublist = new List(List.UNORDERED, 20); //create SubList (UnOrderedList)
//20 is space between bullet and list items
sublist.add("Item 1 - SubItem 1");
sublist.add("Item 1 - SubItem 1");
list.add(sublist);
list.add(new ListItem("Item 2"));
sublist = new List(List.UNORDERED, 20); //create SubList (UnOrderedList)
//20 is space between bullet and list items
sublist.add("Item 2 - SubItem 1");
sublist.add("Item 2 - SubItem 1");
list.add(sublist);
document.add(list);
document.close();
fos.close();
System.out.println("PDF created in >> "+ pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Create List and SubList In Pdf Example.pdf
*/
|
PDF formed after executing above java program will look like this >
Creating PASSWORD PROTECTED PDF in java - iText tutorial >
Follow these 8 steps to create Password Protected Pdf in java using iText>
1) Create FileOutputStream - the file in which created PDF will be stored
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
2) create Document (This document will represent the PDF document)
Document class is present in com.itextpdf.text.Document.
Document document = new Document();
3) Attach PdfWriter to document,whenever any PDF element is added to document, it will get written to FileOutputStream
PdfWriter.getInstance(document, fos);
4) Before opening the document, Create userPassword and ownerPassword
String userPassword = "a";
String ownerPassword = "b";
5) Now use setEncryption method of pdfWriter class to set the encryption type, get bytes from password for encryption.
pdfWriter.setEncryption(userPassword.getBytes(),
ownerPassword.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
6) Now you may open the document.
document.open();
7) add paragraph to document
document.add(new Paragraph("Hi!, This is my first PDF using iText"));
8) close document
document.close();
We will generate PDF files in java using iText library. PDF files can be generated dynamically in java using iText library.
Download all jars required to execute program >
Program/Example to How to Create Password Protected Pdf in java - iText Example - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How can I Create Password Protected Pdf Example
* Using iText library - core java tutorial
*
*/
public class CreatePasswordProtectedPdfExample {
public static void main(String[] args) {
try {
String pdfFilePath = "E:/Create Password Protected Pdf Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
String userPassword = "a";
String ownerPassword = "b";
pdfWriter.setEncryption(userPassword.getBytes(),
ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
document.open();
document.add(new Paragraph("This is Password protected PDF file"));
document.close();
fos.close();
System.out.println("PDF created in >> " + pdfFilePath);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Create Password Protected Pdf Example.pdf
*/
|
PDF formed after executing above java program will look like this >
You will have to provide password for opening the pdf file. You may use userPassword (‘a’) or ownerPassword (‘b’) to open the file.
Creating IMAGES in pdf in java - iText tutorial >
Adding the Images In Pdf (from local system or if you are working on web applications you may give path of image on server) using iText library in java.
Follow these steps to Add Images in java using iText>
- Create FileOutputStream - the file in which created PDF will be stored
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
- create Document (This document will represent the PDF document)
Document class is present in com.itextpdf.text.Document.
Document document = new Document();
- Attach PdfWriter to document,whenever any PDF element is added to document, it will get written to FileOutputStream
PdfWriter.getInstance(document, fos);
- open the document.
document.open();
- Now we will get the image from your local system or if you are working on web applications you may give path of image on server.
And create object/instance of com.itextpdf.text.Image like (Image image = Image.getInstance("E:/tiger.jpg"))
Then optionally you may set absolute position of image in PDF by using ( image.setAbsolutePosition(100f, 500f));
Then optionally you may set the image width and height using by using scaleAbsolute method OR
use the scaleAbsoluteWidth and scaleAbsoluteHeight methods to set width and height
//Scale image's width and height
image.scaleAbsolute(200f, 200f);
OR use below two methods >
//Scale image's height
image.scaleAbsoluteWidth(200f);
//Scale image's width
image.scaleAbsoluteHeight(200f);
- add image to document
document.add(image);
- close document
document.close();
Program/Example to Adding the Images In Pdf - Using iText library - core java tutorial
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* Adding the Images In Pdf File Example
* Using iText library - core java tutorial
*
*/
public class AddingImagesInPdfFileExample {
public static void main(String[] args) {
try {
String pdfFilePath = "E:/Adding Images In Pdf File Example.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
document.add(new Paragraph("This is AddingImagesInPdfFileExample.pdf"));
//Add Image from your system
Image image = Image.getInstance("E:/tiger.jpg");
//Set absolute position for image in PDF (or fixed)
image.setAbsolutePosition(100f, 500f);
/* Set width and height of Image >
* You may scaleAbsolute method
* OR use
* scaleAbsoluteWidth and scaleAbsoluteHeight methods to set width and height
*/
//Scale image's width and height
//image.scaleAbsolute(200f, 200f);
//Scale image's height
image.scaleAbsoluteWidth(200f);
//Scale image's width
image.scaleAbsoluteHeight(200f);
document.add(image); //Add image to document
document.close();
fos.close();
System.out.println("PDF created in >> "+ pdfFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Adding Images In Pdf File Example.pdf
*/
|
PDF formed after executing above java program will look like this >
How to Add And Rotate Images In Pdf in java - iText java tutorial example
How can you Add the Images from URL In Pdf in java program - using iText library - core java tutorial in Java with program and examples.
Adding WATERMARK in pdf in java - iText tutorial >
How to add TEXT as Watermark In Pdf in java - iText java tutorial
How to add IMAGE as Watermark In Pdf in iText in java example
Creating CHAPTER, SECTION, CHUNK, PARAGRAPH AND PHRASE in pdf in java - iText tutorial >
Create Chunk In Pdf in java - iText java tutorial program
how to Create Paragraph In Pdf - iText java tutorial program
Create Left, center and right aligned Paragraph In Pdf - iText java tutorial
Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example
Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >
Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >
How To Set SuperScript And SubScript In Pdf in java - iText java tutorial
UnderLine Text In Pdf in java - iText java tutorial example
Strikethrough Text In Pdf in java - iText java tutorial program
HTML to PDF in java - iText tutorial >
Create new page, set page height and width in java - iText tutorial >
How To Create New Pages In Pdf Using Itext - iText java tutorial example
How to Find Pdf Page Height And Width in java - iText java tutorial program
HEADER and FOOTER in pdf in java - iText tutorial >
BAR AND PIE CHARTS in pdf in java - iText tutorial >
Download all jars required to execute program >
Program/Example - How to create Bar Chart In Pdf in java Example Using Itext - Using iText library - core java tutorial
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import com.itextpdf.awt.DefaultFontMapper;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* How to create BarChart In Pdf in java Example Using Itext -
* iText library - core java tutorial
*
*/
public class BarChartInPdfExampleUsingItext {
public static void main(String[] args) throws Exception {
DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
defaultCategoryDataset.setValue(190, "LaptopUsers", "1995");
defaultCategoryDataset.setValue(205, "LaptopUsers", "2000");
defaultCategoryDataset.setValue(300, "LaptopUsers", "2005");
defaultCategoryDataset.setValue(350, "LaptopUsers", "2010");
defaultCategoryDataset.setValue(390, "LaptopUsers", "2015");
JFreeChart jFreeChart = ChartFactory.createBarChart(
"LaptopUsers BarChart", //title
"Year", // categoryAxisLabel
"LaptopUsers", //valueAxisLabel
defaultCategoryDataset, //dataset
PlotOrientation.VERTICAL, //orientation
false, false, false); //legend, tooltips and urls
String pdfFilePath = "E:/BAR CHART created in pdf in java using iText.pdf";
OutputStream fos = new FileOutputStream(new File(pdfFilePath));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
PdfContentByte pdfContentByte = writer.getDirectContent();
int width = 400; //width of BarChart
int height = 300; //height of BarChart
PdfTemplate pdfTemplate = pdfContentByte.createTemplate(width, height);
//create graphics
Graphics2D graphics2d = pdfTemplate.createGraphics(width, height,
new DefaultFontMapper());
//create rectangle
java.awt.geom.Rectangle2D rectangle2d = new java.awt.geom.Rectangle2D.Double(
0, 0, width, height);
jFreeChart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
pdfContentByte.addTemplate(pdfTemplate, 40, 500); //0, 0 will draw BAR chart on bottom left of page
document.close();
System.out.println("PDF created in >> " + pdfFilePath);
}
}
/*OUTPUT
PDF created in >> E:/BAR CHART created in pdf in java using iText.pdf
*/
|
PDF formed after executing above java program will look like this (Bar chart is created in pdf in java using itext) >
MODIFY/ EDIT pdf in java - iText tutorial >
How to Modify - Add Text To Existing PDF in java - iText java tutorial example
How To Read And Add Image To Existing Pdf in java Example - iText java tutorial
Solve common Exceptions in itext in java - iText tutorial >
Alternates to itext in java - iText tutorial >
Summary -
So in this core java tutorial we learned how to create pdf dynamically in java using itext library.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
References >
RELATED LINKS>