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. It`s time to explore how to create checkboxes and copy pages. We will again use iText library to create PDF documents, because it`s 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 don`t 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 it`s added on root level. Take a look at the following code and read its comments:
[java]
public class CheckBoxExample {
public static void main(String[] args) throws DocumentException,
IOException {
[/java]
//This is the code of the character that represent a checked checkbox and is supported in wingding
[java]
int check=0xFE;
[/java]
//This is the code of the character that represent an unchecked checkbox and is supported in wingding
[java]
int uncheck=0x6F;
[/java]
//Here we create instance of BaseFont object loaded with our custom font wingding
[java]
BaseFont bf = BaseFont.createFont("wingding.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
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);
document.open();
[/java]
//It is as simple as just outputting the desired character
[java]
document.add(new Paragraph(""+(char)check,checkBoxFont));
document.add(new Paragraph(""+(char)uncheck,checkBoxFont));
document.close();
fos.close();
}
}
[/java]
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:
[java]
public class ConcatPdfExample {
[/java]
//This takes as arguments list of file paths and a boolean which indicates whether we want to add page //numbering
[java]
public static void concatPDFs(List pathOfPDFFiles, boolean paginate) throws
FileNotFoundException {
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(new
File("ConcatPdfExample.pdf"));
try {
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
List readers = new ArrayList();
int totalPages = 0;
for(String path : pathOfPDFFiles){
[/java]
//We create input streams from the file paths
[java]
InputStream inputStream = new FileInputStream(new File(path));
[/java]
//If we are able to open the streams we create separate reader for each PDf file and add it in our list
[java]
if(inputStream != null){
PdfReader pdfReader = new PdfReader(inputStream);
inputStream.close();
readers.add(pdfReader);
totalPages += pdfReader.getNumberOfPages();
}
}
PdfCopy copy = new PdfCopy(document, outputStream);
document.open();
PdfImportedPage page;
PdfCopy.PageStamp stamp;
int currentPageNumber = 0;
int n;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();
[/java]
// Loop through the PDF readers and add their content to the output.
[java]
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
n = pdfReader.getNumberOfPages();
for (int pageNumber = 0; pageNumber < n; ) {
currentPageNumber++;
page = copy.getImportedPage(pdfReader, ++pageNumber);
[/java]
// Code for pagination.
[java]
if (paginate) {
stamp = copy.createPageStamp(page);
[/java]
// Add page numbers
[java]
Phrase phrase = new Phrase(String.format(“page %d of %d”,
currentPageNumber, totalPages));
phrase.setFont(new Font(bf,9));
ColumnText.showTextAligned(
stamp.getUnderContent(), Element.ALIGN_RIGHT,phrase,
page.getBoundingBox().getRight()-70, 26, 0);
stamp.alterContents();
}
copy.addPage(page);
}
copy.freeReader(pdfReader);
pdfReader.close();
}
outputStream.flush();
document.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document.isOpen())
document.close();
try {
if (outputStream != null)
outputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static void main(String[] args) throws FileNotFoundException {
List<String> filePaths = new LinkedList<String>();
filePaths.add(“PageTemplateExample.pdf”);
filePaths.add(“CheckBoxExample.pdf”);
concatPDFs(filePaths, true);
}
}
[/java]
Here is the example.
Any questions/comments regarding the article ?
Leave your comments below.