在Default.aspx.cs你做相關的事情,你在你的問題的代碼塊冷落:
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, memoryStream, '\0');
PdfSignatureAppearance pdfSignatureAppearance = pdfStamper.SignatureAppearance;
//...
pdfSignatureAppearance.PreClose(exc);
//...
pdfStamper.Writer.CloseStream = false;
pdfStamper.Close();
每當你叫PdfSignatureAppearance.PreClose,
還必須使用PdfSignatureAppearance.Close,
不PdfStamper.Close,
比照該方法單證:
/**
* This is the first method to be called when using external signatures. The general sequence is:
* preClose(), getDocumentBytes() and close().
* <p>
* If calling preClose() <B>dont't</B> call PdfStamper.close().
* <p>
* <CODE>exclusionSizes</CODE> must contain at least
* the <CODE>PdfName.CONTENTS</CODE> key with the size that it will take in the
* document. Note that due to the hex string coding this size should be
* byte_size*2+2.
* @param exclusionSizes a <CODE>HashMap</CODE> with names and sizes to be excluded in the signature
* calculation. The key is a <CODE>PdfName</CODE> and the value an
* <CODE>Integer</CODE>. At least the <CODE>PdfName.CONTENTS</CODE> must be present
* @throws IOException on error
* @throws DocumentException on error
*/
public void PreClose(Dictionary<PdfName, int> exclusionSizes) {
(從PdfSignatureAppearance.cs)
/**
* Closes the document. No more content can be written after the
* document is closed.
* <p>
* If closing a signed document with an external signature the closing must be done
* in the <CODE>PdfSignatureAppearance</CODE> instance.
* @throws DocumentException on error
* @throws IOException on error
*/
public void Close() {
(從PdfStamper.cs)
原因是,當您使用CreateSignature(pdfReader, memoryStream, '\0'),
創建PdfStamper
時,壓模本身不會寫入內存流,而是寫入內部ByteBuffer
(這是最終簽名創建和集成所必需的)。只要在PdfSignatureAppearance.Close
期間將該ByteBuffer
的內容寫入存儲器流。
而且我看你使用
byte[] bt = memoryStream.GetBuffer();
請不要!除非您確定正確解釋緩衝區內容(可能包含其他垃圾數據),否則請使用
byte[] bt = memoryStream.ToArray();
改爲。
來源
2013-07-18 07:05:16
mkl
嗨@mkl,謝謝!但確定另一個困難......我應該按照以下步驟:preClose(),getDocumentBytes()和close()。 [1.]但是getDocumentBytes()方法在PdfSignatureAppearance中不可用。 [2.]而close()方法需要參數PdfDictionary ...我試圖重現一個使用iText(JAVA)創建的例子到iTextSharp(.NET)。該示例由Bruno Lowagie在白皮書「PDF文檔的數字簽名」,「4.3.3使用客戶端上創建的簽名在服務器上籤署文檔」(頁116)介紹。再次感謝 – brunopacola
1)getDocumentBytes似乎是一個剩餘的,使用rangeStream就是這樣的; 2)該字典是注入外部簽名的地方。 – mkl