简体   繁体   中英

Editing existing hyperlinks in pdf with itext 4

We need to change the hyperlinks in a pdf. There are many hyperlinks which start with p- and we need to remove the p-.

So far we are able to read the hyperlinks. Anyone knows how to edit them and save the pdf?

Thank you

public static void main(final String[] args) {
    final List<byte[]> pdfs = new ArrayList<>();
    final List<String> testfiles = Arrays.asList("72076.5.1.pdf");
    final String dir = "C:\\Entwicklung\\testaround\\pdf\\";
    for (final String name : testfiles) {
        final File file = new File(dir + name);
        final Path path = Paths.get(file.toURI());
        try (InputStream istream = new FileInputStream(path.toFile())) {
            final byte[] data = ByteStreams.toByteArray(istream);
            pdfs.add(data);
            final PdfReader reader = new PdfReader(data);
            final PdfDictionary pageDictionary = reader.getPageN(36);
            final PdfArray annotations = pageDictionary.getAsArray(PdfName.ANNOTS);
            if (annotations != null && annotations.size() != 0) {
                for (int i = 0; annotations.size() > i; i++) {
                    final PdfObject annot = annotations.getDirectObject(i);
                    final PdfDictionary annotationDictionary = (PdfDictionary)PdfReader.getPdfObject(annot);
                    if (annotationDictionary.get(PdfName.SUBTYPE).equals(PdfName.LINK)) {
                        final PdfDictionary annotationAction = annotationDictionary.getAsDict(PdfName.A);
                        if (annotationAction.get(PdfName.S).equals(PdfName.URI)) {
                            final PdfString destination = annotationAction.getAsString(PdfName.URI);
                            final String url = destination.toString();
                            System.out.println(url);
                        }
                    }
                }
            }
            final int n = reader.getNumberOfPages();
            System.out.println("Pages : " + n);
            Files.write(Paths.get(dir + "new.pdf"), data);
        } catch (final FileNotFoundException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

First of all you have to set the new value to the URI key in the annotationAction dictionary:

if (annotationAction.get(PdfName.S).equals(PdfName.URI)) {
    final PdfString destination = annotationAction.getAsString(PdfName.URI);
    final String url = destination.toString();
    System.out.println(url);
    String updatedUrl = [... url changed to match the changed criteria ...];
    annotationAction.put(PdfName.URI, new PdfString(updatedUrl));
}

After that you have to save the changes by applying a PdfStamper without further actions:

final byte[] data = ByteStreams.toByteArray(istream);
pdfs.add(data);
final PdfReader reader = new PdfReader(data);
final PdfDictionary pageDictionary = reader.getPageN(36);
final PdfArray annotations = pageDictionary.getAsArray(PdfName.ANNOTS);
if (annotations != null && annotations.size() != 0) {
    [...]
}
new PdfStamper(reader, new FileOutputStream(dir + name + "-new.pdf")).close();

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