如何编写PDF的编辑内容

时间:2013-06-15 07:01:29

标签: c# .net

这里我删除了超链接。 但是当我再次删除超链接时,我正在编写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(); 
} 
} 
}

1 个答案:

答案 0 :(得分: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);

                  }
            }
        }