To create the TiffImage class object, we can use the package com.lowagie.text.pdf.codec and then use the iText RandomAccessFileOrArray class object class in the constructor method. As an argument of the RandomAccessFileOrArray constructor method, we pass a string argument defining the path of the image, as shown in Listing 6-6.
// Creating an Image object
String imageLogoFile = “/opt/AuditReport/images/AuditMasterLogo.tif”;
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(imageLogoFile);
int pages = TiffImage.getNumberOfPages(ra);
Listing 6-6Code to create an iText Image object RandomAccessFileOrArray
Now we can instantiate the Image class of the iText library package, com.lowagie.text.Image. In the constructor, we can pass the preceding RandomAccessFileOrArray class object as an argument, as shown in Listing 6-7.
// Creating an Image object
Image image;
image = TiffImage.getTiffImage(ra, 1);
Listing 6-7Code to create the iText Image class object
Adding Images to the Document
We can now add the image object created in the previous step using the add() method of the Document class, as shown in Listing 6-8.
// Adding the Audit Master Logo image to the Audit report document
Rectangle pageSize = new Rectangle(image.getWidth(),
image.getHeight());
document.setPageSize(pageSize);
document.add(image);
Listing 6-8Code to set the page size and add the iText Image object to a document object
Closing the Document
Finally, we must close the document using the close() method of the Document class, as shown in Listing 6-9.
// Save to the Linux folder path /opt/AuditMaster/AuditTest
// Closing the Audit report document
document.close();
System.out.println(“Audit Master Logo Image added”);
}
}
Listing 6-9Code to close the pdf Document object
Example 2 – Test Code to Add the Audit Master Logo
The following Test Java code was used to add the Audit Master Logo TIFF image to a PDF document using the iText library. It creates a PDF Audit Report document with the name AuditImage.pdf, adds the Audit Master TIFF image to it, and saves it in the Linux folder path /opt/AuditReport/AuditTest/.
We have saved this code in a file with name AuditMasterLogoTest.java.
package com.ibm.filenet.ps.ciops.test;
import java.io.File;
import java.io.FileOutputStream;
// iText jar Library imports
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfDocument;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;
public class AuditMasterLogoTest {
public static void main(String args[]) throws Exception {
// 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);
PdfWriter AuditPDFwriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
// Create a PdfDocument
document.open();
// Creating an Audit Report Document as class Document
AuditPDFwriter.setPdfVersion(PdfWriter.VERSION_1_3);
// Creating an ImageData object
String imageLogoFile = “/opt/AuditReport/images/AuditMasterLogo.tif”;
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(imageLogoFile);
int pages = TiffImage.getNumberOfPages(ra);
// Creating an Image object
image = TiffImage.getTiffImage(ra, 1);
// Adding the Audit Master Logo image to the Audit report document
Rectangle pageSize = new Rectangle(image.getWidth(),
image.getHeight());
document.setPageSize(pageSize);
document.add(image);
// Save to the Linux folder path /opt/AuditMaster/AuditTest
// Closing the Audit report document
document.close();
System.out.println(“Audit Master Logo Image added”);
}
}
Listing 6-10Complete example Java code to add a TIFF image to a pdf Document
Part 2 – Example 3 – An Audit Report from the Audit Master
A complete program to provide a pdf Audit report from the Audit questions from the Audit Master using Cases from the solution created in Chapter 1 is as follows.
We added Audit questions as follows:
Check management responsibility
Leave a Reply