我们需要更改 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);
}
}
}
}
森林海
相关分类