Create PDF documents like a pro – part 3

Create PDF documents like a pro – part 3

December 18, 2014

Up to part 3. In my series of  blog posts I explore how to manipulate PDF files through Java. In Part 1 I described how to create dynamic documents and documents from template. In Part 2 we discussed how to add footer and content to template. Its  time to explore how to create checkboxes and copy pages. We will again use  iText library to create PDF documents, because its opensource and can be downloaded here.


Creating checkboxes

I will show you how to accomplish the following scenario where you want to produce static PDF output that has some check boxes in it. We dont need them to be fillable as a template. So here the trick is to use special font called wingding which supports some special symbols. You must download it and add it to your project. In my example its added on root level. Take a look at the following code and read its comments:

public class CheckBoxExample {
public static void main(String[] args) throws DocumentException,
IOException {

//This is the code of the character that represent a checked checkbox and is supported in wingding

int check=0xFE;

//This is the code of the character that represent an unchecked checkbox and is supported in wingding

int uncheck=0x6F;

//Here we create instance of BaseFont object loaded with our custom font wingding

BaseFont bf = BaseFont.createFont("wingding.ttf", BaseFont.IDENTITY_H,
Font checkBoxFont = new Font(bf,14);
Document document = new Document();
document.setMargins(72f, 72f, 36f, 45f);
FileOutputStream fos = new FileOutputStream(new File("CheckBoxExample.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, fos);;

//It is as simple as just outputting the desired character

document.add(new Paragraph(""+(char)check,checkBoxFont));
document.add(new Paragraph(""+(char)uncheck,checkBoxFont));

Here you cam see the example.

Copying pages

Now let`s see how to accomplish the following scenario where you want to unite multiple PDF files in one. Also, we will add page numbering at the bottom of every page. For the purposes of the example we will use previously generated check box PDF and static template PDF. Look the following code as an example:

public class ConcatPdfExample {

//This takes as arguments list of file paths and a boolean which indicates whether we want to add page //numbering

public static void concatPDFs(List pathOfPDFFiles, boolean paginate) throws
FileNotFoundException {
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(new
try {
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
List readers = new ArrayList();
int totalPages = 0;
for(String path : pathOfPDFFiles){

//We create input streams from the file paths

 InputStream inputStream = new FileInputStream(new File(path));

//If we are able to open the streams we create separate reader for each PDf file and add it in our list

if(inputStream != null){
PdfReader pdfReader = new PdfReader(inputStream);
totalPages += pdfReader.getNumberOfPages();
PdfCopy copy = new PdfCopy(document, outputStream);;
PdfImportedPage page;
PdfCopy.PageStamp stamp;
int currentPageNumber = 0;
int n;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();

// Loop through the PDF readers and add their content to the output.

while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader =;
n = pdfReader.getNumberOfPages();
for (int pageNumber = 0; pageNumber < n; ) {
page = copy.getImportedPage(pdfReader, ++pageNumber);

// Code for pagination.

if (paginate) {
stamp = copy.createPageStamp(page);

// Add page numbers

Phrase phrase = new Phrase(String.format(“page %d of %d”,
currentPageNumber, totalPages));
         phrase.setFont(new Font(bf,9));
stamp.getUnderContent(), Element.ALIGN_RIGHT,phrase,
page.getBoundingBox().getRight()-70, 26, 0);
} catch (Exception e) {
} finally {
if (document.isOpen())
try {
if (outputStream != null)
} catch (IOException ioe) {
public static void main(String[] args) throws FileNotFoundException {
List<String> filePaths = new LinkedList<String>();
     concatPDFs(filePaths, true);

Here is the example.


Any questions/comments regarding the article ?
Leave your comments below.


Denis Danov

Java EE and Oracle Developer at Dreamix

More Posts - Website

Follow Me:
TwitterLinkedInGoogle Plus

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!