我正在尝试使用 itext7 c# 导入仅包含注释注释而不包含 acro 表单字段的 fdf 文档。请建议或提供代码片段
以下是我的FDF(Forms Data Format)文档内容
%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
下面是 FDF 的 xml 版本,即 XFDF 文件
<?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
>
我尝试使用下面的代码
// 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();
}
}
}
}
上面的代码并没有将FDF内容添加到pdf文档中。
要导入 FDF,您只需双击打开它,然后 Acrobat Reader 或其他系统注释器将要求打开 PDF 包装器(供注释使用或填充任何定义的表单字段)。如果 PDF 模板文件已位于正确位置,则无需 Acrobat Reader 查询要打开哪个模板。
对于可编程方法,Adobe 为 SDK 开发人员发布了“FDF 工具包”,或者您可以简单地使用 PDFtk,为此,在 https://github.com/ActiveArchitexture/FDF
有一个基于 java 的示例