在这里,我删除了超链接。但是在删除超链接后,当我再次编写 PDF 的内容时,会插入带有名称的 pdf 文件的路径,而不是之前的链接...
这是pdf文件图片链接:
这是我的代码...
PdfDictionary PageDictionary = default(PdfDictionary);
PdfArray Annots = default(PdfArray);
PdfReader reader = new PdfReader(pdfFilePath);
//Loop through each page
for (int i = 1; i <= reader.NumberOfPages; 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; }
//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))
{
//Removing Link
AnnotationAction.Remove(PdfName.URI);
}
}
}
OutputFile = "NewFile.pdf![enter image description here][3]"
using (FileStream FS = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (Document Doc = new Document())
{
using (PdfCopy writer = new PdfCopy(Doc, FS))
{
Doc.Open();
for (int j = 1; j <= reader.NumberOfPages; j++) { writer.AddPage(writer.GetImportedPage(reader, j));
}
Doc.Close();
}
}
}