Thanks a lot for the help. I was thinking there might have been a way to do that all at once while writing to a single file, but I guess not. The process you described worked - I took the PdfContentByte directly from the PdfStamper while reading in the temporary file, so I didn't have to use templates at all.
Thanks, Matt --- Paulo Soares <[EMAIL PROTECTED]> wrote: > -Use a PdfStamper to read in a page with form > fields, > fill in the fields AND FLATTEN THEM. Write the page > to byte array. > -Use a PdfCopy object and its getImportedPage() > method > to copy each byte array to A TEMPORARY file (using a > new > Reader constructed for each byte array). > -Repeat the above 2 steps for every page (variable > number. > -Read the temporary file and use PdfStamper to add > the page numbers. > > Paulo > > ----- Original Message ----- > From: "Matthew Metlis" <[EMAIL PROTECTED]> > To: <[email protected]> > Sent: Friday, June 16, 2006 12:11 AM > Subject: [iText-questions] Can't get PdfTemplate to > display text > > > > Hi, > > > > I am having trouble using PdfTemplate. I am > taking > > existing Java code that uses iText to print a > document > > with varying pages, and I want to add a > PdfTemplate to > > each page to display the total number of pages > (which > > I won't know until I get to the end of the > process). > > > > I would appreciate any help. I'm sorry this is so > > much code; I chopped out everything that I was > > confident would not be applicable to this problem. > > > > The printing code basically works as follows: > > -Use a PdfStamper to read in a page with form > fields, > > fill in the fields. Write the page to byte array. > > -Use a PdfCopy object and its getImportedPage() > method > > to copy each byte array to the final file (using a > new > > Reader constructed for each byte array). > > -Repeat the above 2 steps for every page (variable > > number). > > > > I've gotten the use of PdfTemplate to occur > without > > any runtime errors (at least any I can see from a > > console). I think I may be adding the template to > the > > wrong object. I have tried adding it to both the > > PdfCopy object and the PdfStamper object (not both > at > > once) - the places I tried separately I marked > with a > > "here?" comment. Whichever I do, no text shows up > in > > the PDF file from the template. The rest of the > > printing process works properly; the template is > the > > exception. > > > > Control flow starts in the generate() method. > Some of > > this is pseudo-code. > > > > FileOutputStream outStream; > > Document document; > > PdfCopy copier; > > Helper helper; > > > > generate() { > > helper = new Helper(); > > outStream = new FileOutputStream(OUT_FILENAME); > > for each page { // variable # of pages > > ByteArrayOutputStream bs = new > > ByteArrayOutputStream(); > > // read in a single page with form fields. > > PdfReader reader = new > > PdfReader(ORIGINAL_PDF_FILE); > > PdfStamper stamper = new PdfStamper(reader, > > bs); > > // code omitted: use the stamper to set > form > > fields. > > > addTemplateToPage(stamper.getOverContent(1)); > > // here? > > stamper.setFormFlattening(true); > > stamper.close(); > > bs.close(); > > concat(bs converted to byte[]); > > } // end for each page > > > > copier.close(); > > document.close(); > > outStream.close(); > > } > > > > void concat(byte[] b) { > > PdfReader reader = new PdfReader(b); > > int n = reader.getNumberOfPages(); > > > > if (document == null || copier == null) { > > document = new > > Document(reader.getPageSizeWithRotation(i)); > > copier = new PdfCopy(document, outStream); > > copier.setPageEvent(helper); > > document.open(); > > } > > > > PdfImportedPage page; > > for (int i = 0; i < n; ) { > > ++i; > > page = copier.getImportedPage(reader, i); > > // PdfImportedPage throws error on > > addTemplate() calls, so can't add template here. > > copier.addPage(page); > > > addTemplateToPage(copier.getDirectContent()); > > // here? > > copier.freeReader(reader); > > } > > } > > > > void addTemplateToPage(PdfContentByte cb) { > > if (template == null { > > template = cb.createTemplate(100, 100); > > template.setBoundingBox(new Rectangle(-20, > > -20, 300, 300)); // tried without this line too. > > helper.setTemplate(template); > > } > > cb.addTemplate(100, 100); > > } > > > > class Helper extends PdfPageEventHelper { > > PdfTemplate template; > > > > void setTemplate(PdfTemplate template) { > > this.template = template; > > } > > > > public void onCloseDocument(PdfWriter writer, > > Document document) { > > BaseFont helvetica = null; > > try { > > helvetica = > > BaseFont.createFont("Helvetica", BaseFont.WINANSI, > > false); > > } catch (Exception ex) { > > print errors and return; > > } > > > > template.beginText(); > > template.setFontAndSize(helvetica, 12); > > template.setTextMatrix(200, 200); > > template.showText(200, 200); > > template.endText(); > > // print statement here to verify this is being > > called - it is. > > } > > } // end class Helper > > > > > > __________________________________________________ > > Do You Yahoo!? > > Tired of spam? Yahoo! Mail has the best spam > protection around > > http://mail.yahoo.com > > > > > > _______________________________________________ > > iText-questions mailing list > > [email protected] > > > https://lists.sourceforge.net/lists/listinfo/itext-questions > > > _______________________________________________ > iText-questions mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/itext-questions > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions
