0

I am trying to import the fdf document which has only the annots comments not the acro form field using the itext7 c#. Please suggest or provide the code snippet

Below is my FDF (Forms Data Format) document content

%FDF-1.2
%âãÏÓ
1 0 obj
<</FDF<</Annots[2 0 R]/F(/C/Users/Desktop/Emptypdf.pdf)/ID[<><>]/UF(/C/Users/Desktop/Emptypdf.pdf)>>/Type/Catalog>>
endobj
2 0 obj
<</Author(SYSTEM)/C[0.0 0.72 0.92]/Contents(FA)/DA(0 G 0 0.72 0.92 rg 0 Tc 0 Tw 100 Tz 0 TL 0 Ts 0 Tr /Arial 12 Tf)/DS(font: Arial,sans-serif 12.0pt; text-align:left; color:#00B8EB )/F 4/M(D:20230829121640+05'30')/NM(FA in page 1)/Page 0/RC(<?xml version="1.0"?><body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:APIVersion="Acrobat:23.3.0" xfa:spec="2.0.2" ><p dir="ltr"><span dir="ltr" style="font-size:12.0pt;text-align:left;color:#FF0000;font-w\
eight:normal;font-style:normal">FA</span></p></body>)/Rect[10.0 565.44 55.7559 585.44]/Subj(Text Box)/Subtype/FreeText/Type/Annot>>
endobj
trailer
<</Root 1 0 R>>
%%EOF

Below is the xml version of FDF ie.,XFDF file

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"
><annots
><freetext color="#00B8EB" flags="print" date="D:20230829121640+05'30'" name="FA in page 1" page="0" rect="10.000000,565.440000,55.755900,585.440000" subject="Text Box"
><contents-richtext
><body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:APIVersion="Acrobat:23.3.0" xfa:spec="2.0.2"
><p dir="ltr"
><span dir="ltr" style="font-size:12.0pt;text-align:left;color:#FF0000;font-weight:normal;font-style:normal"
>FA</span
></p
></body
></contents-richtext
><defaultappearance
>0 G 0 0.72 0.92 rg 0 Tc 0 Tw 100 Tz 0 TL 0 Ts 0 Tr /Arial 12 Tf</defaultappearance
><defaultstyle
>font: Arial,sans-serif 12.0pt; text-align:left; color:#00B8EB </defaultstyle
></freetext
></annots
><f href="Emptypdf_acrf.pdf"
/><ids original="F7F892FBFFEABF49A24F61F6C466DB21" modified="F7F892FBFFEABF49A24F61F6C466DB21"
/></xfdf
>

I tried using the below code

// Replace with the paths to your PDF and FDF files
            string pdfFilePath = "C:\\blankpage.pdf";
            string fdfFilePath = "C:\\blankpage.xfdf";

            using (PdfReader pdfReader = new PdfReader(pdfFilePath))
            {
                using (FileStream fs = new FileStream(fdfFilePath, FileMode.Open, FileAccess.Read))
                {
                    XmlDocument fdfDoc = new XmlDocument();
                    
                    fdfDoc.Load(fs);

                    var nsmgr = new XmlNamespaceManager(fdfDoc.NameTable);
                    nsmgr.AddNamespace("a", "http://ns.adobe.com/xfdf/");

                    XmlNodeList commentNodes = fdfDoc.SelectNodes("//a:annots", nsmgr);
                    foreach (XmlNode commentNode in commentNodes)
                    {
                        string annotationText = commentNode.InnerText;

                        // Apply the annotationText to the PDF using iTextSharp
                        PdfDictionary pageDict = pdfReader.GetPageN(1); // Change 1 to the page number you need

                        PdfArray annotsArray = pageDict.GetAsArray(PdfName.ANNOTS);
                        if (annotsArray == null)
                        {
                            annotsArray = new PdfArray();
                            pageDict.Put(PdfName.ANNOTS, annotsArray);
                        }

                        PdfDictionary commentDict = new PdfDictionary();
                        commentDict.Put(PdfName.TYPE, new PdfName("Annot"));
                        commentDict.Put(PdfName.SUBTYPE, new PdfName("Text"));
                        commentDict.Put(PdfName.CONTENTS, new PdfString(annotationText, PdfObject.TEXT_UNICODE));
                        // Set other annotation properties here

                        annotsArray.Add(commentDict);
                    }

                    using (FileStream outputPdfStream = new FileStream("C:\\output.pdf", FileMode.Create))
                    {
                        using (PdfStamper stamper = new PdfStamper(pdfReader, outputPdfStream))
                        {
                            stamper.Close();
                        }

                    }
                }
            }

The above code is not adding the FDF content into the pdf document.

  • 1
    What do you mean by "import"? Your code line opens the PDF file at `pdfFilePath` for processing and storing the resulting PDF file at `outputFilePath`, the processing being finished after you close `pdfDocument`. – mkl Aug 29 '23 at 06:36
  • I have FDF document, which has only the text annotations and I am trying to import/merge the FDF document into PDF programmatically using c#. – Kothandaraman Aug 29 '23 at 06:41
  • Do you mean like this: https://stackoverflow.com/questions/808670/combine-two-or-more-pdfs – Heinz Siahaan Aug 29 '23 at 06:46
  • No, I have updated my post with the FDF (Forms Data Format) document content in the question. – Kothandaraman Aug 29 '23 at 06:52
  • Ah, sorry, when originally reading your question, I misunderstood it because of @dbc's edit. – mkl Aug 29 '23 at 06:54
  • Sorry for the incorrect edit. – dbc Aug 29 '23 at 07:00

1 Answers1

0

NOTE from Adobe

FDF can include incremental changes to the PDF file, digital signatures, and graphical data, but XFDF cannot.

Hence for programming FDF is preferred.

To import an FDF you simply open by double click it then Acrobat Reader or other system Annotator will ask to open the PDF wrapper (for use by annotations or with any defined form fields to be populated). If the PDF template file is already in the correct location, then there is no need for Acrobat Reader to query which template to open.

enter image description here

For a programmable approach Adobe publish a "FDF toolkit" for SDK developers or you can simply use PDFtk and for that there is a java based example at https://github.com/ActiveArchitexture/FDF

In Pseudocode the program is as simple as

PDFreader in.FDF
PDFreader save out.PDF

So Adobe Acrobat Reader can do program line one but not 2 without using its JS based action scripted re-actions.

Whereas with say a portable Tracker PDF-Xchange we can use a one line program via shell:-
pdfxedit filled-001.fdf /close:mode=save "filled-001.pdf"
enter image description here

However the downside of PDFXedit is that the shell window will stay active for the next programmed file command. Hence more silent PDFtk is preferred.

However PDFtk also has oddities and limitations that need programming work arounds.

K J
  • 8,045
  • 3
  • 14
  • 36