-1

Good Morning,

I don't know, how can i read the field name form below pdf. I used all methods for AcroFields, but all methods returns 0 or null http://www.finanse.mf.gov.pl/documents/766655/1481810/PIT-8C(7)_v1-0E.pdf

my code:

try {
        PdfReader.unethicalreading = true;
        PdfReader reader = new PdfReader(new FileInputStream("/root/TestPit8/web/notmod.pdf"));

        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("/root/TestPit8/web/testpdf.pdf"));
        AcroFields form = stamper.getAcroFields();


        form.setField("text_1", "666");
        form.setField("text_2", "666");
        form.setField("text_3", "666");
        form.setFieldProperty("text_3", "clrfflags", TextField.PASSWORD, null);
        form.setFieldProperty("text_3", "setflags", PdfAnnotation.FLAGS_PRINT, null);
        form.setField("text_3", "12345678", "xxxxxxxx");
        form.setFieldProperty("text_4", "textsize", new Float(12), null);
        form.regenerateField("text_4");
        stamper.close();
        reader.close();
        } catch( Exception ex) {
            ex.printStackTrace();
        }

Thx forhelp

user3557849
  • 55
  • 3
  • 8

1 Answers1

3

The form you share is a pure XFA form. XFA stands for the XML Forms Architecture.

Please read The Best iText Questions on StackOverflow and scroll to the section entitled "Interactive forms".

These are the first two questions of this section:

You are filling out the form as if it were based on AcroForm technology. That isn't supposed to work, is it? Your form is an XFA form!

Filling out an XFA form is explained in my book, in the XfaMovies example:

public void manipulatePdf(String src, String xml, String dest)
    throws IOException, DocumentException {
    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(xml));
    stamper.close();
    reader.close();
}

In this case, src is a path to the original form, xml is a path to the XML data, and dest is the path of the filled out form.

If you want to read the data, you need the XfaMovie example:

This reads the full form (all the XFA):

public void readXfa(String src, String dest)
    throws IOException, ParserConfigurationException, SAXException,
        TransformerFactoryConfigurationError, TransformerException {
    FileOutputStream os = new FileOutputStream(dest);
    PdfReader reader = new PdfReader(src);
    XfaForm xfa = new XfaForm(reader);
    Document doc = xfa.getDomDocument();
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    tf.transform(new DOMSource(doc), new StreamResult(os));
    reader.close();
}

If you only want the data, you need to examine the datasets node:

public void readData(String src, String dest)
    throws IOException, ParserConfigurationException, SAXException,
        TransformerFactoryConfigurationError, TransformerException {
    FileOutputStream os = new FileOutputStream(dest);
    PdfReader reader = new PdfReader(src);
    XfaForm xfa = new XfaForm(reader);
    Node node = xfa.getDatasetsNode();
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if("data".equals(list.item(i).getLocalName())) {
            node = list.item(i);
            break;
        }
    }
    list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if("movies".equals(list.item(i).getLocalName())) {
            node = list.item(i);
            break;
        }
    }
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    tf.transform(new DOMSource(node), new StreamResult(os));
    reader.close();
}

Note that I don't understand why you think there are fields such as text_1, text_2 in the form. XFA fields are easy to recognize because the contain plenty of [] characters.

Also: from the screenshot below (taken with iText RUPS), it is clear that there are no such fields in the form:

enter image description here

The tools are there on the iText web site. The documentation is there. Please use it!

Update:

So... instead of accepting my comprehensive answer, you decided to post a comment asking me to do your work in your place by asking where I can find example code? in spite of the fact that I provided links to XfaMovie and XfaMovies.

Well, here are two new examples for you:

Of course: I don't understand Polish, so I didn't always fill out the correct values, but now at least you have no longer a reason to ask where I can find example code?

Update 2:

In an extra comment, you claim that you can't find the NIP number (number 10 in the form) anywhere in the data structure.

This means either that you haven't examined data.xml, or that you don't understand XML.

Allow me to show the relevant part of the XML that contains the NIP number:

