java - 使用 itext 4 编辑 pdf 中的现有超链接

标签 java pdf hyperlink itext

我们需要更改 pdf 中的超链接。有很多以p-开头的超链接,我们需要删除p-。

到目前为止我们能够读取超链接。 有人知道如何编辑它们并保存 pdf 吗?

谢谢

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

最佳答案

首先,您必须将新值设置为 annotationAction 字典中的 URI 键:

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

之后,您必须通过应用 PdfStamper 来保存更改,而无需执行进一步操作:

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

关于java - 使用 itext 4 编辑 pdf 中的现有超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57671367/

相关文章:

java - 如何使用 Selenium 在可点击下一个按钮的情况下逐一解析网页?

java - Ant : moving a file into a target directory

java - 将 Tiff 转换为 PDF : PDF is corrupted

css - 在页面上时突出显示页面链接 - a :active?

html - 将悬停颜色更改添加到备用链接颜色

java - Gradle-与外部项目的多项目?

java - 在XStream中使用JavaBeanConverter时如何使用@Transient注解?

node.js - 如何将页眉和页脚内容添加到 pdfkit for node.js

Java 如何显示手册

javascript - 如何使用 jquery 在文本中查找链接?