简体   繁体   English

iTextSharp将文档与表单合并

[英]iTextSharp merging documents with forms

So I have a fairly basic iTextSharp implementation that creates a PDF. 因此,我有一个相当基本的iTextSharp实现,可以创建PDF。 As part of that, any "attached documents" need to be read and merged into the generated document. 作为其一部分,任何“附加文档”都需要读取并合并到生成的文档中。 This works fine except I just ran into a document someone attached that breaks everything. 除我刚碰到一个附件损坏了所有内容的文档外,此方法工作正常。 The PDF still get's generated, but the document that comes out only displays the first page, and Adobe Reader spits out errors (18 if it means anything to anyone) whenever it tries to view pages 2 - 7. 仍然生成了PDF,但是出来的文档仅显示第一页,并且Adobe Reader在尝试查看第2-7页时会吐出错误(如果对任何人都意味着18的错误)。

On page 8 is ANOTHER document that was merged PRIOR to the problem document, and that show up fine. 在第8页上的是另一个文档,该文档已在问题文档之前合并,并且显示得很好。 Then the merged document that is causing the issue is next (25 pages) and IT shows up fine. 然后是引起问题的合并文档(25页),IT正常显示。

But something with merging that document breaks those previous pages. 但是合并该文档的某些操作破坏了先前的页面。 It's really weird because I'd expect it to break the documents that were merged right before it, as well as page 1, etc... 这真的很奇怪,因为我希望它会破坏之前合并的文档以及第1页等。

The only thing I can see in the document that is different, is that page 2 of the problem document being merged in has a filled out form on it. 我在文档中唯一看到的是不同的,是合并的问题文档的第2页上有填写的表格。 I'm trying to get one of the people here that know PDFs better than me to get me one without that form to ensure that's the issue, but it seems like my best candidate right now... 我想让这里的一个人比我更了解PDF,让我不带PDF的人来确保这是问题所在,但现在看来我是最好的候选人...

I tried the following (adding the "Remove any forms" part), but I still have issues. 我尝试了以下操作(添加了“删除所有表单”部分),但是仍然存在问题。 Any ideas? 有任何想法吗?

var reader = new PdfReader(filePath);

// Remove any forms
if (reader.AcroForm != null)
{
    var memStream = new MemoryStream();
    var stamper = new PdfStamper(reader, memStream) { FormFlattening = true };
    stamper.Close();
    reader = new PdfReader(memStream.ToArray());
}

var numberOfPages = reader.NumberOfPages;

var cb = writer.DirectContent;

var i = 0;
while (i < numberOfPages)
{
    i++;
    document.SetPageSize(reader.GetPageSizeWithRotation(i));
    document.NewPage();

    var page = writer.GetImportedPage(reader, i);
    var rotation = reader.GetPageRotation(i);

    if (rotation == 90 || rotation == 270)
    {
        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
    }
    else
    {
        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }
}

reader.Close();

Please read chapter 6 of my book , and you'll notice that using PdfWriter to concatenate (or merge) PDF documents is wrong. 请阅读我的书的第6章 ,您会发现使用PdfWriter连接(或合并)PDF文档是错误的。 Concatenating PDFs is done using PdfCopy . 使用PdfCopy完成PDF的串联。

If the documents that are being merged contain AcroForm forms, the book recommends the use of PdfCopyFields . 如果要合并的文档包含AcroForm表单,则本书建议使用PdfCopyFields In more recent versions of iTextSharp, PdfCopyFields is deprecated in favor of the method that is described here: copy pdf form with PdfCopy not working in itextsharp 5.4.5.0 在iTextSharp的最新版本中,不赞成使用PdfCopyFields而推荐使用此处描述的方法: 使用PdfCopy复制pdf表单在itextsharp 5.4.5.0中不起作用

Without an example, nobody will be able to give you a better answer. 没有示例,没有人能够为您提供更好的答案。 You'll need to share some of the PDFs that cause the problem. 您需要共享一些导致问题的PDF。

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

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