首页 文章

ITextSharp Bug? - 标记受保护的pdf时的空引用

提问于
浏览
0

我正在使用 ITextSharp's PdfStamper填写pdf表格 .

这适用于不受保护和受密码保护的Pdfs,但受证书保护的PDF在调用 PdfStamper.Close() 时会导致 null reference exception .

有没有人遇到过这个?

失败程序示例:

using System.Security.Cryptography.X509Certificates;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using Org.BouncyCastle.Crypto;
using X509Certificate = Org.BouncyCastle.X509.X509Certificate;

namespace ITextError
{
    class Program
    {
        static X509Certificate2 certificate = new X509Certificate2(@"certificate.pfx","password",X509KeyStorageFlags.Exportable);
        static X509Certificate bouncyCertficate = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(certificate);
        static AsymmetricCipherKeyPair keyPair = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(certificate.PrivateKey);

        public static byte[] CreatePdf()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (Document document = new Document())
                {
                    var writer=PdfWriter.GetInstance(document, ms);
                    writer.SetEncryption(new X509Certificate[]{bouncyCertficate},
                                         new int[]{PdfWriter.ALLOW_MODIFY_CONTENTS},
                                         PdfWriter.STANDARD_ENCRYPTION_128
                                         ); 

                    document.Open();
                    document.Add(new Paragraph("Hello World"));
                }
                return ms.ToArray();
            }
        }

        public static byte[] StampPdf(byte[] src)
        {
            File.WriteAllBytes("tmp.pdf",src);
            PdfReader reader = new PdfReader("tmp.pdf",bouncyCertficate,keyPair.Private);
            using (MemoryStream ms = new MemoryStream())
            {
                using (PdfStamper stamper = new PdfStamper(reader, ms,reader.PdfVersion,true))
                {
                    PdfContentByte canvas = stamper.GetOverContent(1);
                    ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
                    stamper.Close();
                }
                return ms.ToArray();
            }
        }  

        static void Main(string[] args)
        {
            File.WriteAllBytes(@"output.pdf",StampPdf(CreatePdf()));
        }
    }
}

异常堆栈跟踪:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at iTextSharp.text.pdf.PdfEncryption.CreateInfoId(Byte[] id, Boolean modified)
at iTextSharp.text.pdf.PdfEncryption.GetFileID(Boolean modified)
at iTextSharp.text.pdf.PdfStamperImp.Close(PdfIndirectReference info, Int32 s kipInfo)
at iTextSharp.text.pdf.PdfStamperImp.Close(IDictionary`2 moreInfo)
at iTextSharp.text.pdf.PdfStamper.Close()
at ITextError.Program.StampPdf(Byte[] src) in Program.cs:line 45
at ITextError.Program.Main(String[] args) in Program.cs:line 53

只有在附加模式下打开压模时才会抛出异常 . 但是不使用追加模式会删除我需要保留的原始保护 .

ITextSharp是Nuget stable的5.5.4版本 .

1 回答

  • 0

    这个iText问题已在5.5.12版中得到解决 .

    相关的git签到是2017年3月31日的d9aede3,并且注释“在签署使用证书加密的文档时修复例外 . 建议https://github.com/MADzO ". This "签名”实际上是保留证书保护的任意标记的特例完整 .

    解决方法是在加密证书的情况下将加载的PDF的原始文档ID添加到 PdfEncryption 对象(其中包含加密相关信息),不仅仅是在密码加密的情况下 .

    PdfEncryption 类要求将其 documentID 成员设置为加密文档必须具有ID . 由于该成员未设置在手头的情况下, NullReferenceException 发生了 .


    使用当前的iTextSharp 5.5.14-SNAPSHOT开发版进行测试 . 通过撤消和重做上述修复来验证 .

    使用用户JC1001提供的测试数据对他的问题进行测试 . 对于受证书保护的文件,PdfStamper上的iTextSharp对象引用错误 .

相关问题