1

I have issue with iTextSharp. Let's assume I have two rows of fields in PDF file (the file is given and I don't know how was created)

Row 1:

data[0].#subform[0].Tabella1[0].Riga2[0].DATA[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINIPM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINILM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDLM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDAM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINIPP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINILP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDLP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDAP[0]

Row 2:

data[0].#subform[0].Tabella1[0].Riga3[0].DATA[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINIPM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINILM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDLM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDAM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINIPP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINILP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDLP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDAP[0]

I read this fields using below code:

            String newFile = source.Insert(source.Length - 4, "newModyfiy");
            using (FileStream outFile = new FileStream(newFile, FileMode.Create))
            {
                PdfReader pdfReader = new PdfReader(source);
                foreach (KeyValuePair<String, AcroFields.Item> kvp in pdfReader.AcroFields.Fields)
                {

                            int fileType = pdfReader.AcroFields.GetFieldType(kvp.Key);
                            string filedValue = pdfReader.AcroFields.GetField(kvp.Key);
                            string transFileName = pdfReader.AcroFields.GetTranslatedFieldName(kvp.Key);
                            textBox1.Text = textBox1.Text + fileType.ToString() + " " + filedValue + " " + transFileName + Environment.NewLine;
                }
                pdfReader.Close();
            }

I am getting for both rows values of the first row only. My target is to write values to those fields and save new file. When I use:

PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create), '\0', true);

I always overwrite values of first row (when I try to set value in second row it appears in first). If I change the last parameter PdfStamper to false it writes fileds correctlly but file is not editable manually.

Is it a matter of pdf file? Is there a way to read and then write values to proper fileds?

I have spent on this few days and could not find reason of this strange behaviour. Any small help or even clue will be appereciated.

Edit: I add mentioned PDF file. https://ufile.io/mwni5

I have deleted some object but general structure is kept.

notyou
  • 9
  • 3
  • *"Is it a matter of pdf file?"* - to allow checking this please share the pdf in question. – mkl Mar 11 '19 at 22:31
  • I wish I could share it but it contains personal details. Is there something I can check personally? Row 1 and Row 2 is Name of the fields from Key. So it sees all fields name correctly but mistakes when reading value. – notyou Mar 12 '19 at 10:30
  • *"Is there something I can check personally?"* - Well, the names look like XFA form names. Thus, please check whether your form is pure AcroForm, pure XFA, or hybrid. And if it is hybrid, please check whether the form definitions match. – mkl Mar 12 '19 at 11:02
  • I have edited question. You can download mentioned pdf file. Could you tell me something more, please? – notyou Mar 12 '19 at 13:57
  • Ok, first of all: This is a hybrid form. iText attempts to also honor the XFA part but has only a limited XFA support. I'd propose you drop the XFA part and use only the AcroForm part. (Valid) AcroForm forms are fully supported by iText, so that should allow you arbitrary manipulations. Is that an option? – mkl Mar 12 '19 at 16:04
  • Unfortunately I don't have any impact how pdf file is created. I just get it from one of a department and I can't tell them to create it different way. Current situation is I get this file from them then I fill it out manually then send them back. I want to make it automatically filled out by c# app with unchanged structurally pdf (only filled). Another suggestion? – notyou Mar 12 '19 at 16:33
  • I'm afraid I'm not knowledgeable enough in XFA forms to tell how to properly fill it by a program. (Aside from XFA being deprecated that's why asked whether dropping the XFA part would be ok... ;) ) – mkl Mar 12 '19 at 17:08
  • okay, thanks for your help anyway. At least I know it's XFA forms ;) – notyou Mar 12 '19 at 17:29

0 Answers0