14

My group has been using the itext-sharp library and C#/.NET to generate custom, dynamic PDFs. For the most part, this process is working great for our needs. The one problem we can run into during development/testing is layout issues which can cause the PDF to not open/render correctly in Adobe Reader, esp. the newer versions of Acrobat/Reader.

The document will open the display correctly for the first X pages. But if there is an error, the remaining pages in the document will not display.

As mentioned, we are usually able to track this problem down to a layout-type issue with our C#/iText code. We eventually find the error by using the guess and check method, or divide and conquer. It works, but it doesn't feel like the best way to solve these problems.

I was wondering if there are any tools available that could speed up the process of validating a PDF document and could help to point out errors in the document?

4

2 回答 2

19

“最便宜”(同时也非常可靠!)的方法是使用 Ghostscript。让 Ghostscript 解释 PDF 并查看它给出的返回值。如果没有问题,PDF文件应该没问题。在 Windows 上:

 gswin32c.exe ^
       -o nul
       -sDEVICE=nullpage ^
        d:/path/to/file.pdf

nullpage输出设备不会创建任何新文件。但是如果遇到错误,Ghostscript 会在 stdout/stderr 上告知。检查%errorlevel%伪环境变量的内容。-- 在 Linux 上:

 gs \
       -o /dev/null \
       -sDEVICE=nullpage \
        /path/to/file.pdf

(检查返回值以echo $?获取0“没有问题”的值。)

如果出现错误,Ghostscript 会发布一些可能对您有帮助的信息。无论如何,至少您可以肯定地识别那些确实没有问题的文件:如果Ghostscript可以处理它们,Acrobat (Reader) 也可以毫无问题地渲染它们。

于 2010-09-06T23:42:57.863 回答
13

验证 PDF 文件可能是一项相当棘手的任务——主要是因为正确执行此操作所需的工具非常昂贵。

Acrobat 有一个工具(高级 > 预检 > PDF 分析 > 报告 PDF 语法问题),可让您扫描 PDF 以查找任何语法问题,但无法以编程方式访问该工具。

Appligent 有一个名为pdfHarmmony的工具,它由 Adob​​e 的 PDF 库提供支持,可以通过编程方式访问,但非常昂贵(2500 美元以上)。如果您负担得起,此选项将为您提供最佳结果。

还有另一种选择是3-Heights PDF Analysis & Repair,我不知道它的质量如何,但同样昂贵。

您可能会对 SourceForge 上的这个PDF Validator 工具感兴趣,但是,它只分析文档结构而不是内容本身,因此不会拾取损坏的图像或内容流。

不幸的是,由于详细分析 PDF 文件的难度,实际上并没有任何免费工具可以正确执行此操作,但我认为检查文档结构的工具总比没有好。

于 2010-09-03T07:36:57.313 回答