ほぼ同じ 2 つのファイルの内容を比較し、対応する pdf ファイルの異なる部分を強調表示する必要があります。pdfboxを使用しています。少なくともロジックで私を助けてください。
4 に答える
GUI を備えたツールを好む場合は、これを試すことができます: diffpdf。これはMark Summerfieldによるもので、Qt で書かれているため、Qt が動作するすべてのプラットフォームで利用できる (またはビルドできる) はずです。
スクリーンショットは次のとおりです。
Linuxのシェルスクリプトでも同じことができます。スクリプトは3つのコンポーネントをラップします。
- ImageMagickの
compare
コマンド pdftk
ユーティリティ_- Ghostscript
.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自体をネイティブに実行できないため、 「デリゲート」としてGhostscriptが必要です。
もう一度、pdftk
pdftk
これで、「差分」PDFページを次のように連結できます。
pdftk \
somewhereelse/diff_page_*.pdf \
cat \
output somewhereelse/diff_allpages.pdf
Ghostscript
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 を使用して jar を作成し、pdf ファイルを比較しました。これ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);