Java Snippets code for generating PDF using iText
Introduction:
This article specifies a simple Java code snippet for generating PDF using iText jar. we are all familiar with iText is an open source library. It is used to creating and manipulating PDF,RTF and HTML files with the use of Java code. iText also support .NET Framework under the name of iTextSharp.
Download the latest iText jar here.
The following snippets of code used to create the pdf document here specify the size and name of the document.
/**
* Create the document and given the name of the document and specify the
* document size like A0 to A9
*/
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\shunmuga\\Desktop" + File.separator + "SamplePicture.pdf"));
The follwing code is used to construct the header and footer of the document.
/**
* Add the header name for the document using the following code
*
*/
HeaderFooter header = new HeaderFooter(new Paragraph("Appache Tomcat"), false);
header.setBorder(Rectangle.NO_BORDER);
header.setAlignment(Element.ALIGN_CENTER);
document.setHeader(header);
/***
* Add the footer name for the document using the following code
*/
HeaderFooter footer = new HeaderFooter(new Phrase("Page No"), false);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_CENTER);
document.setFooter(footer);
If you want add image in the pdf document using the following code.
/**
* Add the image in the pdf document using the following code
*/
File inputFile = new File("tomcat.JPG");
Image img = Image.getInstance(inputFile.getName());
img.setAlignment(Element.ANNOTATION);
img.setIndentationLeft(200);
If you want add paragraph means using the following code.
/**
*Add a paragrah to the pdf document
*/
String address = " Good year Court, \n Subangjaya, \n Malaysia";
Paragraph paragraph = new Paragraph(address);
If you want construct table in pdf using the following code.
/**
* The following code explains the construct table in a pdf
*/
PdfPTable datatable = new PdfPTable(5);
PdfPCell cell = new PdfPCell(new Paragraph("No"));
datatable.addCell(cell);
datatable.addCell("Wonders");
datatable.addCell("Country");
datatable.addCell("Data Of Construction");
datatable.addCell("Image");
//Here Specify the column width of the table
int headerwidths[] =
{
5, 12, 12, 12, 12
};
If you want add chunk or phrase in pdf document using the following code.
/**
*The following code is used to add the chunk between the lines of the
* document
*/
Chunk chunk = new Chunk();
chunk.setNewPage();
chunk.setBackground(Color.BLUE);
/**
* Add a phrase to the pdf document
*/
String address = "\n Good year Court, \n Subangjaya, \n Malaysia";
Phrase phrase = new Phrase(address);
The following code is used to set the desired font style for header or foooter any paragraph.
/**
* Here specify the font
*/
Font headerFont = FontFactory.getFont(FontFactory.TIMES_ROMAN, 14,
new Color(0, 0, 0));
The following code is used to create hyperlink in pdf document.
/**
*Here creating the hyperlink and specify the font
*/
Anchor anchor = new Anchor("www.shunmuga.com",
FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.UNDERLINE, new Color(0, 0, 255)));
Examples:
Have a look at this following examples to construct pdf
1.Java Code to Convert PDF Using Itext jar
2.Java Code to construct a table in PDF using iText
If you find this article is useful to you, dont forget to give your valuable comments. Have a joyous day.