使用第三方PDF库(iTextSharp或PDFSharp)生成多个报表并将它们合并到单个PDF文件中

发布时间 2023-04-03 11:12:54作者: Tammytan

一.下面是一个示例代码,该代码使用iTextSharp将多个PDF文件合并为一个:

public static void MergePDFs(string[] pdfPaths, string outputPath)
{
    using (FileStream stream = new FileStream(outputPath, FileMode.Create))
    {
        iTextSharp.text.Document document = new iTextSharp.text.Document();
        iTextSharp.text.pdf.PdfCopy pdf = new iTextSharp.text.pdf.PdfCopy(document, stream);
        document.Open();
        
        foreach (string pdfPath in pdfPaths)
        {
            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfPath);
            pdf.AddDocument(reader);
            reader.Close();
        }
        
        pdf.Close();
        document.Close();
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Telerik.Reporting;
using Telerik.Reporting.Processing;

namespace PDFGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            // 要生成PDF文件的报表文件名列表
            string[] reportNames = { "report1", "report2", "report3" };

            // 将所有报表生成的PDF文件保存到这个列表
            List<byte[]> pdfBytesList = new List<byte[]>();

            // 生成每个报表的PDF文件并将其添加到列表中
            foreach (string reportName in reportNames)
            {
                byte[] pdfBytes = GeneratePDF(reportName);
                pdfBytesList.Add(pdfBytes);
            }

            // 将所有PDF文件合并为单个文件
            MergePDFs(pdfBytesList, "merged.pdf");
        }

        static byte[] GeneratePDF(string reportName)
        {
            // 获取报表文件路径
            string reportPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, reportName + ".trdp");

            // 创建报表实例
            Report report = new Telerik.Reporting.Report();
            report.Load(reportPath);

            // 渲染报表并获取PDF字节数组
            ReportProcessor reportProcessor = new ReportProcessor();
            RenderingResult result = reportProcessor.RenderReport("PDF", report, null);
            byte[] pdfBytes = result.DocumentBytes;

            // 返回PDF字节数组
            return pdfBytes;
        }

        static void MergePDFs(List<byte[]> pdfBytesList, string outputPath)
        {
            // 创建PDF文档
            Document document = new Document();

            try
            {
                // 创建PdfCopy实例将生成的PDF文件添加到新的文档中
                PdfCopy copy = new PdfCopy(document, new FileStream(outputPath, FileMode.Create));
                document.Open();

                // 遍历所有PDF文件,将每个PDF文件添加到新文档中
                foreach (byte[] pdfBytes in pdfBytesList)
                {
                    PdfReader reader = new PdfReader(pdfBytes);
                    copy.AddDocument(reader);
                }
            }
            finally
            {
                document.Close();
            }
        }
    }
}

 下面是一个示例代码,它演示了如何使用PDFSharp将多个PDF文件合并为一个:

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Collections.Generic;

class Program {
    static void Main(string[] args) {
        // 要合并的PDF文件路径列表
        List<string> pdfFiles = new List<string>();
        pdfFiles.Add("file1.pdf");
        pdfFiles.Add("file2.pdf");
        pdfFiles.Add("file3.pdf");

        // 创建新PDF文件并打开PdfDocument对象
        PdfDocument outputDocument = new PdfDocument();
        foreach (string pdfFile in pdfFiles) {
            // 打开要合并的PDF文件
            PdfDocument inputDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);

            // 将当前PDF文件的所有页面复制到输出文档中
            for (int i = 0; i < inputDocument.PageCount; i++) {
                outputDocument.AddPage(inputDocument.Pages[i]);
            }
        }

        // 将新PDF文件保存到磁盘
        outputDocument.Save("merged.pdf");
        outputDocument.Close();
    }
}