<Deklaracja xmlns="http://crd.gov.pl/wzor/2014/12/05/1880/" xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/">
    ....
    <Podmiot2 rola="Podatnik">
        <etd:OsobaFizyczna>
            <etd:NIP>0123456789</etd:NIP>
            <etd:ImiePierwsze>JUST TRY</etd:ImiePierwsze>
            <etd:Nazwisko>DUDE</etd:Nazwisko>
            <etd:DataUrodzenia>2015-02-19</etd:DataUrodzenia>
        </etd:OsobaFizyczna>
    </Podmiot2>
    ...
</Deklaracja>

In other words, the field name you're looking for is probably something like this: Deklaracja[0].Podmiot2[0].OsobaFizyczna[0].NIP[0] (whatever these words may mean, I only know one Polish word: Podpis).

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Do you have any example, where i can find example code. Simply, i need text to some textfield's in pdf form. – user3557849 Feb 19 '15 at 09:10
  • What do you mean when you say "any program similar to i"? – Bruno Lowagie Feb 19 '15 at 13:36
  • Could you recommend me, any program similar to iText RUPS. I am not quite sure, but this program cut me a some code. For example I can't find source code to first form in pdf document (identyfikator podakowy NIP podmiotu) and if I could ask to help you. In ten form (10. Identyfikator podatkowy and select Field NIP and write down the nip number near this field) – user3557849 Feb 19 '15 at 13:39
  • I'll update my answer. Are you a student? Your questions don't sound like you're a developer. – Bruno Lowagie Feb 19 '15 at 13:55
  • Do you know the concept of StackOverflow? If somebody gives you a correct answer (which I did), you accept the answer. – Bruno Lowagie Feb 19 '15 at 14:07
  • I have the same problem as before. U used : – user3557849 Feb 19 '15 at 15:30
  • I used : bcpkix-jdk15on-1.51.jar, bcprov-jdk15on-151.jar, itextpdf-5.5.0.jar and this xml file(http://itextpdf.com/sites/default/files/data_0.xml) and FillXFA2. I have the same problem as before. I don't still changed some forms, but Your form fields are filled out correctly – user3557849 Feb 19 '15 at 15:42
  • Well, my answer is the most authoritative answer you can get on StackOverflow. If you can't make the examples work, then that's certainly not because I didn't give you sufficient guidance. You're a student. Why don't you ask one of your teachers? – Bruno Lowagie Feb 19 '15 at 17:27
  • Because I was sure that the task, I will execute 100%. I wanted to create form in html(css) and next save it in html, but i stuck at problem with scaling page and support css files., next i tried with conversion xml to pdf and at last, today I came across a change interactive pdf form. Probably my lecturer will not help me with this problem and also i would like to thank you very much for the current help. Was it possible to ask for the last guide especially the program working on your computer – user3557849 Feb 19 '15 at 21:42
  • The examples that work on my computer are already referenced in my answer. Note that the server renamed data.xml to data_0.xml because that there was already another file with that name. – Bruno Lowagie Feb 20 '15 at 07:16
  • I used xml files data_0.xml and java class FillXFA2, but First and seventh interactive field is not fill out. if these files works good on your computer, so probably i have problem with libraries. I used : bcpkix-jdk15on-1.51.jar, bcprov-jdk15on-151.jar, itextpdf-5.5.0.jar. Additionally Yours files after save to pdf, are editable while my files can't be change. – user3557849 Feb 20 '15 at 08:09
  • Thwn try iText 5.5.5. – Bruno Lowagie Feb 20 '15 at 08:51
  • did not help. I have still the same problem. http://pastebin.com/wSgFpPcg http://pastebin.com/UtKz7pee and I wanted to apologize you for a still boring, but I'm going to write a similar program for the thesis, and good working example of the pdf help me to broaden its features. – user3557849 Feb 20 '15 at 09:43
  • Can i please a tiny prompt Are these above source codes correct?? Because i don't know, where i should to looking for a error. Probably it is a vine one of the jar – user3557849 Feb 20 '15 at 11:17
  • By all reasons, this answer should be marked correct. – Rahul Kumar Aug 09 '16 at 16:01