我正在进行一个项目,在这个项目中,我必须使用iTextSharp.PdfReader
读取现有的pdf,然后使用getAnnotations
将它们插入到新生成的pdf中。
我的问题是,我设法从输入pdf中得到注释,但是我找不到注释的细节(比如目标页码,Action.),目前我确实找到了RECT,DEST,SUBTYPE,但是另一个是空的。
获取注释的代码:
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);
}
请任何人帮我解决这个问题,谢谢。
发布于 2022-05-13 23:57:31
我可以提供这样一段用Itext7 java版本编写的代码
public void test() throws IOException {
try(
PdfDocument pdfDocument = new PdfDocument(
new PdfReader(new File("old.pdf")));
PdfDocument newpdfDocument = new PdfDocument(new PdfWriter(new File("new.pdf")));
) {
PdfPage page = pdfDocument.getPage(1);
java.util.List<PdfAnnotation> annotations = page.getAnnotations();
for (PdfAnnotation annotation : annotations) {
System.out.println(("ANNOTS pour la page n°: "));
System.out.println(annotation.getSubtype());
System.out.println(annotation.getRectangle());
System.out.println(annotation.getBorder());
System.out.println(annotation.getPdfObject().get(PdfName.Dest));
System.out.println(annotation.getPdfObject().get(PdfName.Type));
System.out.println();
}
System.out.println(annotations);
newpdfDocument.addNewPage();
PdfAnnotation pdfAnnotation = annotations.get(0);
PdfDictionary object = new PdfDictionary();
object.put(PdfName.Subtype, pdfAnnotation.getSubtype());
object.put(PdfName.Rect, pdfAnnotation.getRectangle());
object.put(PdfName.Border, pdfAnnotation.getBorder());
object.put(PdfName.Type, pdfAnnotation.getPdfObject().get(PdfName.Type));
PdfAnnotation result = PdfAnnotation.makeAnnotation(object);
PdfPage newPage = newpdfDocument.getPage(1);
newPage.addAnnotation(result);
}
}
https://stackoverflow.com/questions/71692704
复制相似问题