0

After reading many stackoverflow and tried many solutions I'm stuck with this:

I am receiving a PDF that I cannot change and need to automatically process it. The PDF is a PDF form with 2 fields and submit button. The following code is the closes I came to what I need to do:

public static final String SRC = "C:\\Dev\\test.pdf";
public static final String DEST = "C:\\Dev\\test_result.pdf";
public static final String DATA = "C:\\Dev\\data.xml";

File file = new File(DEST);
PdfReader reader = new PdfReader(SRC);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST));
AcroFields form = stamper.getAcroFields();
XfaForm xfa = form.getXfa();
xfa.fillXfaForm(new FileInputStream(DATA));

This gives a null pointer:

Exception in thread "main" java.lang.NullPointerException
at com.itextpdf.text.pdf.XfaForm.fillXfaForm(XfaForm.java:1168)
at com.itextpdf.text.pdf.XfaForm.fillXfaForm(XfaForm.java:1146)
at com.itextpdf.text.pdf.XfaForm.fillXfaForm(XfaForm.java:1134)
at com.itextpdf.text.pdf.XfaForm.fillXfaForm(XfaForm.java:1131)

I can get and set the fields on the form with this code:

AcroFields fields = reader.getAcroFields();
fields.setField("pdfForm.loginUser", "myemail@domain.com");
fields.setField("pdfForm.loginPass", "mypassword");

How do I convert the Acrofields to XfaForm?

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Henk
  • 1
  • 1
    You ask: *"How do I convert the Acrofields to XfaForm?"* The answer is: *"You don't!"* AcroForm technology and XFA are too different from each other. They are sometimes used alongside each other in a hybrid form, but it is no use converting one to the other. Looking at the field names `pdfForm.loginUser`, I assume that you have a pure AcroForm form. If it were a hybrid form, the field name would look like `pdfForm[0].loginUser[0]`, and that's not the case. You get the NPE because `xfa` is `null`. There is no XFA inside the PDF. Why would you need XFA? **XFA is deprecated in PDF 2.0!** – Bruno Lowagie Jun 07 '18 at 06:13
  • 1
    Also: the question in the subject of your post is totally different from what you have in the body of your post. In the subject, you are asking about **submitting data**; in the body, you already have the data, and you are asking about **filling out data**. There is a huge difference between **submitting a form** and **filling out a form**, so please clarify. – Bruno Lowagie Jun 07 '18 at 06:16
  • I need the XFA form in order to submit the form. The only way I could get online to submit a PDF form (meaning clicking the button programatically) was by using the XFA form. – Henk Jun 07 '18 at 12:18
  • That's a very strange claim. Are you saying that submitting a form based on AcroForm technology is impossible –which would be absurd— or not desired —which is odd, because if it's XML you want, why not submit the AcroForm fields as XFDF? – Bruno Lowagie Jun 07 '18 at 12:20
  • So, awnsering your second question. The title is related. I am trying to programatically complete a form and NOT save it to PDF but launch the button on the form. This button HTTP posts the form field values to a URL. – Henk Jun 07 '18 at 12:21
  • The PDF submit then return a new PDF file. I will look into XFDF - thanks. – Henk Jun 07 '18 at 12:22
  • Is there any viewer involved? Also: your designs sounds like: I need to go from Amsterdam to Brussels, but I want to complicate things by going to Paris first. If you have the field values, why not just post them to the URL? Why first create a PDF? – Bruno Lowagie Jun 07 '18 at 12:23
  • Please read my answer to this question: https://stackoverflow.com/questions/29113588 Then clarify if what I describe in that answer is part of what you need. – Bruno Lowagie Jun 07 '18 at 12:25
  • Or maybe you want to create an XFDF file on the client side, post it to a server, and then serve a PDF. In that case, you'll need my answer to this question: https://stackoverflow.com/questions/30508966 – Bruno Lowagie Jun 07 '18 at 12:35
  • Thing is - I do not create the PDF. I receive it from another company. When you open the PDF manually, the PDF form is opened with username and password and submit button. After enetering username and pasword, hit the botton and another PDF open - I asume the button URL post return PDF file. – Henk Jun 07 '18 at 14:26
  • So, I can try and take Paris out of the loop - just not sure on the post message format. Will engage with source company. – Henk Jun 07 '18 at 14:27
  • When you revceive the PDF: (1.) extract the field names, (2.) examine the submit format defined for the submit button: GET or POST, and HTML or FDF or XFDF, (3.) submit the key/value pairs using the method discovered in step 2. – Bruno Lowagie Jun 07 '18 at 15:39

0 Answers0