pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maruan Sahyoun <sahy...@fileaffairs.de>
Subject Re: XFA Forms & PDFBox
Date Tue, 30 Sep 2014 16:27:23 GMT
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 <rajeevrmenong@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 <sahyoun@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 <rajeevrmenong@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.
>> 
>> 


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message