获取PdfStamper与MemoryStreams工作(C#,iTextSharp的) [英] Getting PdfStamper to work with MemoryStreams (c#, itextsharp)

查看:2050
本文介绍了获取PdfStamper与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 implements IDisposable, so include a using statement.
    • The PdfStamper using statement takes care of disposing the object, so you don't need to call Close(), and don't need to set the CloseStream property.
    • Your code snippet is returning the byte array too soon, inside the PdfStamper using statement, so your MemoryStream is effectively a no-op. Return the byte array outside of the PdfStamper using statement, and inside the MemoryStream 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屋!

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