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.