获取PdfStamper与MemoryStreams工作(C#,iTextSharp的) [英] Getting PdfStamper to work with MemoryStreams (c#, itextsharp)
问题描述
这来找我返工旧代码,签署PDF文件转换为新的,其标志MemoryStreams(字节数组)的到来和通过Web服务发送。简单吧?嗯,这是昨天。今天我只是无法得到它的工作。
It came to me to rework old code which signs PDF files into new one, which signs MemoryStreams (byte arrays) that come and are sent by web services. Simple, right? Well, that was yesterday. Today I just can't get it to work.
这是旧代码,它使用文件流和它的作品:
This is the old code, which uses FileStreams and it works:
public static string OldPdfSigner(PdfReader pdfReader, string destination, string password, string reason, string location, string pathToPfx)
{
using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
{
...
using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, new FileStream(destination, FileMode.Create, FileAccess.Write), '\0'))
{
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = reason;
sap.Location = location;
return destination;
}
}
}
下面是我所重做自己会抛出System.ObjectDisposedException:无法访问已关闭的流
Below is what I've redone myself which throws System.ObjectDisposedException: Cannot access a closed Stream.
public static byte[] PdfSigner(PdfReader pdfReader, string password, string reason, string location, string pathToPfx)
{
using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
{
...
MemoryStream outputStream = new MemoryStream();
using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, outputStream, '\0'))
{
st.Writer.CloseStream = false;
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = reason;
sap.Location = location;
st.Close();
outputStream.Position = 0;
return outputStream.ToArray();
}
}
}
如果我注释掉
and if I comment out
st.Close();
它创建一个空文件。我在做什么错了?
it creates an empty document. What am I doing wrong?
推荐答案
不具体到您的签名代码,但是工作时 MemoryStream的
和 PdfStamper
,按照这个一般模式:
Not specific to your signing code, but when working with MemoryStream
and PdfStamper
, follow this general pattern:
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms, '\0', true)) {
// do stuff
}
return ms.ToArray();
}
-
的MemoryStream
工具的IDisposable
,因此包括使用
语句。 - 在
PdfStamper
使用
语句采用处理对象的照顾,所以你不需要调用关闭()
,并不需要设置CloseStream
属性。 - 您的代码片段将返回字节数组的太早的
PdfStamper
使用
语句中,让你的的MemoryStream
实际上是一个无操作。返回字节数组的之外的PdfStamper
使用
语句,和里面的的的MemoryStream
使用
语句。 - 一般没有必要重置
的MemoryStream
位置
属性。 - 忽略
PdfStamper
构造之上 - 这是从一些测试代码我有填写表格,并使用任何你需要做您的签名的构造函数/法 MemoryStream
implementsIDisposable
, so include ausing
statement.- The
PdfStamper
using
statement takes care of disposing the object, so you don't need to callClose()
, and don't need to set theCloseStream
property. - Your code snippet is returning the byte array too soon, inside the
PdfStamper
using
statement, so yourMemoryStream
is effectively a no-op. Return the byte array outside of thePdfStamper
using
statement, and inside theMemoryStream
using
statement. - Generally there's no need to reset the
MemoryStream
Position
property. - Ignore the
PdfStamper
constructor above - it's from some test code I had for filling forms, and use whatever constructor/method you need to do your signing.
这篇关于获取PdfStamper与MemoryStreams工作(C#,iTextSharp的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!