获取使用itextsharp签名的pdf的原始内容 [英] get original content of a pdf signed with itextsharp

查看:549
本文介绍了获取使用itextsharp签名的pdf的原始内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取已签名PDF的原始文档,以便将其哈希值与存储的文档进行比较。

I'm trying to get the original document of a signed PDF in order to compare it's hash with an stored doc.

当文档包含时,这非常简单几个签名,使用acrobat阅读器,你可以去文档的上一版本保存它就是它。

This is really easy when the document has several signatures, with acrobat reader you can go the previous revision of the document save it and that's it.

令人惊讶的是,这不适用于第一个签名,其中没有直接获取原始数据的方法。

Surprisingly this does not work with the first signature, where there is no straight forward way to get the original data.

由于无法用读取器执行此操作,我已尝试使用iTextSharp进行编程。然而,尽管我已经深入搜索,但我还没有找到如何做到这一点。我发现的唯一相关帖子是一个但没有提供解决方案。

As it is not possible to do it with the reader I have tried programatically with iTextSharp. However although I have googled deeply I have not found how to do it. The only relevant post I found is this one but no solution is offered.

有没有人遇到过这个问题并找到了解决方案?

Has anyone faced this problem and found a solution?

提前致谢。

编辑:我在这里提供了根据mkl的响应提取数据的代码。阅读响应的注释,注意未签名PDF的未固定长度的问题。

I put here the code that extracts the data based on the response of mkl. Read the comments of the response to beware of the problem with the unfixed length of the non signed PDFs.

String sOriginalText = File.ReadAllText("FileSigned.pdf", Encoding.Default);
int sTrailerNumberPosition = sOriginalText.LastIndexOf("]/Prev ") + "]/Prev ".Length;
int sTrailerNumberEndPosition = sOriginalText.IndexOf(">", sTrailerNumberPosition);
String sTrailerIndex = sOriginalText.Substring(sTrailerNumberPosition, sTrailerNumberEndPosition -sTrailerNumberPosition);
int iTrailerIndexPosition = sOriginalText.IndexOf(sTrailerIndex + "\r\n%%EOF");
int iEndPosition = sOriginalText.IndexOf("%%EOF", iTrailerIndexPosition) + "%%EOF".Length;
String sOutText = sOriginalText.Substring(0, iEndPosition);
File.WriteAllText("c:/OriginalFile.pdf", sOutText, Encoding.Default);


推荐答案

您的任务是否获取签名PDF的原始文件是可以实现的,取决于签名最初的应用方式。

Whether or not your task to get the original document of a signed PDF is realizable at all, depends on how the signature originally was applied.


  1. 如果签名以追加模式应用(即根据PDF规范的语言 ISO 32000-1:2008 作为增量更新,参见第7.5.6节),你只需要切断这个附加的增量更新版本。

  1. If the signature was applied in append mode (i.e. according to the language of the PDF specification ISO 32000-1:2008 as an incremental update, cf. section 7.5.6), you merely have to cut off this appended, incremental update revision.

由于您有一个存储的文档,大概在签名后成为您检查的文档,您可以简单地剪切签名文件的长度。存储一个和比较,例如使用哈希。这足以表明签名文档源自您的原始文档。但是,可能还有其他中间修订,因为您可能已经切断了多个增量更新。

As you have a stored document which presumably after signing has become the document you inspect, you can simply cut the signed file at the length of the stored one and the compare, e.g. using hashes. This suffices to show that the signed document is derived from your original one. There may have been other, intermediary revisions, though, as you might just have cut off multiple incremental updates.

一般情况下,您可以通过找到先前的修订版本> /上一页将已签名的PDF的预告片段输入到先前修订版的交叉参考表,然后从那里继续前进到文档结束标记 %% EOF ,因为在增量更新中

In general you can find the prior revision by following the /Prev trailer entry of your signed PDF to the cross reference table of the prior revision and from there move onwards to the document end marker %%EOF because in an incremental update


添加的预告片应包含除前一个预告片中的Prev条目(如果有)以外的所有条目,无论是否修改。此外,添加的预告片词典应包含一个Prev条目,给出前一个交叉引用部分的位置(参见表15)。每个预告片应以其自己的文件结尾(%% EOF)标记终止。

the added trailer shall contain all the entries except the Prev entry (if present) from the previous trailer, whether modified or not. In addition, the added trailer dictionary shall contain a Prev entry giving the location of the previous cross-reference section (see Table 15). Each trailer shall be terminated by its own end-of-file (%%EOF) marker.

如果PDF使用交叉引用流而不是交叉引用表,交叉引用流字典中有类似的条目:

In case of PDFs using cross reference streams instead of cross reference tables, there is the analogous entry in the cross-reference stream dictionary:


(仅当文件有更多时才存在比一个交叉引用流;在混合引用文件中没有意义;请参见7.5.8.4,与不支持压缩引用流的应用程序的兼容性)从文件开头到开头的解码流中的字节偏移量上一个交叉引用流。此条目与预告片词典中的Prev条目具有相同的功能(表15)。

(Present only if the file has more than one cross-reference stream; not meaningful in hybrid-reference files; see 7.5.8.4, "Compatibility with Applications That Do Not Support Compressed Reference Streams") The byte offset in the decoded stream from the beginning of the file to the beginning of the previous cross-reference stream. This entry has the same function as the Prev entry in the trailer dictionary (Table 15).

您应该知道,附加的增量更新修订版除了签名之外还可以包含其他更改。因此,即使先前版本与您存储的文档相对应,您仍然只知道签名文档基于您保存的文档。

You should be aware, though, that the appended, incremental update revision can contain other changes in addition to the signature. Thus, even if the previous revision corresponds with your stored document, you still only know that the signed document is based on your saved one.

如果追加模式中的签名,则运气不佳:程序操作PDF(例如用于签名)可能会完全重新排列文档的二进制内容,甚至可能重新编号对象,更改压缩,删除未使用的对象等,而文档的外观保持不变。

If the signature was not applied in append mode, you are out of luck: Programs manipulating PDFs (e.g. for signing) might completely rearrange the binary contents of your document, possibly even renumbering objects, changing compression, removing unused objects, etc., while the appearance of the document remains the same.

这篇关于获取使用itextsharp签名的pdf的原始内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