iTextSharp问题连接PDF文档 [英] iTextSharp problem concatenating PDF documents

查看:134
本文介绍了iTextSharp问题连接PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一堆其他PDF中构建单个PDF,我正在填写一些表单值。基本上我正在进行PDF邮件合并。我的代码如下:

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. 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);

}

我遇到的问题当它退出使用(Document document = new Document()) block时抛出空引用异常。

The problem I am having is that is throws a null reference exception when it exits the using (Document document = new Document()) block.

从调试iTextSharp源代码问题是以下方法 PdfAnnotationsimp

From debugging the iTextSharp source the problem is the below method in PdfAnnotationsimp

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

注释为空,因此抛出空引用异常。我应该做些什么来实例化这个?

annotations is null so this throws the null ref exception. Is there something I should be doing to instantiate this?

推荐答案

我改变了:

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

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.

这篇关于iTextSharp问题连接PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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