You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Rajeev Menon <ra...@gmail.com> on 2014/09/30 16:48:49 UTC

XFA Forms & PDFBox

Hi All,

Please advise if PDFBox can populate data to XFA (PDF) forms. I couldn't
find any code that illustrates how to pre-fill XFA forms using PDFBox.

Appreciate help on this.

Thanks
Rajeev.

Re: XFA Forms & PDFBox

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Please find a sample below

With kind regards

Maruan


 

           PDDocument document = PDDocument.load(pdfTemplate) ;
            PDDocumentCatalog catalog = document.getDocumentCatalog();
            PDAcroForm form = catalog.getAcroForm();
            
            if (form == null) {
                throw new IllegalArgumentException("No form contained in pdf template");
            }
            
            PDXFA xfa = form.getXFA();
            
            if (xfa == null) {
                throw new IllegalArgumentException("No XFA form contained in form content");                
            }
            
            COSArray xfaCos = (COSArray) xfa.getCOSObject();
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] xfaBytes = null;
            
            for (int i = 0; i < xfaCos.size(); i += 2) 
            {
                COSBase cosObj = xfaCos.getObject(i);
                if (((COSString) cosObj).getString().equals("datasets")) {
                    
                    InputStream xfaPartialStream = ((COSStream) xfaCos.getObject(i+1)).getUnfilteredStream();
                    
                    xfaBytes = new byte[xfaPartialStream.available()];
                    
                    int nRead = 0;
                    while ((nRead = xfaPartialStream.read(xfaBytes, 0, xfaBytes.length)) != -1) 
                    {
                      baos.write(xfaBytes, 0, nRead);
                    }
                    baos.flush();
                    
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db;
                    db = dbf.newDocumentBuilder();
                    
                    Document currentDataSet = db.parse(new ByteArrayInputStream(baos.toByteArray()));
                    Document importData = db.parse(xmlData);
                    
                    Node dataNode = currentDataSet.getElementsByTagName("xfa:data").item(0);
                    
                    dataNode.replaceChild(currentDataSet.adoptNode(importData.getDocumentElement().cloneNode(true)),dataNode.getFirstChild());
                    
                    PDStream str = new PDEmbeddedFile(document);
                    
                    TransformerFactory tf = TransformerFactory.newInstance();
                    Transformer transformer = tf.newTransformer();
                    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                    
                    transformer.transform(
                            new DOMSource(currentDataSet),
                            new StreamResult(str.createOutputStream())
                        );
                    
                    str.addCompression();
                    xfaCos.set(i+1,str);
                    
                }
            }
            
            
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            document.save(result);
            document.close();


Am 30.09.2014 um 17:53 schrieb Rajeev Menon <ra...@gmail.com>:

> Thanks Maruan. Do you have any sample code that pre-fill the form?
> 
> -Rajeev.
> 
> On Tue, Sep 30, 2014 at 11:26 AM, Maruan Sahyoun <sa...@fileaffairs.de>
> wrote:
> 
>> it's possible but - as you already found out - not illustrated yet. If you
>> try to pre fill Reader Extended forms though currently the Reader
>> Extensions will be invalidated.
>> 
>> BR
>> Maruan
>> 
>> Am 30.09.2014 um 16:48 schrieb Rajeev Menon <ra...@gmail.com>:
>> 
>>> Hi All,
>>> 
>>> Please advise if PDFBox can populate data to XFA (PDF) forms. I couldn't
>>> find any code that illustrates how to pre-fill XFA forms using PDFBox.
>>> 
>>> Appreciate help on this.
>>> 
>>> Thanks
>>> Rajeev.
>> 
>> 


Re: XFA Forms & PDFBox

Posted by Rajeev Menon <ra...@gmail.com>.
Thanks Maruan. Do you have any sample code that pre-fill the form?

-Rajeev.

On Tue, Sep 30, 2014 at 11:26 AM, Maruan Sahyoun <sa...@fileaffairs.de>
wrote:

> it's possible but - as you already found out - not illustrated yet. If you
> try to pre fill Reader Extended forms though currently the Reader
> Extensions will be invalidated.
>
> BR
> Maruan
>
> Am 30.09.2014 um 16:48 schrieb Rajeev Menon <ra...@gmail.com>:
>
> > Hi All,
> >
> > Please advise if PDFBox can populate data to XFA (PDF) forms. I couldn't
> > find any code that illustrates how to pre-fill XFA forms using PDFBox.
> >
> > Appreciate help on this.
> >
> > Thanks
> > Rajeev.
>
>

Re: XFA Forms & PDFBox

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
it’s possible but - as you already found out - not illustrated yet. If you try to pre fill Reader Extended forms though currently the Reader Extensions will be invalidated.

BR
Maruan

Am 30.09.2014 um 16:48 schrieb Rajeev Menon <ra...@gmail.com>:

> Hi All,
> 
> Please advise if PDFBox can populate data to XFA (PDF) forms. I couldn't
> find any code that illustrates how to pre-fill XFA forms using PDFBox.
> 
> Appreciate help on this.
> 
> Thanks
> Rajeev.