How to remove Links from a PDF Document using PDFBox

What is the best way to remove links from a PDF using PDFBox. Example: Say I have the following as a page in PDF:

Test test1

I want that to be converted to

Test test1

removing the link but preserving the text which is test1 in this case.

Upvotes: 3

Views: 917

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18861

List<PDAnnotation> annotations = page.getAnnotations();
for (PDAnnotation annotation : annotations)
{
    PDAnnotation annot = annotation;
    if (annot instanceof PDAnnotationLink)
    {
        PDAnnotationLink link = (PDAnnotationLink) annot;
        PDAction action = link.getAction();
        if (action instanceof PDActionURI)
        {
            PDActionURI uri = (PDActionURI) action;
            if ("https://stackoverflow.com".equals(uri.getURI()))
            {
                annotations.remove(link);
                break;
            }                                
        }
    }
}
page.setAnnotations(annotations);

Upvotes: 4

Related Questions