gpt4 book ai didi

java - 批量重命名PDF文件中的PDF书签/大纲

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:21 30 4
gpt4 key购买 nike

我需要做与这个 Stack Overflow 问题几乎相同的事情:Renaming named destinations in PDF files但我的 PDF 充满了书签,而不是包含指定目的地的文本本身。当我运行Bruno's code时,我的名称对象是空的——尽管(正确读入的)PDF 中有 200 多个书签——并且 Java 抛出 NullPointerException。有什么想法吗?

Exception in thread "main" java.lang.NullPointerException
at annotations.RenameDestinations.manipulatePdf(RenameDestinations.java:41)
at annotations.RenameDestinations.main(RenameDestinations.java:33)

免责声明:我是 iText 新手。

最佳答案

我成功了。如果其他人需要重命名许多 PDF 文件中的 PDF 书签(批量书签重命名),您可以使用下面的脚本避免支付自动书签费用。我将是第一个承认它可以改进的人,可能是大幅改进(尤其是通过重构),但它确实有效!我使用的是 Windows 7 64 位和带有 UTF-8 编码字形的文件。它假设您将创建一个 c:\in 和 c:\out,其中“in”文件夹包含您的原始版本,“out”文件夹包含重命名的书签版本。该程序不会更改您的原始 PDF。观察控制台输出是否有 itext 无法处理的任何 PDF。就我而言,我有一些零字节 PDF、 protected PDF,甚至是无法处理的损坏的 PDF。

package annotations;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.SimpleBookmark;

public class RenameDestinations {
public static void main(String[] args) throws IOException, DocumentException {
String in = "c:/in"; // this must be a directory
String out = "c:/out"; // this must be a directory

// Parent Bookmarks
String find1 = "_\n <"; // e.g., Something_ <Title ...> child bookmark</Title>
String repl1 = "X\n <"; // e.g., SomethingX <Title ...> child bookmark</Title>

// No children
String find2 = "_</Title>"; // e.g., <Title>Something_</Title>
String repl2 = "X</Title>"; // e.g., <Title>SomethingX</Title>

// read a directory of files
// http://stackoverflow.com/questions/13918876/java-open-directory
File directory = new File(in);
File[] contents = directory.listFiles();
for (File f : contents) {
// get extension
String filename = f.getName();
String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length()).toLowerCase();

// leave if not a PDF
if (!extension.equals("pdf")) {
System.out.println(filename);
System.out.println(" NOT PDF, SKIPPED");
continue;
}

// inform user
System.out.println(filename);

String src = f.getAbsolutePath();
String dst = out + "/" + f.getName();
String srcx = in + "/bookmarks.xml"; // save this book's bookmarks

// read and create an xml file of the bookmarks
PdfReader reader = new PdfReader(src);
List<HashMap<String, Object>> list = SimpleBookmark.getBookmark(reader);

// Create a stamper
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dst));

//leave if no bookmarks
if (list == null) {
System.out.println(filename);
System.out.println(" NO BOOKMARKS, THUS NO CHANGES MADE");
stamper.setOutlines(list);
}else {
// create bookmarks xml
SimpleBookmark.exportToXML(list, new FileOutputStream(srcx), "UTF-8", true);

// find and replace bookmark titles
// http://stackoverflow.com/questions/3935791/find-and-replace-words-lines-in-a-file
Path path = Paths.get(srcx);
Charset charset = StandardCharsets.UTF_8;
// store
String content = new String(Files.readAllBytes(path), charset);
// replace
content = content.replaceAll(find1, repl1);
content = content.replaceAll(find2, repl2);
// write
Files.write(path, content.getBytes(charset));

// read
// https://code.google.com/p/pdf-pub-tools/source/browse/trunk/pdf-pub-tools/src/java/net/mitnet/tools/pdf/book/pdf/util/PdfBookmarkBuilder.java?spec=svn133&r=133
List<HashMap<String, Object>> bookmarks = SimpleBookmark.importFromXML(new FileReader(srcx));
stamper.setOutlines(bookmarks);

//inform user
System.out.println(" BOOKMARKS RENAMED!");
}

// Close the stamper and reader
stamper.close();
reader.close();

}
//inform user
System.out.println("DONE!");
}

}

关于java - 批量重命名PDF文件中的PDF书签/大纲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22924428/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com