万一有人关心,我可以在上面的第一条评论中使用 Chris Haas 链接的代码,但对其进行了如下修改:
foreach (FileInfo file in files)
{
PdfReader reader = default(PdfReader);
bool linkReplaced = false;
//Setup some variables to be used later
reader = new PdfReader(file.FullName);
int pageCount = reader.NumberOfPages;
PdfDictionary pageDictionary = default(PdfDictionary);
PdfArray annots = default(PdfArray);
//Loop through each page
for (int i = 1; i <= pageCount; i++)
{
//Get the current page
pageDictionary = reader.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;
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;
string fValue = string.Empty;
string ufValue = string.Empty;
string uriValue = string.Empty;
PdfObject a = AnnotationDictionary.Get(PdfName.A);
if (a.IsDictionary())
{
//Get the ACTION for the current annotation
PdfDictionary AnnotationAction = (PdfDictionary)a;
//Test if it is a URI action
if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI))
{
uriValue = AnnotationAction.Get(PdfName.URI).ToString();
if ((uriValue.IndexOf(findValue, StringComparison.OrdinalIgnoreCase) > -1))
{
string uriValueReplace = Replace(uriValue, findValue, replaceValue, StringComparison.OrdinalIgnoreCase);
//Change the URI to something else
AnnotationAction.Put(PdfName.URI, new PdfString(uriValueReplace));
linkReplaced = true;
}
}
}
else if (a.IsIndirect())
{
// Get the indirect reference
PdfIndirectReference indirectRef = (PdfIndirectReference)a;
// Get the GoToR type object which is at the document level
PdfDictionary goToR = (PdfDictionary)reader.GetPdfObject(indirectRef.Number);
// Get the FileSpec object whic his at the document lelvel
PdfObject f = goToR.Get(PdfName.F);
if (f == null || !f.IsIndirect())
continue;
PdfObject fileSpecObject = reader.GetPdfObject(((PdfIndirectReference)goToR.Get(PdfName.F)).Number);
if (!fileSpecObject.IsDictionary())
continue;
PdfDictionary fileSpec = (PdfDictionary)fileSpecObject;
fValue = fileSpec.Get(PdfName.F).ToString();
ufValue = fileSpec.Get(PdfName.UF).ToString();
if ((fValue.IndexOf(findValue, StringComparison.OrdinalIgnoreCase) > -1) || (ufValue.IndexOf(findValue, StringComparison.OrdinalIgnoreCase) > -1))
{
string fValueReplace = Replace(fValue, findValue, replaceValue, StringComparison.OrdinalIgnoreCase);// fValue.Replace(findValue, replaceValue);
string ufValueReplace = Replace(fValue, findValue, replaceValue, StringComparison.OrdinalIgnoreCase);// ufValue.Replace(findValue, replaceValue);
// Update the references to the file
fileSpec.Put(PdfName.F, new PdfString(fValueReplace));
fileSpec.Put(PdfName.UF, new PdfString(ufValueReplace));
linkReplaced = true;
}
}
}
}
}