Free Support Forum - aspose.com

Remove hyperlinks from pdf files


#1

I have a need to remove hyperlinks from documents as we convert them to PDF. This is to prevent a vulnerability that someone could embed a hyperlink to a malicious website. Is there a way to accomplish this with Aspose?

I found this 2014 support thread with someone else asking for this feature. Has it been implemented? To remove hyperlink from pdf


#2

@blbaumann

Thank you for contacting support.

Would you please share a sample PDF document so that we may investigate further and assist you accordingly.


#3

Test embedded hyperlink.docx.pdf (8.9 KB)

This was a word doc that i added a link to google to. Went through aspose and still has a clickable link. Result I’d like to see is all hyperlinks stripped from the generated PDF files. Use case is that I have documents coming in from various sources that cannot be trusted. I’m converting them to PDF and don’t want to run the risk that the resulting document is linking to a dangerous place and the client reading the PDF could click on the hyperlink.


#4

@blbaumann

Thank you for sharing requested data.

Please try using below code snippet to remove hyperlinks from a PDF document, and then share your kind feedback with us.

// load the PDF file
Document doc = new Document(dataDir + "Test embedded hyperlink.docx.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("");
int pageNum = 1;
//accept the absorber for first page
Page page = doc.Pages[pageNum];
page.Accept(textFragmentAbsorber);
//get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
//loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
        LinkAnnotation linkAnnot = new LinkAnnotation(page, textFragment.Rectangle);
        linkAnnot.Border = (new Border(linkAnnot));
        linkAnnot.Border.Width = (0);
        page.Annotations.Add(linkAnnot);
}
doc.Save(dataDir + "HyperlinkRemoved_output_19.5.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.