我需要比较两个几乎相似的文件的内容,并突出显示相应 pdf 文件中的不同部分。我正在使用pdfbox。请至少帮助我理解逻辑。
4 回答
如果你更喜欢带有 GUI 的工具,你可以试试这个:diffpdf。它是由Mark Summerfield编写的,由于它是用 Qt 编写的,因此它应该在所有运行 Qt 的平台上都可用(或应该是可构建的)。
这是一个屏幕截图:
你可以在 Linux 上使用 shell 脚本来做同样的事情。该脚本包含 3 个组件:
- ImageMagick 的
compare
命令 - 实用
pdftk
程序 - 鬼脚本
.bat
将其转换为 DOS/Windows的批处理文件相当容易...
以下是构建块:
pdftk
使用此命令将多页 PDF 文件拆分为多个单页 PDF:
pdftk first.pdf burst output somewhere/firstpdf_page_%03d.pdf
pdftk 2nd.pdf burst output somewhere/2ndpdf_page_%03d.pdf
相比
使用此命令为每个页面创建一个“差异”PDF 页面:
compare \
-verbose \
-debug coder -log "%u %m:%l %e" \
somewhere/firstpdf_page_001.pdf \
somewhere/2ndpdf_page_001.pdf \
-compose src \
somewhereelse/diff_page_001.pdf
请注意,这compare
是 ImageMagick 的一部分。但是对于 PDF 处理,它需要 Ghostscript 作为“代表”,因为它本身不能这样做。
再一次,pdftk
现在您可以再次将您的“差异”PDF 页面与pdftk
:
pdftk \
somewhereelse/diff_page_*.pdf \
cat \
output somewhereelse/diff_allpages.pdf
鬼脚本
Ghostscript 会自动将元数据(例如当前日期+时间)插入其 PDF 输出。因此,这不适用于基于 MD5hash 的文件比较。
如果您想自动发现由纯白页组成的所有案例(这意味着:您的输入页面中没有明显的差异),您还可以使用bmp256
输出设备转换为无元数据位图格式。您可以对原始 PDF(first.pdf 和 2nd.pdf)或 diff-PDF 页面执行此操作:
gs \
-o diff_page_001.bmp \
-r72 \
-g595x842 \
-sDEVICE=bmp256 \
diff_page_001.pdf
md5sum diff_page_001.bmp
只需创建一个带有 MD5sum(供参考)的全白 BMP 页面,如下所示:
gs \
-o reference-white-page.bmp \
-r72 \
-g595x842 \
-sDEVICE=bmp256 \
-c "showpage quit"
md5sum reference-white-page.bmp
我自己也遇到了这个问题,我发现最快的方法是使用 PHP 及其对 ImageMagick (Imagick) 的绑定。
<?php
$im1 = new \Imagick("file1.pdf");
$im2 = new \Imagick("file2.pdf");
$result = $im1->compareImages($im2, \Imagick::METRIC_MEANSQUAREERROR);
if($result[1] > 0.0){
// Files are DIFFERENT
}
else{
// Files are IDENTICAL
}
$im1->destroy();
$im2->destroy();
当然,您需要先安装 ImageMagick 绑定:
sudo apt-get install php5-imagick # Ubuntu/Debian
我想出了一个使用 apache pdfbox 来比较 pdf 文件的 jar - 这可以比较pixel by pixel
和突出差异。
查看我的博客:http ://www.testautomationguru.com/introducing-pdfutil-to-compare-pdf-files-extract-resources/例如并下载。
获取页数
import com.taguru.utility.PDFUtil;
PDFUtil pdfUtil = new PDFUtil();
pdfUtil.getPageCount("c:/sample.pdf"); //returns the page count
以纯文本形式获取页面内容
//returns the pdf content - all pages
pdfUtil.getText("c:/sample.pdf");
// returns the pdf content from page number 2
pdfUtil.getText("c:/sample.pdf",2);
// returns the pdf content from page number 5 to 8
pdfUtil.getText("c:/sample.pdf", 5, 8);
从 PDF 中提取附加图像
//set the path where we need to store the images
pdfUtil.setImageDestinationPath("c:/imgpath");
pdfUtil.extractImages("c:/sample.pdf");
// extracts & saves the pdf content from page number 3
pdfUtil.extractImages("c:/sample.pdf", 3);
// extracts & saves the pdf content from page 2
pdfUtil.extractImages("c:/sample.pdf", 2, 2);
将 PDF 页面存储为图像
//set the path where we need to store the images
pdfUtil.setImageDestinationPath("c:/imgpath");
pdfUtil.savePdfAsImage("c:/sample.pdf");
在文本模式下比较 PDF 文件(更快——但它不比较 PDF 中的格式、图像等)
String file1="c:/files/doc1.pdf";
String file1="c:/files/doc2.pdf";
// compares the pdf documents & returns a boolean
// true if both files have same content. false otherwise.
pdfUtil.comparePdfFilesTextMode(file1, file2);
// compare the 3rd page alone
pdfUtil.comparePdfFilesTextMode(file1, file2, 3, 3);
// compare the pages from 1 to 5
pdfUtil.comparePdfFilesTextMode(file1, file2, 1, 5);
以二进制模式比较 PDF 文件(较慢 - 逐像素比较 PDF 文档 - 突出显示 pdf 差异并将结果存储为图像)
String file1="c:/files/doc1.pdf";
String file1="c:/files/doc2.pdf";
// compares the pdf documents & returns a boolean
// true if both files have same content. false otherwise.
pdfUtil.comparePdfFilesBinaryMode(file1, file2);
// compare the 3rd page alone
pdfUtil.comparePdfFilesBinaryMode(file1, file2, 3, 3);
// compare the pages from 1 to 5
pdfUtil.comparePdfFilesBinaryMode(file1, file2, 1, 5);
//if you need to store the result
pdfUtil.highlightPdfDifference(true);
pdfUtil.setImageDestinationPath("c:/imgpath");
pdfUtil.comparePdfFilesBinaryMode(file1, file2);