Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c# - Reading hyperlinks from pdf file

I'm trying to read a pdf file and get all hyperlinks from this file. I'm using iTextSharp for C# .net.

PdfReader reader = new PdfReader("test.pdf");           
List<PdfAnnotation.PdfImportedLink> list = reader.GetLinks(36); 

This method "GetLinks" return a list with a lot of information about the links, but this method does not return the value that I want, the hyperlink string and I exactly know that there are hyperlinks in 36th page

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

PdfReader.GetLinks() is only meant to be used with links internal to the document, not external hyperlinks. Why? I don't know.

The code below is based off of code I wrote earlier but I've limited it to links stored in the PDF as a PdfName.URI. Its possible to store the link as Javascript that ultimately does the same thing and there's probably other types but you'll need to detect for that. I don't believe there's anything in the spec that says that a link actually needs to be a URI, its just implied, so the code below returns a string that you can (probably) convert to a URI on your own.

    private static List<string> GetPdfLinks(string file, int page)
    {
        //Open our reader
        PdfReader R = new PdfReader(file);

        //Get the current page
        PdfDictionary PageDictionary = R.GetPageN(page);

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

        List<string> Ret = new List<string>();

        //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());
            }
        }

        return Ret;

    }

And call it:

        string myfile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
        List<string> Links = GetPdfLinks(myfile, 1);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...