使用iTextSharp将现有PDF从文件添加到未写入的文档 [英] Add an existing PDF from file to an unwritten document using iTextSharp

查看:101
本文介绍了使用iTextSharp将现有PDF从文件添加到未写入的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将现有磁盘上PDF文件中的页面正确添加到当前生成的内存中PDF文档中?

How do I correctly add the page(s) from an existing on-disk PDF file, to a currently-being-generated in-memory PDF document?

我们有一个使用iTextSharp生成PDF文档的类。这很好用。目前我们添加条款&条件作为最后一页的图像:

We have a class that produces a PDF document using iTextSharp. This works fine. At the moment we add a Terms & Conditions as an image at the last page:

this.nettPriceListDocument.NewPage();
this.currentPage++;
Image logo = Image.GetInstance("/inetpub/Applications/Trade/Reps/images/TermsAndConditions.gif");
logo.SetAbsolutePosition(0, 0);
logo.ScaleToFit(this.nettPriceListDocument.PageSize.Width, this.nettPriceListDocument.PageSize.Height);
this.nettPriceListDocument.Add(logo);

我将此图像作为PDF文档,并希望附加它。如果我能解决这个问题,就可以将我们可能需要的其他PDF文档追加到我生成的内容中。

I have this image as a PDF document and would prefer to append it. If I can figure this out it would be possible to append other PDF documents we might need to what I am generating.

我试过:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfWriter writer = PdfWriter.GetInstance(this.nettPriceListDocument, this.nettPriceListMemoryStream);
PdfContentByte content = writer.DirectContentUnder;
for (int pageno = 1; pageno < reader.NumberOfPages + 1; pageno++)
{
    this.nettPriceListDocument.NewPage();
    this.currentPage++;
    PdfImportedPage newpage = writer.GetImportedPage(reader, pageno);
    content.AddTemplate(newpage, 1f, 1f);
}

这导致<$ c $的文档未打开异常c> writer.DirectContentUnder

我也尝试过:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfConcatenate concat = new PdfConcatenate(this.nettPriceListMemoryStream);
concat.AddPages(reader);

这导致在文档的通常第一页上插入一个空白的,奇怪大小的页面。

Which results in inserting a blank, oddly sized page over the usual first page of the document.

我也尝试过:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfCopy copier = new PdfCopy(nettPriceListDocument, nettPriceListMemoryStream);
for (int pageno = 1; pageno < reader.NumberOfPages + 1; pageno++)
{
    this.currentPage++;
    PdfImportedPage newpage = copier.GetImportedPage(reader, pageno);
    copier.AddPage(newpage);
}
copier.Close();
reader.Close();

这会导致复印机上的NullReferenceException(新页面)

我也试过:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfCopyFields copier = new PdfCopyFields(nettPriceListMemoryStream);
copier.AddDocument(reader);

这也会导致复印机上的NullReferenceException.AddDocument(读者)

我从各种StackOverflow问题和答案中获得了大部分这些想法。没有人似乎要处理的一件事是将现有PDF文件中的新页面添加到尚未写入磁盘上的PDF文件的已存在的内存中文档。该文档已经打开并且已经写入了数据页面。如果我离开这个条款&条件程序输出,或者只是写一个图像(就像最初的一样),得到的PDF出来就好了。

I've got most of these ideas from various StackOverflow questions and answers. One thing that nobody seemed to deal with, is adding new page(s) from an existing PDF file, to an already-existing in-memory document that is not yet written to a PDF file on disk. This document has already been opened and had pages of data written into it. If I leave this Terms & Conditions procedure out, or just write it is an image (like originally), the resulting PDF comes out just fine.

要完成我的开始:我如何正确添加从现有的磁盘PDF文件到当前正在生成的内存中PDF文档的页面?

To finish as I started: How do I correctly add the page(s) from an existing on-disk PDF file, to a currently-being-generated in-memory PDF document?

提前感谢并感谢您的思考就此而言。如果我能提供更多信息,请告诉我。

Thanks and appreciation in advance for your musings on this. Please let me know if I can provide further information.

推荐答案

这是一个简单的合并方法,可将PDF文件复制到一个PDF中。合并pdf时我经常使用这种方法。我已经使用它来合并不同大小的页面,没有问题。希望它有所帮助。

Here's a simple merge method that copies PDF files into one PDF. I use this method quite often when merging pdfs. I have used this to merge different sized pages as well without issue. Hope it helps.

public MemoryStream MergePdfForms(List<byte[]> files)
{
    if (files.Count > 1)
    {
        PdfReader pdfFile;
        Document doc;
        PdfWriter pCopy;
        MemoryStream msOutput = new MemoryStream();

        pdfFile = new PdfReader(files[0]);

        doc = new Document();
        pCopy = new PdfSmartCopy(doc, msOutput);

        doc.Open();

        for (int k = 0; k < files.Count; k++)
        {
            pdfFile = new PdfReader(files[k]);
            for (int i = 1; i < pdfFile.NumberOfPages + 1; i++)
            {
                ((PdfSmartCopy)pCopy).AddPage(pCopy.GetImportedPage(pdfFile, i));
            }
            pCopy.FreeReader(pdfFile);
        }

        pdfFile.Close();
        pCopy.Close();
        doc.Close();

        return msOutput;
    }
    else if (files.Count == 1)
    {
        return new MemoryStream(files[0]);
    }

    return null;
}

这篇关于使用iTextSharp将现有PDF从文件添加到未写入的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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