我从http://google.com
之类的PDF文件中获取了链接值但是我需要使用锚文本值,例如click here
。如何获取锚链接值文本?
我已通过使用以下URL来获取PDF文件的URL值:Reading hyperlinks from pdf file例如。
Anchor a = new Anchor("Test Anchor");
a.Reference = "http://www.google.com";
myParagraph.Add(a);
在这里,我得到了http://www.google.com
,但我需要获得锚点值,即Test Anchor
需要您的建议。
从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();
}
}
}
}
[不幸的是,我认为您将无法做到这一点,至少在没有大量猜测的情况下。在HTML中,这很容易,因为超链接及其文本存储为:
<a href="http://www.example.com/">Click here</a>
但是,在PDF中,这两个实体不以任何形式的关系存储。从技术上讲,我们认为PDF中的“超链接”是恰好位于文本顶部的PDF注释。您可以通过在诸如Adobe Acrobat Pro之类的编辑程序中打开PDF来查看此内容。您可以更改文本,但“可点击”区域不会更改。您还可以移动“可点击”区域并调整其大小,并将其放置在文档中的任何位置。
创建PDF时,iText / iTextSharp会将其抽象出来,因此您不必考虑这一点。您可以创建带有可单击文本的“超链接”,但是当它生成PDF时,最终将把文本创建为普通文本,计算矩形坐标,然后在该矩形上添加注释。
我确实说过,您可以尝试对此进行猜测,但它可能对您不起作用。为此,您需要获取用于注释的矩形,然后找到也在这些坐标处的文本。但是,由于存在填充问题,因此不会完全匹配。如果您绝对必须在超链接下获取文本,那么这是我所知道的唯一方法。祝你好运!