65.9K
CodeProject 正在变化。 阅读更多。
Home

简单的 .NET PDF 合并器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.75/5 (39投票s)

2008年7月31日

GPL3

1分钟阅读

viewsIcon

264013

downloadIcon

11241

一个简单的 .NET PDF 合并器,支持页眉和页脚文本。

引言

互联网上有一些 PDF 合并器可用。但是,要么它们是商业产品,要么不支持打印页眉和/或页脚文本,这对于打印页码等特别有用。

背景

本文档中介绍的 PDF 合并器使用开源 PDF 库 iTextSharp 来处理 PDF 文件。示例解决方案还包括一个小的 Windows Forms 应用程序来演示其功能。

使用代码

对于合并过程,PDF 库利用 iTextSharp.text.pdf.PdfWriter 对象的 PDF 页面事件。在初始化 PdfPageEvent 实例(继承自 iTextSharp.text.pdf.IPdfPageEvent)期间,可以将页眉/页脚文本所需的信息传递到构造函数调用中。

writer.PageEvent = new PdfPageEvents(/*Any type of information goes here*/);

重要的是,页眉和页脚文本都应该在 'public void OnEndPage(PdfWriter writer, Document document)' 方法中渲染。'public void OnStartPage(PdfWriter writer, Document document)' 是不准确的

为了提高性能,该库现在利用 iTextSharp 库中的 PdfCopy 类,而不是上述描述的方法。

即使显示的示例非常基础,它通常也能很好地概述如何填充页眉和页脚,例如,使用图片、文本等。

源代码

using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace HelveticSolutions.PdfLibrary
{
    public static class PdfMerger
    {
        /// <summary>
        /// Merge pdf files.
        /// </summary>
        /// <param name="sourceFiles">PDF files being merged.</param>
        /// <returns></returns>
        public static byte[] MergeFiles(List<byte[]> sourceFiles)
        {
            Document document = new Document();
            using (MemoryStream ms = new MemoryStream())
            {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;

                // Iterate through all pdf documents
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                {
                    // Create pdf reader
                    PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
                    int numberOfPages = reader.NumberOfPages;

                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        documentPageCounter++;
                        PdfImportedPage importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp = copy.CreatePageStamp(importedPage);

                        // Write header
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                            new Phrase("PDF Merger by Helvetic Solutions"), importedPage.Width / 2, importedPage.Height - 30,
                            importedPage.Width < importedPage.Height ? 0 : 1);

                        // Write footer
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                            new Phrase(String.Format("Page {0}", documentPageCounter)), importedPage.Width / 2, 30,
                            importedPage.Width < importedPage.Height ? 0 : 1);

                        pageStamp.AlterContents();

                        copy.AddPage(importedPage);
                    }

                    copy.FreeReader(reader);
                    reader.Close();
                }

                document.Close();
                return ms.GetBuffer();
            }
        }
    }
}

历史

  • 2008-01-08 - 创建文章。
  • 2014-11-17 - 根据其他用户的反馈更新了实现。
© . All rights reserved.