PdfReader.GetLinks()
предназначен только для использования с внутренними ссылками на документ, а не с внешними гиперссылками.Зачем?Я не знаю.
Приведенный ниже код основан на коде, который я написал ранее , но я ограничил его ссылками, хранящимися в PDF как PdfName.URI
.Можно сохранить ссылку как Javascript, который в конечном итоге делает то же самое, и, вероятно, есть другие типы, но вам нужно будет это обнаружить.Я не верю, что в спецификации есть что-то, что говорит, что ссылка на самом деле должна быть URI, это просто подразумевается, поэтому код ниже возвращает строку, которую вы можете (вероятно) преобразовать в URI самостоятельно.
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;
}
И назовите это:
string myfile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
List<string> Links = GetPdfLinks(myfile, 1);