【问题标题】:Comparison of two pdf files两个pdf文件的比较
【发布时间】:2011-10-05 23:37:17
【问题描述】:

我需要比较两个几乎相似的文件的内容,并在相应的 pdf 文件中突出显示不同的部分。我正在使用pdfbox。请至少帮助我理解逻辑。

【问题讨论】:

    标签: pdf comparison pdfbox


    【解决方案1】:

    您可以在 Linux 上使用 shell 脚本做同样的事情。该脚本包含 3 个组件:

    1. ImageMagick 的compare 命令
    2. pdftk 实用程序
    3. 鬼脚本

    将其转换为用于 DOS/Windows 的 .bat 批处理文件相当容易......

    这里是构建块:

    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 作为 'delegate',因为它本身不能这样做。

    再一次,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
    

    【讨论】:

    • 这是一个使用 ImageMagick 和 Poppler 工具(为了提高速度)逐页直观地比较两个 PDF 的脚本:gist.github.com/brechtm/891de9f72516c1b2cbc1。它为pdfdiff 目录中的 PDF 的每一页输出一个 JPG,并另外打印两个 PDF 之间不同的页数。
    【解决方案2】:

    如果您更喜欢带有 GUI 的工具,您可以试试这个:diffpdf。它是由Mark Summerfield 编写的,由于它是用 Qt 编写的,因此它应该在所有运行 Qt 的平台上都可用(或应该是可构建的)。

    截图如下:

    【讨论】:

    • 是否有机会在 CLI 上使用它,跳过 GUI 并将输出直接重定向到文件?
    • @caw: (1) 你看到my other answer了吗? -- (2) AFAIK,较新版本的 DiffPDF 可以将输出重定向到 CSV 文件。不过,我不知道这是否完全跳过了 GUI。 -- (3) 有一个“纯 CLI”版本的 DiffPDF 可用,称为 DiffPDFc,可在 www.qtrac.eu 找到——但是,它仅适用于 Windows。
    • 我没有,但之前尝试过 ImageMagick、pdftk 和 Ghostscript。不是那种组合,而是分开。由于diffpdf 的结果非常好,实际上非常好,我希望所有这些已经存在的功能都可以用于在 CLI 上重定向到 PDF。真可惜!也感谢有关该工具其他版本的信息。不幸的是,较新的版本不再是开源的,而且仅 Windows 也不是完美的。
    【解决方案3】:

    我想出了一个使用 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);
    

    【讨论】:

    • 尝试下载该文件时出现错误“传输的文件包含病毒,因此被阻止。URL:testautomationguru.com/download/304 媒体类型:application/java-vm 病毒名称:McAfeeGW:BehavesLike。 Java.Suspicious.xm"
    • 我尝试从上述站点运行 jar 文件,但我收到类似“没有主要清单属性,在 taguru-pdf-util.jar 中”的错误,请你帮我解决这个问题
    【解决方案4】:

    我自己也遇到了这个问题,我发现最快的方法是使用 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
    

    【讨论】:

    • 我还需要安装 ghostscript
    猜你喜欢
    • 1970-01-01
    • 2010-11-02
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多