如何合并多个pdf文件(在运行时生成)? [英] How to merge multiple pdf files (generated in run time)?

查看:310
本文介绍了如何合并多个pdf文件(在运行时生成)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何合并多个pdf文件(在运行时生成)通过 ItextSharp 然后打印它们。

How to merge multiple pdf files (generated on run time) through ItextSharp then printing them.

I发现以下链接,但该方法需要考虑pdf名称存储的pdf文件,这不是我的情况。

I found the following link but that method requires the pdf names considering that the pdf files stored and this is not my case .

我有多个报告我会将它们转换为 pdf文件通过此方法:

I have multiple reports i'll convert them to pdf files through this method :

private void AddReportToResponse(LocalReport followsReport)
{
    string mimeType;
    string encoding;
    string extension;
    string[] streams = new string[100];
    Warning[] warnings = new Warning[100];
    byte[] pdfStream = followsReport.Render("PDF", "", out mimeType, out encoding, out extension, out streams, out warnings);
  //Response.Clear();
  //Response.ContentType = mimeType;
  //Response.AddHeader("content-disposition", "attachment; filename=Application." + extension);
  //Response.BinaryWrite(pdfStream);
  //Response.End();
}

现在我要合并所有这些生成的文件(字节)在一个pdf文件中打印

Now i want to merge all those generated files (Bytes) in one pdf file to print it

推荐答案

如果要使用iText合并源文档(夏普),有两种基本情况:

If you want to merge source documents using iText(Sharp), there are two basic situations:


  1. 你真的想要合并文件,以原始格式获取页面,尽可能多地传输内容和交互式注释。在这种情况下,您应该使用基于 Pdf * Copy * 类系列成员的解决方案。

  1. You really want to merge the documents, acquiring the pages in their original format, transfering as much of their content and their interactive annotations as possible. In this case you should use a solution based on a member of the Pdf*Copy* family of classes.

您实际上想要将源文档中的页面集成到一个新文档中,但希望新文档管理一般格式并且不关心原始文档中的交互功能(注释...)(甚至想要摆脱他们)。在这种情况下,您应该使用基于 PdfWriter 类的解决方案。

You actually want to integrate pages from the source documents into a new document but want the new document to govern the general format and don't care for the interactive features (annotations...) in the original documents (or even want to get rid of them). In this case you should use a solution based on the PdfWriter class.

您可以在第6章(特别是第6.4节)中找到详细信息。 href =http://itextpdf.com/book/ =noreferrer> iText in Action - 2nd Edition 。可以通过此处和C#'ified versions 这里

You can find details in chapter 6 (especially section 6.4) of iText in Action — 2nd Edition. The Java sample code can be accessed here and the C#'ified versions here.

使用<$ c的简单示例$ c> PdfCopy 是 Concatenate.java / < a href =http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter06&ex=Concatenate =noreferrer> Concatenate.cs 。代码的核心部分是:

A simple sample using PdfCopy is Concatenate.java / Concatenate.cs. The central piece of code is:

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

            for (int i = 0; i < pdf.Count; ++i)
            {
                PdfReader reader = new PdfReader(pdf[i]);
                // loop over the pages in that document
                int n = reader.NumberOfPages;
                for (int page = 0; page < n; )
                {
                    copy.AddPage(copy.GetImportedPage(reader, ++page));
                }
            }
        }
    }
    mergedPdf = ms.ToArray();
}

这里 pdf 可以要么定义为 List< byte []> ,要么立即包含源文档(适用于合并中间内存文档的用例),要么定义为 List< String> 包含源文档文件的名称(如果从磁盘合并文档,则相应)。

Here pdf can either be defined as a List<byte[]> immediately containing the source documents (appropriate for your use case of merging intermediate in-memory documents) or as a List<String> containing the names of source document files (appropriate if you merge documents from disk).

概述引用章节的结尾总结了所提到的类的用法:

An overview at the end of the referenced chapter summarizes the usage of the classes mentioned:


  • PdfCopy :从一个或多个现有PDF文档复制页面。主要缺点: PdfCopy 未检测到冗余内容,并且在连接表单时失败。

  • PdfCopy: Copies pages from one or more existing PDF documents. Major downsides: PdfCopy doesn’t detect redundant content, and it fails when concatenating forms.

PdfCopyFields :将不同表单的字段放在一个表单中。使用 PdfCopy 连接表单时,可以用来避免表单字段遇到的问题。内存使用可能是个问题。

PdfCopyFields: Puts the fields of the different forms into one form. Can be used to avoid the problems encountered with form fields when concatenating forms using PdfCopy. Memory use can be an issue.

PdfSmartCopy :从一个或多个现有PDF文档中复制页面。 PdfSmartCopy 能够检测到冗余内容,但它需要比 PdfCopy 更多的内存和CPU。

PdfSmartCopy: Copies pages from one or more existing PDF documents. PdfSmartCopy is able to detect redundant content, but it needs more memory and CPU than PdfCopy.

PdfWriter :从头开始生成PDF文档。可以从其他PDF文档导入页面。主要缺点是导入页面的所有交互功能(注释,书签,字段等)在此过程中都会丢失。

PdfWriter: Generates PDF documents from scratch. Can import pages from other PDF documents. The major downside is that all interactive features of the imported page (annotations, bookmarks, fields, and so forth) are lost in the process.

这篇关于如何合并多个pdf文件(在运行时生成)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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