Thanks for the reply!
So, taking my cfscript into account above, which JAVA object has the methods that i need to:
1) add a table to a pdf (1 col 2 row with each cell set to center)
2) insert an image into the top cell
3) insert text into the bottom cell
Here are the java objects defined in my code:
pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(fullPathToInputFile);
outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(pdfReader, outStream);
img = createObject("java", "com.lowagie.text.Image").getInstance(fullPathToWatermark);
Can i use one of the objects above or do i need to instantiate a different object? This is where i'm confused...if i knew which lib to research i'm sure i could figure it out...
Thanks...
The classes you need are PdfTable and PdfCell
http://cfsearching.blogspot.com/2008/09/getting-started-with-itext-tables.html
I was curious, so I threw together a rough example. It adds the table to a pdfTemplate, then adds the watermark template to each page. The positioning and table dimensions are based on my sample data, so you will need to tweak them. Just make sure the template dimensions are large enough to accomodate your image and text. If the dimensions are not large enough, the watermark may not be visible.
<!--- NOTES: 1. This code uses deprecated methods for compatibility with the iText version in CF8 2. The table dimensions and watermark positions used are for demo purposes only and should be adjusted.
Test Files: PDF: http://itextdocs.lowagie.com/examples/com/lowagie/examples/general/copystamp/ChapterSection.pdf IMAGE: http://code.google.com/apis/themes/images/beveled_purpleblue.png --->
<cfscript> err = ""; // TEST VALUES maxlinechars = 40; dCompany = "Google Inc"; dName = "1600 Amphitheatre Parkway"; dPhone = "+1 650-253-0000"; dEmail = "someone@google.com"; // simplify code by putting the watermark text values into an array textElements = [ dCompany, dName, dPhone, dEmail ]; inputFile = ExpandPath("ChapterSection.pdf"); outputFile = ExpandPath("ChapterSection_Watermark_ImageWithText.pdf"); imgPath = ExpandPath("/dev/beveled_purpleblue.png");
try { // initalize objects for reading and writing the pdf pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(inputFile); outStream = createObject("java", "java.io.FileOutputStream").init(outputFile); pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init( pdfReader, outStream );
// get the dimension of the watermark image // note: the table width and height are sample values for demo only .. // must ensure your dimensions are large enough to accomdate your text and image // otherwise the watermark may not be fully visible img = createObject("java", "com.lowagie.text.Image").getInstance(imgPath); tableWidth = img.width() + 100; tableHeight = img.height() + 75; // create a template for storing the table cb = pdfStamper.getOverContent(1); template = cb.createTemplate(tableWidth, tableHeight);
// create a single column table table = createObject("java", "com.lowagie.text.pdf.PdfPTable").init( 1 ); table.setTotalWidth( tableWidth );
// reusable objects for adding table rows PdfCell = createObject("java", "com.lowagie.text.pdf.PdfPCell"); Phrase = createObject("java", "com.lowagie.text.Phrase");
// add the watermark image to the first row imageCell = PdfCell.init( img, false ); imageCell.setBorder( PdfCell.NO_BORDER); imageCell.setHorizontalAlignment( PdfCell.ALIGN_CENTER ); table.addCell( imageCell );
// add each text element in the array to a new row for (x = 1; x <= arrayLen(textElements); x++) { textCell = PdfCell.init( Phrase.init( textElements ) ); textCell.setBorder( PdfCell.NO_BORDER ); textCell.setHorizontalAlignment( PdfCell.ALIGN_CENTER ); table.addCell( textCell ); } // get the calculated table height table.calculateHeightsFast(); tableHeight = table.getTotalHeight(); table.writeSelectedRows(0, -1, 0, tableHeight, template); WriteOutput("Calculated tableWidth="& tableWidth &", tableHeight="& tableHeight &"<br>");
// add the template watermark to each page // note: the x/yPos values are for demo only (top right) ... i = 0; totalPages = pdfReader.getNumberOfPages(); while (i LT totalPages) { i = i + 1; content = pdfStamper.getOverContent( javacast("int", i) ); // arbitrary positioning code pageSize = pdfReader.getPageSize(i); yPos = pageSize.height() - tableHeight - 15; xPos = (pageSize.width() - tableWidth) - 25; content.addTemplate( template, xPos, yPos ); WriteOutput("Watermarked page "& i &" at xPos="& xPos &",yPos="& yPos &"<br>"); } } catch (Exception e) { err = e; } if (IsDefined("pdfStamper")) { pdfStamper.close(); }
if (IsDefined("outStream")) { outStream.close(); } </cfscript>
<cfdump var="#err#" label="ERROR"> |