Expected Output from the AuditTest.java Code- PDF Document Creation Using Java

As shown in the preceding code, the generated PDF can be set with a metadata Header. This is a standard PDF format which can be used to set attributes such as the pdf author name, a title, a file description, etc. This is a useful additional feature for use with an Auditing system, where traceability is important (especially since the Audit system itself is audited by the Auditing standards body!).

Adding an Image to a PDF Document

An empty PDF Document is created using the Document class. A PdfDocument object is then passed as an argument to the Document class constructor. To add an image to the PDF, we can create an object of the image that is required to be added and add this image using the add() method of the Document class.

The following are the steps to add a TIFF image to the PDF document.

NoteI am using iText version 2.1.7 for this chapter; there is a new iText version 7 available which has more features (support for jpeg images, for example), but the 2.1.7 version is supported by an Apache 2.0 license (embedded in the com.lowagie.text-2.1.7.jar file) and is fine for the application we are using, and the whole system is held in this one .jar file.

Later versions are split into multiple .jar files, and some of the Java classes are coded differently.

Creating a PdfWriter Object

The PdfWriter class is a Java DocWriter class for a PDF. This class is found in the Java package com.lowagie.text.pdf. The constructor of this class accepts a string containing the path of the file where the PDF is to be created.

The PdfWriter class is created by first passing a string value file path (defining the folder path of the PDF file) as shown in Listing 6-2.

// Create a PdfWriter

String sTargetPDFFile = “/opt/AuditReport/AuditTest/AuditImage.pdf”;

File pdfFile = new File(sTargetPDFFile);

com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A3.rotate(), 50, 50, 100, 100);

Listing 6-2Code snippet for creating the iText Document Class

When an object of this type is passed to an iText Document Java class, every element added to the document object will be written to the file we defined earlier.

Creating an iText Document Object Class

The Document Java class object holds an image of the PDF Document used in iText. This Java class is defined in the package com.lowagie.text.Document. To instantiate this class, a PdfWriter class object is passed to the Document Java class. This relationship can be demonstrated using the Java code in Listing 6-3.

PdfWriter AuditPDFwriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));

// Create a PdfDocument

document.open();

Listing 6-3Code to create an iText PdfWriter class

After a PDF Document class object is created, you can add the elements like page, font, file attachment, and event handler using iText methods provided by the class as shown in Listing 6-4 for adding sections of Auditor questions.

//AUDITOR START OUTPUT TO PDF

for(int iSection=0 ; iSection < noAUDITORSections ; iSection++) {

// We add one empty line

addEmptyLine(sections, 1);

// Lets write a big header

//TODO Pick all text up from the config.xml file

Paragraph paragraph = new Paragraph(TabSECTIONS_AUDITOR[iSection], catFont);

paragraph.setAlignment(Element.ALIGN_CENTER);

sections.add(paragraph);

addEmptyLine(sections, 1);

for(int iProp=0 ; iProp < noAUDITORprops[iSection] ; iProp++) {

paragraph = new Paragraph(AUDITOR_propNames[iSection][iProp] + ” : ” + AUDITOR_propValues[iSection][iProp], catFont);

paragraph.setAlignment(Element.ALIGN_CENTER);

sections.add(paragraph);

addEmptyLine(sections, 1);

}

}

document.add(sections);

// Start a new page

document.newPage();

Listing 6-4Code to add paragraphs, sections, and New pages to a pdf document

Creating the Document Object

The Document class defined in the Java package com.lowagie.text.Document is the root element of the PDF image object.

You can instantiate the Document class by passing an object of the class PdfWriter (in package com.lowagie.text.pdf) created in the previous steps, as shown in Listing 6-5, which can be set to a specific PDF version (we selected version 1.3).

// Creating an Audit Report Document as class Document

AuditPDFwriter.setPdfVersion(PdfWriter.VERSION_1_3);

Listing 6-5Code to set the version of PDF generated

Leave a Reply

Your email address will not be published. Required fields are marked *