Over a million developers have joined DZone.

Simple PDF Creation With IText

DZone's Guide to

Simple PDF Creation With IText

Free Resource
Simple class which illustrates usage of iText to create a PDF file.

package testrecorder;

import java.io.FileOutputStream;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfWriter;

 * @author ditch182
class createPDF {
	private static Font catFont = new Font(Font.FontFamily.HELVETICA, 18);
	private static Font redFont = new Font(Font.FontFamily.HELVETICA, 12);
	private static Font subFont = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD);
	private static Font smallBold = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
        private Object thistest;
        public createPDF(Test test) {
            try {

                        Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream(test.getControlNo().toString() + ".pdf"));
			addMetaData(document, test);
			addContent(document, test);
		} catch (Exception e) {

    private void addMetaData(Document document, Test thisTest) {
		document.addSubject("Using iText");
		document.addKeywords("Java, PDF, iText");
		document.addAuthor("Richard Hibbitts");
		document.addCreator("Automated Test Recorder");

    private void addContent(Document document, Test thisTest) throws DocumentException {
        Anchor anchor = new Anchor("First Chapter", catFont);
		anchor.setName("First Chapter");

		// 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("Hello"));

		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"));

		// Add a  little list

		// Add a small table
		// Now a small table

		// Now add all this to the document

		// 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


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}