如何从 pdf 文件 C# 中使用 AnchorText 读取超链接

Anb*_*van 1 c# itext

我已经从 PDF 文件中获取了链接值,http://google.com 但我需要获取锚文本值,例如click here. 如何取锚链接值文本?

我使用以下 URL 获取了 PDF 文件的 URL 值: 例如,从 pdf 文件中读取超链接

Anchor a = new Anchor("Test Anchor");
a.Reference = "http://www.google.com";
myParagraph.Add(a);
Run Code Online (Sandbox Code Playgroud)

在这里我得到了http://www.google.com但我需要得到锚值,即Test Anchor

需要你的建议。

小智 5

您需要从 PDF 文件中确定放置链接的区域,然后使用 iTextSharp 阅读链接下方的文本。

通过这种方式,您可以提取链接下方的文本。这种方法的局限性在于,如果链接区域比文本宽,则提取将读取该区域下的全文。

private void GetAllHyperlinksFromPDFDocument(string pdfFilePath)
{
    string linkTextBuilder = "";
    string linkReferenceBuilder = "";

    PdfDictionary PageDictionary = default(PdfDictionary);
    PdfArray Annots = default(PdfArray);
    PdfReader R = new PdfReader(pdfFilePath);

    List<BinaryHyperlink> ret = new List<BinaryHyperlink>();

    //Loop through each page
    for (int i = 1; i <= R.NumberOfPages; i++)
    {
        //Get the current page
        PageDictionary = R.GetPageN(i);

        //Get all of the annotations for the current page
        Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);

        //Make sure we have something
        if ((Annots == null) || (Annots.Length == 0))
            continue;

        //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.GetAsDict(PdfName.A);
            if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI))
            {
                //Get action link URL : linkReferenceBuilder
                PdfString Link = AnnotationAction.GetAsString(PdfName.URI);
                if (Link != null)
                    linkReferenceBuilder = Link.ToString();

                //Get action link text : linkTextBuilder
                var LinkLocation = AnnotationDictionary.GetAsArray(PdfName.RECT);
                List<string> linestringlist = new List<string>();
                iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(((PdfNumber)LinkLocation[0]).FloatValue, ((PdfNumber)LinkLocation[1]).FloatValue, ((PdfNumber)LinkLocation[2]).FloatValue, ((PdfNumber)LinkLocation[3]).FloatValue);
                RenderFilter[] renderFilter = new RenderFilter[1];
                renderFilter[0] = new RegionTextRenderFilter(rect);
                ITextExtractionStrategy textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter);
                linkTextBuilder = PdfTextExtractor.GetTextFromPage(R, i, textExtractionStrategy).Trim();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)