当前位置: 首页 > 知识库问答 >
问题:

如何识别iTextSharp在PDF上找到的注释/链接位置

廉雅惠
2023-03-14

我不知道如何恰当地提出我的问题,但我会尽力而为。

我们有一个逻辑来检查文档中的注释/链接。我们有一份PDF文档没有通过检查。然而,当我们检查这份文件时,我们没有发现任何链接或评论或注释的迹象。

这是评论/注释部分的屏幕截图,指示找不到评论。

然而,当我们通过C# iTextSharp代码运行这个文档时,链接确实出现了,但是没有任何细节

以下是我们的代码,其中子类型返回为“链接”,但“名称”,“内容”,“a”,“bs”,“边框”,“c”,“t”,“h”,“m”,“p”,“rect”,“类型”变量返回null/nothing。因此,我的问题是。我如何知道 PDF iTextSHarp 究竟能找到什么?它的位置是什么?它的内容是什么?因为它是完全不可见的,不可能在文档上找到

提前万分感谢!

    public string AnyPDFCheckComments(string inFileName, string strUsername, string strFilename)
    {
            string strCommentType = string.Empty;
            string strWidgetFound = string.Empty;
            string strPageNumber = string.Empty;
            string message = string.Empty;
            string strComments = string.Empty;
            string strCommentsFound = string.Empty;
            int intCommentCount = 0;

            PdfReader reader = new PdfReader(inFileName);

            for (int i = 1; i <= reader.NumberOfPages; ++i)
            {
                strPageNumber = i.ToString();
                if (intCommentCount >= 5)
                {
                    break;
                }

                PdfDictionary pagedic = reader.GetPageN(i);

                PdfArray annotarray = (PdfArray)PdfReader.GetPdfObject(pagedic.Get(PdfName.ANNOTS));

                if (annotarray == null || annotarray.Size == 0)
                {
                    continue;
                }

                // Finding out the comments
                foreach (object annot in annotarray.ArrayList)
                {
                    PdfDictionary annotationDic = null;
                    if (annot is PdfIndirectReference)
                    {
                        annotationDic = (PdfDictionary)PdfReader.GetPdfObject((PdfIndirectReference)annot);
                    }
                    else
                    {
                        annotationDic = (PdfDictionary) annot;
                    }

                    PdfString name = annotationDic.GetAsString(PdfName.T);
                    PdfString contents = annotationDic.GetAsString(PdfName.CONTENTS);

                    PdfString a = annotationDic.GetAsString(PdfName.A);
                    PdfString bs = annotationDic.GetAsString(PdfName.BS);
                    PdfString border = annotationDic.GetAsString(PdfName.BORDER);
                    PdfString c = annotationDic.GetAsString(PdfName.C);
                    PdfString t = annotationDic.GetAsString(PdfName.T);
                    PdfString h = annotationDic.GetAsString(PdfName.H);
                    PdfString m = annotationDic.GetAsString(PdfName.M);
                    PdfString p = annotationDic.GetAsString(PdfName.P);
                    PdfString rect = annotationDic.GetAsString(PdfName.RECT);
                    PdfString type = annotationDic.GetAsString(PdfName.TYPE);

                    **PdfName subType = (PdfName)annotationDic.Get(PdfName.SUBTYPE);**
                    if ((subType.Equals(PdfName.TEXT)) && (strCommentsVariables.IndexOf("text") != -1))
                    {
                        strCommentType = "text";
                        //break;
                    }
                    else if ((subType.Equals(PdfName.FREETEXT)) && (strCommentsVariables.IndexOf("freetext") != -1))
                    {
                        strCommentType = "freetext";
                        //break;
                    }
                    else if ((subType.Equals(PdfName.HIGHLIGHT)) && (strCommentsVariables.IndexOf("highlight") != -1))
                    {
                        strCommentType = "highlight";
                        //break;
                    }
                    **else if ((subType.Equals(PdfName.LINK)) && (strCommentsVariables.IndexOf("Link") != -1))
                    {
                        strCommentType = "Link";
                        //break;
                    }**
                    else if ((subType.Equals(PdfName.SQUARE)) && (strCommentsVariables.IndexOf("Rectangle-Square") != -1))
                    {
                        strCommentType = "Rectangle-Square";
                        //break;
                    }
                    else if ((subType.Equals(PdfName.FILEATTACHMENT)) && (strCommentsVariables.IndexOf("FileAttachment") != -1))
                    {
                        strCommentType = "FileAttachment";
                        //break;
                    }
                    else if ((subType.Equals(PdfName.WIDGET)) && (strCommentsVariables.IndexOf("widget") != -1))
                    {
                        strWidgetFound = "widget";
                    }

                    if ((strCommentType != ""))
                    {
                        strCommentsFound = "Yes";
                        intCommentCount = ++intCommentCount;
                        strComments = strComments + "<BR>" + "A comment type of '" + "<b>" + strCommentType + "</b>" + "' has been found on page no: " + "<b>" + strPageNumber + "</b>";
                        if (intCommentCount == 5)
                        {
                            break;
                        }
                        else
                        {
                            strCommentType = string.Empty;
                        }
                    }
                }
            }

            return strComments;

        }
    }

