使用 itextSharp 从现有 PDF 中获取/设置注释
我正在开发一个项目,我必须使用 iTextSharp.PdfReader 读取现有的 pdf,然后使用 getAnnotations 以便将它们插入到新生成的 pdf 中。
我的问题是我设法从输入的pdf中获取注释,但我找不到注释详细信息(例如目标页码,操作..),目前,我确实找到了RECT,DEST,SUBTYPE,但其他一个是 null
获取注释的代码:
for (int i = 1; i <= pagesNbr; i++)
{
iTextSharp.text.pdf.PdfReader read = new iTextSharp.text.pdf.PdfReader("TEST_mod.pdf");
iTextSharp.text.pdf.PdfDictionary pageDict = read.GetPageN(i);
iTextSharp.text.pdf.PdfArray annotArray = pageDict.GetAsArray(iTextSharp.text.pdf.PdfName.ANNOTS);
for (int j = 0; j < annotArray.Size; ++j)
{
iTextSharp.text.pdf.PdfDictionary curAnnot = annotArray.GetAsDict(j);
foreach (iTextSharp.text.pdf.PdfObject A in annotArray.ArrayList)
{
iTextSharp.text.pdf.PdfDictionary AnnotationDictionary = (iTextSharp.text.pdf.PdfDictionary)iTextSharp.text.pdf.PdfReader.GetPdfObject(A);
sw.WriteLine("\n ANNOTS pour la page n°: "+i+"\n");
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.TYPE));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.SUBTYPE));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.RECT));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.BORDER));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.DEST));
}
}
}
我的另一个问题是如何将它们重新插入到我新生成的 pdf 中,当然是在获取它们之后, 现在我使用此代码只是为了测试我是否真的可以手动插入注释
public void setAnnots(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Rectangle linkLocation,PdfDestination destination,int destPage)
{
PdfAnnotation link = PdfAnnotation.CreateLink(
writer,
linkLocation,
PdfAnnotation.HIGHLIGHT_INVERT,
destPage, destination);
writer.AddAnnotation(link);
}
请任何人都可以帮我解决这个问题,提前致谢。
I'm working on a project where i have to read an existing pdf using iTextSharp.PdfReader
, then getAnnotations
in order to insert them back in a new generated pdf.
My problem is that i managed to get Annotations from the input pdf, but i can't find the annotation details (such as destination page number, Action..), for now, i did found RECT, DEST, SUBTYPE, but the other one are null
Code to get annotation :
for (int i = 1; i <= pagesNbr; i++)
{
iTextSharp.text.pdf.PdfReader read = new iTextSharp.text.pdf.PdfReader("TEST_mod.pdf");
iTextSharp.text.pdf.PdfDictionary pageDict = read.GetPageN(i);
iTextSharp.text.pdf.PdfArray annotArray = pageDict.GetAsArray(iTextSharp.text.pdf.PdfName.ANNOTS);
for (int j = 0; j < annotArray.Size; ++j)
{
iTextSharp.text.pdf.PdfDictionary curAnnot = annotArray.GetAsDict(j);
foreach (iTextSharp.text.pdf.PdfObject A in annotArray.ArrayList)
{
iTextSharp.text.pdf.PdfDictionary AnnotationDictionary = (iTextSharp.text.pdf.PdfDictionary)iTextSharp.text.pdf.PdfReader.GetPdfObject(A);
sw.WriteLine("\n ANNOTS pour la page n°: "+i+"\n");
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.TYPE));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.SUBTYPE));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.RECT));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.BORDER));
sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.DEST));
}
}
}
My other problem, is how to re insert them in my new generated pdf, after getting them of course,
For now im using this code only to test if i can really insert an annoation manually
public void setAnnots(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Rectangle linkLocation,PdfDestination destination,int destPage)
{
PdfAnnotation link = PdfAnnotation.CreateLink(
writer,
linkLocation,
PdfAnnotation.HIGHLIGHT_INVERT,
destPage, destination);
writer.AddAnnotation(link);
}
Please can anyone help me solve this problem, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以提供这样一段用Itext7 java版本编写的代码
I can offer such a piece of code written in Itext7 java version