合并两个(或更多)PDF [英] Combine two (or more) PDF's

查看:36
本文介绍了合并两个(或更多)PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我需要为我的销售人员提供每周报告包.这个包包含几个(5-10)个水晶报告.

Background: I need to provide a weekly report package for my sales staff. This package contains several (5-10) crystal reports.

问题:我想允许用户运行所有报告,也只运行一个报告.我想我可以通过创建报告然后执行以下操作来做到这一点:

Problem: I would like to allow a user to run all reports and also just run a single report. I was thinking I could do this by creating the reports and then doing:

List<ReportClass> reports = new List<ReportClass>();
reports.Add(new WeeklyReport1());
reports.Add(new WeeklyReport2());
reports.Add(new WeeklyReport3());
<snip>

foreach (ReportClass report in reports)
{
    report.ExportToDisk(ExportFormatType.PortableDocFormat, @"c:
eports" + report.ResourceName + ".pdf");
}

这将为我提供一个装满报告的文件夹,但我想通过电子邮件向每个人发送一个包含所有每周报告的 PDF.所以我需要把它们结合起来.

This would provide me a folder full of the reports, but I would like to email everyone a single PDF with all the weekly reports. So I need to combine them.

有没有一种简单的方法可以在不安装任何第三方控件的情况下做到这一点?我已经有了 DevExpress &CrystalReports 和我不想添加太多.

Is there an easy way to do this without install any more third party controls? I already have DevExpress & CrystalReports and I'd prefer not to add too many more.

最好将它们组合在 foreach 循环中还是单独的循环中?(或另一种方式)

Would it be best to combine them in the foreach loop or in a seperate loop? (or an alternate way)

推荐答案

我不得不解决一个类似的问题,我最终做的是创建一个使用 PDFSharp 项目,基本上是 MIT 许可的.

I had to solve a similar problem and what I ended up doing was creating a small pdfmerge utility that uses the PDFSharp project which is essentially MIT licensed.

代码非常简单,我需要一个 cmdline 实用程序,因此与 PDF 合并相比,我有更多的代码专门用于解析参数:

The code is dead simple, I needed a cmdline utility so I have more code dedicated to parsing the arguments than I do for the PDF merging:

using (PdfDocument one = PdfReader.Open("file1.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument two = PdfReader.Open("file2.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument outPdf = new PdfDocument())
{                
    CopyPages(one, outPdf);
    CopyPages(two, outPdf);

    outPdf.Save("file1and2.pdf");
}

void CopyPages(PdfDocument from, PdfDocument to)
{
    for (int i = 0; i < from.PageCount; i++)
    {
        to.AddPage(from.Pages[i]);
    }
}

这篇关于合并两个(或更多)PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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