This is one of those questions asked without thinking. You can use
PdfCopyFields to keep the fields but unless you rename the fields they will
echo each other of the same name. Are you sure this is what you want?

----- Original Message ----- 
From: "Rafael Afonso" <[EMAIL PROTECTED]>
To: <itext-questions@lists.sourceforge.net>
Sent: Wednesday, August 03, 2005 6:41 PM
Subject: [iText-questions] Merging Pages erase fields


> 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!
>
>



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to