使用 itextSharp 从现有 PDF 中获取/设置注释

发布于 2025-01-18 10:58:16 字数 1909 浏览 0 评论 0原文

我正在开发一个项目,我必须使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2025-01-25 10:58:16

我可以提供这样一段用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);
    }
}

I can offer such a piece of code written in Itext7 java version

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文