0

在这里,我删除了超链接。但是在删除超链接后,当我再次编写 PDF 的内容时,会插入带有名称的 pdf 文件的路径,而不是之前的链接...

这是pdf文件图片链接:

i.stack.imgur.com/9eSZ7.jpg

这是我的代码...

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(); 
} 
} 
}
4

1 回答 1

0

我自己得到了解决方案,我使用了 PDFNet.. 解决方案如下.. 您可以删除链接并在保存文档之前调用该函数...

PDFDoc doc = new PDFDoc(fileIn);
            RemoveCertainExistingLinks(doc, ExcusableLinks);
            doc.Save(fileOut, pdftron.SDF.SDFDoc.SaveOptions.e_linearized);



public void RemoveCertainExistingLinks(PDFDoc doc, ICollection<string> excusedLinks)
        {
            for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
            {
                Page p = itr.Current();

                int numAnnots = p.GetNumAnnots();

                // Loops over the annotations backwards because the document is modified in
                // place.
                int i = numAnnots; 
                while (i != 0)
                {                    
                    i--;

                    Annot annot = p.GetAnnot(i);

                    if (annot.GetType() != Annot.Type.e_Link || !annot.IsValid())
                    {
                        continue;
                    }


                    pdftron.PDF.Action linkAction = annot.GetLinkAction();
                    if (linkAction.GetType() != pdftron.PDF.Action.Type.e_URI)
                    {
                        continue;
                    }


                    pdftron.SDF.Obj sdfobj = linkAction.GetSDFObj();

                    // this should be a dictionary
                     pdftron.SDF.Obj URIobj = sdfobj.FindObj("URI");
                     string URI = URIobj.GetAsPDFText();                   


                    p.AnnotRemove(i);

                  }
            }
        }
于 2013-06-29T06:51:19.220 回答