简体   繁体   English

iTextSharp问题连接PDF文档

[英]iTextSharp problem concatenating PDF documents

I am trying to build up a single PDF from a bunch of other PDFs that I am filling out some form values in. Essentially I am doing a PDF mail merge. 我正在尝试从一堆其他PDF中构建单个PDF,我正在填写一些表单值。基本上我正在进行PDF邮件合并。 My code is below: 我的代码如下:

byte[] completedDocument = null;
using (MemoryStream streamCompleted = new MemoryStream())
{
    using (Document document = new Document())
    {
    document.Open();
    PdfCopy copy = new PdfCopy(document, streamCompleted);
    copy.Open();

    foreach (var item in eventItems)
    {
        byte[] mergedDocument = null;
        PdfReader reader = new PdfReader(pdfTemplates[item.DataTokens[NotifyTokenType.OrganisationID]]);
        using (MemoryStream streamTemplate = new MemoryStream())
        {
            using (PdfStamper stamper = new PdfStamper(reader, streamTemplate))
            {
                foreach (var token in item.DataTokens)
                {
                    if (stamper.AcroFields.Fields.Any(fld => fld.Key == token.Key.ToString()))
                    {
                        stamper.AcroFields.SetField(token.Key.ToString(), token.Value);
                    }
                }
                stamper.FormFlattening = true;
                stamper.Writer.CloseStream = false;
            }

            mergedDocument = new byte[streamTemplate.Length];
            streamTemplate.Position = 0;
            streamTemplate.Read(mergedDocument, 0, (int)streamTemplate.Length);
        }
        reader = new PdfReader(mergedDocument);

        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            document.SetPageSize(PageSize.A4);
            copy.AddPage(copy.GetImportedPage(reader, i));
        }
    }
}
completedDocument = new byte[streamCompleted.Length];
streamCompleted.Position = 0;
streamCompleted.Read(completedDocument, 0, (int)streamCompleted.Length);

} }

The problem I am having is that is throws a null reference exception when it exits the using (Document document = new Document()) block. 我遇到的问题是当它退出using (Document document = new Document())块时抛出一个空引用异常。

From debugging the iTextSharp source the problem is the below method in PdfAnnotationsimp 通过调试iTextSharp源,问题是PdfAnnotationsimp的以下方法

public bool HasUnusedAnnotations() {
            return annotations.Count > 0;
        }

annotations is null so this throws the null ref exception. annotations为null,因此抛出null ref异常。 Is there something I should be doing to instantiate this? 我应该做些什么来实例化这个?

I changed: 我变了:

document.Open();
PdfCopy copy = new PdfCopy(document, streamCompleted);

to

PdfCopy copy = new PdfCopy(document, streamCompleted);
document.Open();

And it fixed the problem. 它解决了这个问题。 This library needs better exception handling. 该库需要更好的异常处理。 When you do something slightly wrong it falls over horribly and gives you no clue about what you did wrong. 如果你做了一些稍微错误的事情,它就会惨不忍睹,并且不会让你知道你做错了什么。 I have no idea how i could possibly have worked this out if I didn't have the source code. 如果我没有源代码,我不知道如何才能解决这个问题。

What version of iTextSharp are you using? 你在用什么版本的iTextSharp? The Document class doesn't implement IDisposable so you can't wrap it in a using block. Document类不实现IDisposable因此您无法将其包装在using块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM