1
votes

I have a problem, not get extract the Highlighted Text from PDF File. The str variable is always empty. Anybody can help me?

My code:

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; }

I'm using ITextSharp library. PFLibrary is iTextSharp.text.pdf namespace.

I want sweep all pages from pdf and extract all Highlighted Texts, It is 245 pages but i will put filter per page. I can identify the highlight annotations however not returned a string with the text highlighted

1

1 Answers

0
votes

I managed to solve my problem with the following code:

    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;

    }