how to avoid generating temp file?

how to avoid generating temp file?

Forum member · Feb 24, 2010
Hello, I need to load a PDF part template, fill some form fields and then import the populated area into the actual PDF document. I ended with the following piece of code but I hope there is a better way to achieve the same thing without the need for creating a temporary file? TIA, Best regards, Giovanni /** * Returns the footer part * * @return the footer part */ private static PageElement getFooter() { // fill placeholders MergeDocument footer = new MergeDocument("footer.pdf", new MergeOptions(true)); footer.getForm().getFields().getFormField("ph0001").setValue("Juan Perez"); footer.getForm().getFields().getFormField("ph0002").setValue("Head of Customer Acquisition"); footer.getForm().getFields().getFormField("ph0003").setValue("Dayana Gonzales"); footer.getForm().getFields().getFormField("ph0004").setValue("Head of Customer Satisfaction"); footer.draw("temp.pdf"); int pageNumber = 1; int xOffset = 10; int yOffset = 400; ImportedPageArea footerArea = new ImportedPageArea("temp.pdf", pageNumber, xOffset, yOffset); footerArea.getContents().setClipLeft(0); footerArea.getContents().setClipTop(0); footerArea.getContents().setClipRight(200); footerArea.getContents().setClipBottom(200); return footerArea; }

Re: how to avoid generating temp file?

Support · Feb 24, 2010
Hello, You can write the PDF data to a byte array or to a memory stream by using the overloaded Draw method. You can refer to the Document Output topic on our help documentation. You can create a PdfDocument object using the byte array or memory stream and then you can use the PdfPage object to create the ImportedPageArea class. You can refer to the ImportedPageArea constructors on our help documentation. This way you can avoid saving the PDF disk and loading it again. Thanks, ceTe Software Support Team.

Re: how to avoid generating temp file?

Forum member · Feb 24, 2010
Hello, Thank you for your support! I modified the code to the snippet below but now it won't attach anything to the ending document. Please help me make sure this is the intended use of the API. TIA, Best regards, Giovanni /** * Returns the footer {@link PageElement} * * @return the footer {@link PageElement} */ private static PageElement getFooter() { // fill placeholders on template footer MergeDocument mergeDocument = new MergeDocument("footer.pdf", new MergeOptions(true)); mergeDocument.getForm().getFields().getFormField("ph0001").setValue("Juan Perez"); mergeDocument.getForm().getFields().getFormField("ph0002").setValue("Head of Customer Acquisition"); mergeDocument.getForm().getFields().getFormField("ph0003").setValue("Dayana Gonzales"); mergeDocument.getForm().getFields().getFormField("ph0004").setValue("Head of Customer Satisfaction"); // output the populated template to a PdfDocument ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); mergeDocument.draw(outputStream); PdfDocument pdfDocument = new PdfDocument(new ByteArrayInputStream(outputStream.toByteArray())); // create an imported area from the in-memory pdfDocument int pageNumber = 1; int xOffset = 0; int yOffset = 400; ImportedPageArea importedArea = new ImportedPageArea(pdfDocument.getPage(pageNumber), pageNumber, xOffset, yOffset); importedArea.getContents().setClipLeft(50); importedArea.getContents().setClipTop(50); importedArea.getContents().setClipRight(50); importedArea.getContents().setClipBottom(50); return importedArea; }

Re: how to avoid generating temp file?

Support · Feb 24, 2010
Hello Giovanni, Are you not seeing the form field values filled in when using ImportedPageArea? IF yes, it is the desired behavior. When using the ImportedPageArea, it only imports the contents of the page and not any annotations like form fields that exist on the page of the PDF. You can try to flatten the PDF and do the form filling using the CreateLabel method and that may work for you. That way the PDF will have the data you need populated without form fields and ImportedPageArea will work fine in that case. Thanks, ceTe Software Support Team

Re: how to avoid generating temp file?

Forum member · Feb 26, 2010
Hello, No, using the code above I don't see anything coming from the ImportedPageArea. This is why I posted again before. It just does not work, the API javadoc is not very clear either what API sequence is the correct one for a given use-case. My use case is simple ... just populate placeholders in a given template and then import that as an area into a separate existing document. Can you please be specific about "flatten the PDF" or "do the form filling using the CreateLabel method" this can take me up to hours to figure out what you mean in terms of code. Thanks in advance, Best regards, Giovanni

Re: how to avoid generating temp file?

Support · Feb 26, 2010
Hello Giovanni, When using the ImportedPageArea it will not get the data of the form fields to the other pages. You will have to do the form flattening using the CreateLabel on the field and then take the ImportedPageArea. Below is a sample code for this. public static void main(String[] args){ Document document = new Document(); Page page = new Page(); ImportedPageArea area = getFooter(); page.getElements().add(area); document.getPages().add(page); document.draw("D:/Attachments/new.pdf"); } private static ImportedPageArea getFooter() { PdfDocument pdf = new PdfDocument("C:/Temp/Footer.pdf"); MergeDocument mergeDocument = new MergeDocument(pdf); PdfFormField f1_1 = pdf.getForm().getFields().getPdfFormField("f1-1"); PdfFormField f1_2 = pdf.getForm().getFields().getPdfFormField("f1-2"); f1_1.createLabel(mergeDocument.getPages().getPage(f1_1.getOriginalPageNumber() - 1), "Acme Corp.", com.cete.dynamicpdf.Font.getCourier(), 12); f1_2.createLabel(mergeDocument.getPages().getPage(f1_2.getOriginalPageNumber() - 1), "ceTe Software", com.cete.dynamicpdf.Font.getCourier(), 12); byte[] data = mergeDocument.draw(); PdfDocument pdfDocument = new PdfDocument(data); ImportedPageArea importedArea = new ImportedPageArea(pdfDocument.getPage(1), 0, 400); importedArea.getContents().setClipLeft(50); importedArea.getContents().setClipTop(50); importedArea.getContents().setClipRight(50); importedArea.getContents().setClipBottom(50); return importedArea; } Thanks, ceTe Software Support Team.

Re: how to avoid generating temp file?

Forum member · Feb 26, 2010
Hello, Thank you very nice! it works nicely. However there is a slight issue with this approach. The problem is that it seems that I have to hardwire the Font type and size and this is not optimal. I need the newly created labels to take the Font information from the original field ... f1_2.createLabel(mergeDocument.getPages().getPage(f1_2.getOriginalPageNumber() - 1), "ceTe Software", com.cete.dynamicpdf.Font.getCourier(), 12); I could not find any alternative to omit passing specific format information => that's why I need the templates in the first place. Neither I could find a way to grab the correct Font from the field. Any ideas? Thanks in advance, Best regards, Giovanni

Re: how to avoid generating temp file?

Support · Feb 26, 2010
Hello Giovanni, It is not possible to find out what font and font size is being used by the form field programatically using the API. You will need to know what font and its font size is used by the form fields before hand and use that same font in the CreateLabel method. Using CreateLabel method is the only possible way to do form filling and use ImportedPageArea together. Thanks, ceTe Software Support Team