javapdfpdfboxxfa

Programatically fill Government PDF (XFA dynamic)


I need to fill a form from a XFA PDF generated with LiveCycle. I'm using PDFBox 3.0.0-beta1 atm and I've tried iText 4.2.0 and PDFBox 2.0.28 but with no success. Anything I do to the PDF, results in a unusable PDF, it breaks the usage rights if I understood correctly. Below is my java code:

PDDocument doc = Loader.loadPDF(new File(Paths.get(FileUtils.listFiles(new File("formulare"), new PrefixFileFilter("F1129"), null).toArray()[0].toString()).toUri()));
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDAcroForm acroForm = catalog.getAcroForm();
PDXFAResource xfa1 = acroForm.getXFA();

COSStream cosout = doc.getDocument().createCOSStream();
OutputStream out = cosout.createRawOutputStream();

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();

StreamResult result = new StreamResult(out);
Document src2 = acroForm.getXFA().getDocument();

NodeList dataElements = src2.getElementsByTagName("script");
if (dataElements != null) {
  for (int i = 0; i < dataElements.getLength(); i++) {
    if (i == 45) {                   
       dataElements.item(i).setTextContent(dataElements.item(i).getTextContent() + "csDataTool.GetInstance().ExecuteImport();");
    }
  }
}

transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.transform(new DOMSource(src2), result);

PDXFAResource xfaout = new PDXFAResource(cosout);
acroForm.setXFA(xfaout);
out.close();

FileOutputStream fos = new FileOutputStream("f1129Modified.pdf");
doc.saveIncremental(fos);
doc.close();
cosout.close();
fos.flush();
fos.close();

i == 45 (that is the position of the script I want to edit)

But after I modify the XFA, the PDF opens normally but I can't to anything in it. No errors, no warnings... but if I just do this

dataElements.item(i).setTextContent(dataElements.item(i).getTextContent());

it works perfectly...

My question is: is this even possible? To modify the javascript without breaking usage rights? If not, how can I fill this PDF? The XML that they want to be attached has different tags than the XSD I've tried to find inside the XFA.

The PDF in question can be downloaded from here: https://mfinante.gov.ro/documents/2552173/2552377/31.OrdinPlataElectronic_2023_05_19_A2.0.26+.pdf/5acf3ff7-7ff1-aa2c-283c-151d49af0d8b?t=1684492636871&download=true

An example of a XML to be imported:

<?xml version="1.0"?>
<f1129 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="mfp:anaf:dgti:f1129:declaratie:v1" xmlns="mfp:anaf:dgti:f1129:declaratie:v1" versiune_pdf="A2.0.21" d_rec="0" suma_control="3644129" total_opm="245.5" nr_inregistrari="1" luna_r="12" an="2022" data_document="19.12.2022" nr_document="1234567890" nume_ip="asdasdas" adresa_ip="asddas" cui_ip="3643884" tip_ent="1">
  <rand_op nr_op="32" iban_platitor="RO38TREZ24A670503200109X" den_trezorerie="TREZORERIA  STATULUI" cod_program="0000000000" cod_angajament="AAAFSF23N4X" ind_angajament="AA2" cui_beneficiar="17460640" den_beneficiar="Your Consulting SRL" iban_beneficiar="RO38TREZ24A670503200109X" den_banca_trez="TREZORERIA  STATULUI" suma_op="245.5" explicatii="asdasd" />
</f1129>

The strangest thing is that if I use Adobe Reader PRO everything works fine. xD And I can't find a reason why ...


Solution

  • Although it is not completely clear what you want*, here are some answers to part of your questions:

    My question is: is this even possible? To modify the javascript without breaking usage rights?

    It is a signature so no. If you manipulate the document you'll break the signature. In the past there was a mechanism called "reader enabled" where you could unlock features in _dobe reader for certain documents. It might be that the document was "reader enabled" and thus allows editing XFA forms in _dobe reader. Found the following infos as part of the signature:

    appRightsDocument: FullSave
    appRightsForm: FillIn,Add,Delete
    appRightsSignature: Modify
    appRightsAnnots: 
    appRightsEF: Create,Delete,Modify,Import
    

    So to change the JavaScript you have to remove the UA-signature but then you might loose the rights to do stuff in _dobe reader.

    And finally one remark about XFA: XFA is a proprietary standard of adobe and has been deprecated in PDF2.0. I would not use it for any project (ever).


    *