共有1个答案

何昆
2023-03-14

这是我们的代码,其中子类型作为“链接”返回,但“名称”、“内容”、“a”、“bs”、“边界”、“c”、“t”、“h”、“m”、“p”、“rect”、“类型”变量返回空/无。

你试图以字符串的形式检索所有这些值。但是

  • 链接动作“a”和页面“p”是字典,边框样式(“bs”)也是字典
  • “border”、颜色“c”和矩形“rect”是数组
  • 突出显示模式“h”和“type”是名称

因此您将无法将它们作为字符串检索。

此外,“名称”/“t”(T)是表单字段名称,因此不适用于链接。“内容”(特别是对于链接)和修改日期“m”是可选的。

因此,我的问题是。我怎样才能知道PDF iTextSHarp到底找到了什么?它的位置在哪里?它的内容是什么?因为它完全看不见,也不可能在文件上找到

首先,将值作为它们的实际类型进行检索。

然后您从'rect'中获取位置,这是必填项。(如果'rect'保持null,则注释无效。)

关于内容:注释不需要有内容,特别是链接注释不需要。

 类似资料:
  • 我需要在现有的pdf上添加一些文本注释。我设法创建了注释并显示它们。唯一的问题是,当在Adobe Reader中打开pdf文档时,注释的内容(如果已最大化)会显示在页面的右侧。 可以将展开标注的坐标设置为与图标相同?(我在用itext 5操作pdf) 提前谢谢

  • 我正试图在我的vue应用程序上创建一个链接到本地pdf文件。现在我的问题是。 我的PDF文件位于assets/static/中,并且现在像这样链接到它们。

  • 问题内容: 我想在叠加文字中添加一个链接。我已经读过,使用Anchor 仅适用于从头开始制作的文档,不适用于现有的pdf。我的 代码是向每个页面添加一个覆盖文本。我的目标是使 该文本的一部分可点击。我不知道如何制作 短语中的链接注释。 这是我的代码: 所以我的叠加看起来像这样: 从[匿名]下载我的网站在1:20 2015年2月17日 上午CST 如何将我的网站转换为链接注释?在SO中搜索时,我 找

  • 我在eclipse中签出了一个gradle项目,但它不能识别任何注释 其中配置是注释。 所有与注释对应的导入也失败,错误如下: 包含所有文件的整个项目将填充错误。但在Intellij中,同样的项目不存在这样的问题。我错过了Eclipse的哪一个设置? gradle build工作很好,尽管

  • 我有一本书的多份副本。不同用户评论的pdf文档。我想把所有这些评论合并成一个新的pdf“合并”。 我在一个名为“路径”和“目录”属性的文档类中编写了这个子类。 这段代码导致了一个我无法解决的异常。 iText。内核PDFEException:“Pdf间接对象属于其他Pdf文档。将对象复制到当前pdf文档。' 要执行此任务,我需要更改什么?还是我完全摆脱了我的代码块?

  • 我想这样打印: 谢谢你们的回答伙计们 HTML: