如何删除PDF文件中的超链接 [英] How to remove hyperlinks in PDF Files

查看:95
本文介绍了如何删除PDF文件中的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB.net(iTextSharp库)阅读PDF文件,如何删除HyperLinks并编写已编辑的pdf文件???



这是一段代码,我正在阅读pdf文件..............

I'm reading a PDF file using VB.net(iTextSharp library), How to remove HyperLinks and write the edited pdf file???

This is a piece of code where i'm reading pdf file..............

string linkTextBuilder = "";
            string linkReferenceBuilder = "";   
                   
            
            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))
                    {                       
                        Get action link URL : linkReferenceBuilder
                        PdfString Link = AnnotationAction.GetAsString(PdfName.URI);
                       if (Link != null)
                       {
                            linkReferenceBuilder = Link.ToString();                                                        
                        }
}
                }

推荐答案

我自己得到解决方案,解决方案如下:

您可以在保存文档之前删除链接并调用该函数...



I got solution by myself and solution is below..
You can remove the link and call the function before saving document...

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);

                  }
            }
        }


你应该看看从PDF文档(iTextSharp)中删除超链接 [ ^ ]



- Amit


这篇关于如何删除PDF文件中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