【问题标题】:How to copy only hilighted text from pdf using itextsharp library?如何使用 itextsharp 库仅从 pdf 中复制突出显示的文本?
【发布时间】:2016-05-15 21:55:11
【问题描述】:

我有一个问题,无法从 PDF 文件中提取突出显示的文本。 str 变量始终为空。任何人都可以帮助我吗?

我的代码:

private static string GetPdfHighlighText(string file, int page) {
    string nv = "";
    PdfReader reader = new PdfReader(file);
    for (int x = 1; x < reader.NumberOfPages; x++)
    {
        PdfDictionary pageDict = reader.GetPageN(x);
        PdfArray annots = pageDict.GetAsArray(PdfName.ANNOTS);
        if (annots != null)
        {

            for (int i = 1; i <= annots.Size; ++i)
            {
                PdfDictionary annotationDic = (PdfDictionary)PdfReader.GetPdfObject(annots[i]);
                PdfName subType = (PdfName)annotationDic.Get(PdfName.SUBTYPE);
                if (subType.Equals(PdfName.HIGHLIGHT))
                {

                    PdfString str = annots.GetAsString(i);

                    nv = nv + str;

                }
            }
        }
    }

    return nv; }

我正在使用 ITextSharp 库。 PFLibrary 是 iTextSharp.text.pdf 命名空间。

我想从 pdf 中扫描所有页面并提取所有突出显示的文本, 它是 245 页,但我会每页放置过滤器。我可以识别突出显示的注释,但没有返回突出显示文本的字符串

【问题讨论】:

    标签: c# pdf itextsharp


    【解决方案1】:

    我设法用以下代码解决了我的问题:

        public string GetPdfLinks(string file,  int pgIni, int pgFim)
        {
            Progresso = 0;
            //Open our reader
            PdfReader R = new PdfReader(file);
            List<string> Ret = new List<string>();
    
            for (int i = pgIni; i <= pgFim; i++)
            {
    
                //Get the current page
                PdfDictionary PageDictionary = R.GetPageN(i);
    
                //Get all of the annotations for the current page
                PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);
    
                //Make sure we have something
                if ((Annots == null) || (Annots.Length == 0))
                    return null;
    
                 //kjkjjj
    
                //Loop through each annotation
                foreach (PdfObject A in Annots.ArrayList)
                {
                    //Convert the itext-specific object as a generic PDF object
                    PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A);
    
                    //Make sure this annotation has a link
                    if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
                        continue;
    
                    //Make sure this annotation has an ACTION
                    if (AnnotationDictionary.Get(PdfName.A) == null)
                        continue;
    
                    //Get the ACTION for the current annotation
                    PdfDictionary AnnotationAction = (PdfDictionary)AnnotationDictionary.Get(PdfName.A);
    
                    //Test if it is a URI action (There are tons of other types of actions, some of which might mimic URI, such as JavaScript, but those need to be handled seperately)
                    if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI))
                    {
                        PdfString Destination = AnnotationAction.GetAsString(PdfName.URI);
                        if (Destination != null)
                            Ret.Add(Destination.ToString());
                    }
                }
    
                Progresso++;
            }
    
            foreach (string link in Ret)
            {
                resultado = resultado + link + "\n ";
            }
    
            return resultado;
    
        }
    

    【讨论】:

      猜你喜欢
      • 2014-12-26
      • 2014-06-14
      • 2011-09-25
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2019-11-13
      • 2016-05-01
      • 2020-04-16
      相关资源
      最近更新 更多