Hello:I have a PDF template with one page. I want generate many pages from this template and save them in a only file, joining them. If I do PdfStamper.setFormFlattening(true) (Fields are NOT editable in final file) when I fill template it is all OK. But if I do PdfStamper.setFormFlattening(false) (Fields are editable in final file) these fields are not correctly filled.
Below I wrote a program to test this:
======================== BEGIN OF PROGRAM ============================== import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.AcroFields; import com.lowagie.text.pdf.BadPdfFormatException; import com.lowagie.text.pdf.PdfCopy; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; public class TestITextFields { final String NOW = String.valueOf(System.currentTimeMillis()); /** * if <code>true</code> all fields are not editable* (PdfStamper.setFormFlattening(true)). If <code>false</code> all fields
* are editable (PdfStamper.setFormFlattening(false)). * */ final boolean freezeFields = false; final String templateName = "teste.pdf"; final String fieldName = "remetente"; /** * Fills field's template with page number. * * @param numPage page Number.* @return Array of bytes corresponding to new page generated from template.
* @throws IOException * @throws DocumentException */ private byte[] generatePages(int numPage) throws IOException, DocumentException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // we create a reader for a certain document PdfReader reader = new PdfReader(templateName); // filling in the form PdfStamper stamp = new PdfStamper(reader, baos); try { AcroFields form = stamp.getAcroFields(); form.setField(fieldName, String.valueOf(numPage)); stamp.setFormFlattening(freezeFields); baos.flush(); } finally { stamp.close(); baos.close(); } return baos.toByteArray(); } /** * Create a new file for each individual page. * * @param bytesPages List of bytes array corresponding to a page. * @throws IOException */ private void createAFilePerPage(List bytesPages) throws IOException { for (int i = 0; i < bytesPages.size(); i++) { System.out.println("Creating file for page " + (i + 1));OutputStream os = new FileOutputStream(NOW + "." + (i + 1) + ".pdf");
os.write((byte[]) bytesPages.get(i)); os.flush(); os.close(); } } /*** @param bytesPdfs Array of bytes list. Any array correspond to a Pdf Page. * @param writer PdfCopy instance corresponding to temporary Pdf File to be
* created. * @throws IOException * @throws BadPdfFormatException If a bad PDF format has been used to * construct a PdfObject. */private void fillPDF(List bytesPages, PdfCopy writer) throws IOException,
BadPdfFormatException { for (Iterator itBytesPage = bytesPages.iterator(); itBytesPage .hasNext();) { byte[] bytesPage = (byte[]) itBytesPage.next(); InputStream is = new ByteArrayInputStream(bytesPage); PdfReader pdfReader = new PdfReader(is); pdfReader.consolidateNamedDestinations(); writer.addPage(writer.getImportedPage(pdfReader, 1)); if (pdfReader.getAcroForm() != null) { writer.copyAcroForm(pdfReader); } is.close(); } } /** * Generate new file merging joining all pages. * * @param bytesPages Array of bytes list. Any array correspond to a Pdf * Page. * @throws Exception */ private void mergeAndSavePages(List bytesPages) throws Exception { System.out.println("Merging all pages"); // Step 1: creation of a document-object Document document = new Document((new PdfReader((byte[]) bytesPages .get(0))).getPageSizeWithRotation(1)); // Step 2: we create a writer that listens to the // document PdfCopy writer = new PdfCopy(document, new FileOutputStream(NOW + ".pdf")); // Step 3: we open the document document.open(); // Step 4: we add content fillPDF(bytesPages, writer); // Step 5: we close the document document.close(); } /** * @param args */ public static void main(String[] args) throws Exception { final int numPages = 5; TestITextFields appl = new TestITextFields(); // Generate Pages List pagesBytes = new ArrayList(numPages); for (int i = 1; i <= numPages; i++) { pagesBytes.add(appl.generatePages(i)); } appl.createAFilePerPage(pagesBytes); appl.mergeAndSavePages(pagesBytes); } } ========================= END OF PROGRAM =============================== If freezeFields attribute is true, there is not problem.If freezeFields attribute is false, there is not problem with individual pages file. Field's file are filled and editable. But in merged file (joining all pages) many of fields are neither filled nor editable.
Is this a bug? Or some kind of error that I am doing? Thanks, -- Rafael Ubiratam Clemente Afonso Sun Certified Java Programmer [EMAIL PROTECTED] --------------------------------- Where is Debug? Debug is on the Table!
teste.pdf
Description: Adobe PDF document