简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM