How to create PDF with Java and iText Example
iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. I will show you a example here.
iText has a hierarchical structure. The smallest text unit is a "Chunk" which is a String with a pre-defined font. A "Phrase" combines several Chunks and allows to define line spacing. "Paragraph" is a subclass of "Phrase" and allows to define more layout attributes, e.g. margins. The class "Anchor" is a subclass of "Paragraph" and serves as the basis for hyperlinks in the generated PDF.
for How to create PDF with Java and iText Example
Installation
Download the iText core binary from here link. Download iText Jar
Creating PDF
package itext.test;
import java.io.FileOutputStream;
import java.util.Date;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class GeneratePDFTest {
public static void main(String[] args) {
try {
//create a document
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
//adding metadata about pdf file
addMetaData(document);
//adding title of pdf file
addTitlePage(document);
//adding contents into pdf file.
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static String FILE = "D:/test/TestPdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);
//iText allows to add metadata to the PDF which can be viewed in your Adobe Reader
// You can view under File -> Properties
private static void addMetaData(Document document) {
document.addTitle("My Test PDF");
document.addSubject("Using iText");
document.addKeywords("Java, PDF, iText");
document.addAuthor("Wakil Ahmad");
document.addCreator("Wakil");
}
private static void addTitlePage(Document document) throws DocumentException {
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Writing a big header
preface.add(new Paragraph("Title of the document as pdf file", catFont));
addEmptyLine(preface, 1);
// Will create: Report generated by: _name, _date
preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(),smallBold));
addEmptyLine(preface, 3);
preface.add(new Paragraph("Describes about document ",smallBold));
addEmptyLine(preface, 8);
preface.add(new Paragraph("This document is a preliminary version", redFont));
document.add(preface);
// Start a new page
document.newPage();
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
private static void addContent(Document document) throws DocumentException {
Anchor anchor = new Anchor("Testing page 1", catFont);
anchor.setName("Test Chapter 1");
// Second parameter is the number of the chapter
Chapter catPart = new Chapter(new Paragraph(anchor), 1);
Paragraph subPara = new Paragraph("Subcategory 1", subFont);
Section subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("How are you"));
subPara = new Paragraph("Subcategory 2", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("Paragraph 1"));
subCatPart.add(new Paragraph("Paragraph 2"));
subCatPart.add(new Paragraph("Paragraph 3"));
subCatPart.add(new Paragraph("Paragraph 4"));
// Add a list
createList(subCatPart);
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 5);
subCatPart.add(paragraph);
// Adding a table
createTable(subCatPart);
// Now add all this to the document
document.add(catPart);
// Next section
anchor = new Anchor("Second Chapter", catFont);
anchor.setName("Second Chapter");
// Second parameter is the number of the chapter
catPart = new Chapter(new Paragraph(anchor), 1);
subPara = new Paragraph("Subcategory", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("This is a very important message"));
// Now add all this to the document
document.add(catPart);
}
private static void createList(Section subCatPart) {
List list = new List(true, false, 10);
list.add(new ListItem("First point"));
list.add(new ListItem("Second point"));
list.add(new ListItem("Third point"));
subCatPart.add(list);
}
private static void createTable(Section subCatPart) throws BadElementException {
PdfPTable table = new PdfPTable(3);
PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 2"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 3"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell("1.0");
table.addCell("1.1");
table.addCell("1.2");
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.3");
subCatPart.add(table);
}
}
After Running the above java program, the output file below here.
iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. I will show you a example here.
iText has a hierarchical structure. The smallest text unit is a "Chunk" which is a String with a pre-defined font. A "Phrase" combines several Chunks and allows to define line spacing. "Paragraph" is a subclass of "Phrase" and allows to define more layout attributes, e.g. margins. The class "Anchor" is a subclass of "Paragraph" and serves as the basis for hyperlinks in the generated PDF.
for How to create PDF with Java and iText Example
Installation
Download the iText core binary from here link. Download iText Jar
Creating PDF
package itext.test;
import java.io.FileOutputStream;
import java.util.Date;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class GeneratePDFTest {
public static void main(String[] args) {
try {
//create a document
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
//adding metadata about pdf file
addMetaData(document);
//adding title of pdf file
addTitlePage(document);
//adding contents into pdf file.
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static String FILE = "D:/test/TestPdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);
//iText allows to add metadata to the PDF which can be viewed in your Adobe Reader
// You can view under File -> Properties
private static void addMetaData(Document document) {
document.addTitle("My Test PDF");
document.addSubject("Using iText");
document.addKeywords("Java, PDF, iText");
document.addAuthor("Wakil Ahmad");
document.addCreator("Wakil");
}
private static void addTitlePage(Document document) throws DocumentException {
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Writing a big header
preface.add(new Paragraph("Title of the document as pdf file", catFont));
addEmptyLine(preface, 1);
// Will create: Report generated by: _name, _date
preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(),smallBold));
addEmptyLine(preface, 3);
preface.add(new Paragraph("Describes about document ",smallBold));
addEmptyLine(preface, 8);
preface.add(new Paragraph("This document is a preliminary version", redFont));
document.add(preface);
// Start a new page
document.newPage();
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
private static void addContent(Document document) throws DocumentException {
Anchor anchor = new Anchor("Testing page 1", catFont);
anchor.setName("Test Chapter 1");
// Second parameter is the number of the chapter
Chapter catPart = new Chapter(new Paragraph(anchor), 1);
Paragraph subPara = new Paragraph("Subcategory 1", subFont);
Section subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("How are you"));
subPara = new Paragraph("Subcategory 2", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("Paragraph 1"));
subCatPart.add(new Paragraph("Paragraph 2"));
subCatPart.add(new Paragraph("Paragraph 3"));
subCatPart.add(new Paragraph("Paragraph 4"));
// Add a list
createList(subCatPart);
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 5);
subCatPart.add(paragraph);
// Adding a table
createTable(subCatPart);
// Now add all this to the document
document.add(catPart);
// Next section
anchor = new Anchor("Second Chapter", catFont);
anchor.setName("Second Chapter");
// Second parameter is the number of the chapter
catPart = new Chapter(new Paragraph(anchor), 1);
subPara = new Paragraph("Subcategory", subFont);
subCatPart = catPart.addSection(subPara);
subCatPart.add(new Paragraph("This is a very important message"));
// Now add all this to the document
document.add(catPart);
}
private static void createList(Section subCatPart) {
List list = new List(true, false, 10);
list.add(new ListItem("First point"));
list.add(new ListItem("Second point"));
list.add(new ListItem("Third point"));
subCatPart.add(list);
}
private static void createTable(Section subCatPart) throws BadElementException {
PdfPTable table = new PdfPTable(3);
PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 2"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 3"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell("1.0");
table.addCell("1.1");
table.addCell("1.2");
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.3");
subCatPart.add(table);
}
}
After Running the above java program, the output file below here.
Read an existing PDF :
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
public class ReadUsePDFTest {
private static String INPUTFILE = "D:/test/testPdf.pdf";
private static String OUTPUTFILE = "D:/test/ReadPdf.pdf";
public static void main(String[] args) throws DocumentException,
IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(OUTPUTFILE));
document.open();
PdfReader reader = new PdfReader(INPUTFILE);
int n = reader.getNumberOfPages();
PdfImportedPage page;
// Go through all pages
for (int i = 1; i <= n; i++) {
// Only page number 2 will be included
if (i == 2) {
page = writer.getImportedPage(reader, i);
Image instance = Image.getInstance(page);
document.add(instance);
}
}
document.close();
}
}



No comments:
Post a Comment