【发布时间】:2013-05-03 11:35:29
【问题描述】:
我有一个 大型单个 pdf 文档,其中包含 多条记录。每条记录通常占用一页,但有些记录使用 2 页。记录以定义的文本开头,始终相同。
我的目标是将此 pdf 文件拆分为单独的 pdf 文件,并且拆分应始终在找到“标题文本”之前进行。
注意:我正在寻找使用 java 或 python 的工具或库。必须在Win 7 上免费提供。
有什么想法吗? AFAIK imagemagick 不会为此工作。可以itext 这样做吗?我从来没有用过,它的
相当复杂,所以需要一些提示。
编辑:
标记的答案使我找到了解决方案。为了完整起见,我的确切实现是:
public void splitByRegex(String filePath, String regex,
String destinationDirectory, boolean removeBlankPages) throws IOException,
DocumentException {
logger.entry(filePath, regex, destinationDirectory);
destinationDirectory = destinationDirectory == null ? "" : destinationDirectory;
PdfReader reader = null;
Document document = null;
PdfCopy copy = null;
Pattern pattern = Pattern.compile(regex);
try {
reader = new PdfReader(filePath);
final String RESULT = destinationDirectory + "/record%d.pdf";
// loop over all the pages in the original PDF
int n = reader.getNumberOfPages();
for (int i = 1; i < n; i++) {
final String text = PdfTextExtractor.getTextFromPage(reader, i);
if (pattern.matcher(text).find()) {
if (document != null && document.isOpen()) {
logger.debug("Match found. Closing previous Document..");
document.close();
}
String fileName = String.format(RESULT, i);
logger.debug("Match found. Creating new Document " + fileName + "...");
document = new Document();
copy = new PdfCopy(document,
new FileOutputStream(fileName));
document.open();
logger.debug("Adding page to Document...");
copy.addPage(copy.getImportedPage(reader, i));
} else if (document != null && document.isOpen()) {
logger.debug("Found Open Document. Adding additonal page to Document...");
if (removeBlankPages && !isBlankPage(reader, i)){
copy.addPage(copy.getImportedPage(reader, i));
}
}
}
logger.exit();
} finally {
if (document != null && document.isOpen()) {
document.close();
}
if (reader != null) {
reader.close();
}
}
}
private boolean isBlankPage(PdfReader reader, int pageNumber)
throws IOException {
// see http://itext-general.2136553.n4.nabble.com/Detecting-blank-pages-td2144877.html
PdfDictionary pageDict = reader.getPageN(pageNumber);
// We need to examine the resource dictionary for /Font or
// /XObject keys. If either are present, they're almost
// certainly actually used on the page -> not blank.
PdfDictionary resDict = (PdfDictionary) pageDict.get(PdfName.RESOURCES);
if (resDict != null) {
return resDict.get(PdfName.FONT) == null
&& resDict.get(PdfName.XOBJECT) == null;
} else {
return true;
}
}
【问题讨论】:
-
iText 如果可以正确解析 PDF 中的文本,则可以执行您想要的操作。你说,图书馆一定是免费的。 iText 作为免费软件要求您遵守 AGPL 许可。
-
这是一次性的,所以 AGPl 不是问题。文本是可选的(例如文本),因此应该可以工作。但是,如果您知道的话,我对代码示例很感兴趣。
-
目前我只能通过智能手机上网。稍后我会尝试找到一些示例代码。