It would seem you cannot open the Stamper in append mode e.g.: *PdfStamper
stamper = new PdfStamper(reader, output, '\0', true);*  and then replace
the XFA stream with a new one containing data.

If I do NOT open the Stamper in append mode, then my XFA replacement works
but my PDF is no longer Reader Enabled, so no win / win solution.

My workaround for now is to generate and supply the user with an XDP file.

Suggestions for any other workarounds welcome!


On Wed, Apr 11, 2012 at 2:22 PM, Mario Ribeiro <mario.f.ribe...@gmail.com>wrote:

> Hi iText Gurus
>
> New to iText.  I have searched the archives, forums and examples, found
> some others with the issue but never an explanation for a resolution.
>
> I have complex reader enabled XFA forms.  I want to populate them
> programatically with data while preserving the reader enabling as per
> example http://itextpdf.com/examples/iia.php?id=166.  The populating is
> happening by manipulating and switching out the DOM document for the XFA
> Form using XfaForm.setXfa as in method fillData2 in example
> http://itextpdf.com/examples/iia.php?id=164.  Difference being that I am
> reusing the Dom Doc retrieved from the PDF instead of creating my own from
> scratch.
>
> Program runs fine, but when I open the new PDF, reader enabling is
> preserved but none of the changes are showing, it looks the same as the
> source (size differs though).  I also write out the XML doc just to make
> sure it has the changes which it does.
>
> Where am I going wrong?
>
> The source code and the source PDF file can be found here:
> https://docs.google.com/open?id=0B9IjLSDCpdS5RTJYNFJmbmlGa1k
>
> Here is my program:
>
> public class populateXfaPdf {
>     private static final String SOURCE_FILE = "c:/temp/SF-424.pdf";
>     private static final String DESTINATION_FILE =
> "c:/temp/NEW_SF-424.pdf";
>     private static final String XML_FILE = "c:/temp/NEW_SF-424.xml";
>
>     public populateXfaPdf() {
>         super();
>     }
>
>     /**
>      * Find node in document by name
>      * @param doc
>      * @param name
>      * @return
>      */
>     private static Node findNode(Document doc, String name) {
>         DocumentTraversal traversal = (DocumentTraversal)doc;
>         NodeIterator iterator =
> traversal.createNodeIterator(doc.getDocumentElement(),
> NodeFilter.SHOW_ELEMENT, null, true);
>         for (Node n = iterator.nextNode(); n != null; n =
> iterator.nextNode()) {
>             if ((((Element)n).getTagName()).equals(name)) {
>                 return n;
>             }
>         }
>
>         return null;
>     }
>
>     /**
>      * update node's text content
>      * @param doc
>      * @param nodeName
>      * @param value
>      */
>     private static void injectNodeValue(Document doc, String nodeName,
> String value) {
>         Node n = findNode(doc, nodeName);
>         if (n != null && (n.getTextContent() == null ||
> n.getTextContent().length() < 1)) {
>             n.setTextContent(value);
>         }
>
>     }
>
>     /**
>      * Populate XML doc nodes with text values
>      * @param doc
>      */
>     public static void changeDocContent(Document doc) {
>         injectNodeValue(doc, "SF424_2_1:ProjectTitle", "My Test Title");
>         injectNodeValue(doc, "SF424_2_1:Email", "itext_t...@test.com");
>         injectNodeValue(doc, "SF424_2_1:ProjectStartDate", "2012-02-01");
>         injectNodeValue(doc, "SF424_2_1:ProjectEndDate", "2013-02-28");
>     }
>
>     /**
>      * Write XML doc out to file
>      * @param doc
>      * @throws TransformerException
>      * @throws FileNotFoundException
>      * @throws IOException
>      */
>     private static void docToFile(Document doc)
>         throws TransformerException, FileNotFoundException, IOException {
>         OutputStream out = new FileOutputStream(XML_FILE);
>
>         Result outputTarget = new StreamResult(out);
>         Transformer tf = TransformerFactory.newInstance().newTransformer();
>         tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
>         tf.setOutputProperty(OutputKeys.INDENT, "yes");
>         tf.transform(new DOMSource(doc), outputTarget);
>         out.close();
>     }
>
>     public static void main(String[] args)
>         throws FileNotFoundException, IOException, DocumentException,
> ParserConfigurationException, SAXException, TransformerException {
>         populateXfaPdf populateXfaPdf = new populateXfaPdf();
>
>         //get Reader Enabled XFA pdf
>         File file = new File(SOURCE_FILE);
>         FileInputStream fis = new FileInputStream(file);
>
>         //Output stream for destination file
>         OutputStream output = new FileOutputStream(DESTINATION_FILE);
>
>         //iText
>         PdfReader reader = new PdfReader(fis);
>
>         //New Stamper in append mode - preserve reader enabling
>         PdfStamper stamper = new PdfStamper(reader, output, '\0', true);
>
>         //Get XFA form
>         XfaForm xfa = new XfaForm(reader);
>
>         //Extract XML doc
>         Document doc = xfa.getDomDocument();
>
>         //Manipulate XML
>         changeDocContent(doc);
>
>         //set xfa doc to changed document
>         xfa.setDomDocument(doc);
>
>         //set changed = true --> not sure what this does?
>         xfa.setChanged(true);
>
>         //The switch --> does not seem to work!
>         XfaForm.setXfa(xfa, stamper.getReader(), stamper.getWriter());
>
>         //write XML doc out to file to varify changes
>         docToFile(doc);
>
>         stamper.close();
>         output.close();
>         fis.close();
>     }
> }
>
> --
> Regards,
> Mario
>



-- 
Regards,
Mario Ribeiro
------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to