.NET 8上进行PDF合并

前言:在.NET 8中使用itext7需安装 itext7 和 itext7.bouncy-castle-fips-adapter 两个Nuget包,或者使用Aspose.PDF、PdfSharpCore、Spire.PDF任意一个第三方包就行

对比:

|--------------|------|--------------|
| 第三方包 | 是否开源 | 是否收费 |
| itext7 | 是 | 不收费,免费使用没有水印 |
| PdfSharpCore | 是 | 不收费,免费使用没有水印 |
| Spire.PDF | 否 | 是,可以免费使用但有水印 |
| Aspose.PDF | 否 | 是,可以免费使用但有水印 |

安装如下Nuget的包

复制代码
  <ItemGroup>
    <PackageReference Include="Aspose.PDF" Version="23.11.1" />
    <PackageReference Include="itext7" Version="8.0.2" />
    <PackageReference Include="itext7.bouncy-castle-fips-adapter" Version="8.0.2" />
    <PackageReference Include="PdfSharpCore" Version="1.3.62" />
    <PackageReference Include="Spire.PDF" Version="9.12.0" />
  </ItemGroup>

项目结构示意图

一、itext7

复制代码
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfTest.PdfHelpers
{
    public class iText7Helper
    {
        // https://blog.csdn.net/m0_47619208/article/details/130725907
        /// <summary>
        /// 合并多个PDF文档
        /// 添加NuGet包引用:添加itext7和itext7.bouncy-castle-adapter,后者必须进行添加,否则会在运行报错。
        /// NuGet包为iText7,该库满足跨平台需求
        /// https://itextpdf.com/
        /// https://itextpdf.com/itext-suite-net-c
        /// </summary>
        /// <param name="fileList"></param>
        /// <param name="outFile"></param>
        /// <returns></returns>
        public static Task MergePdf(string[] fileList, string outFile)
        {
            using PdfWriter writer = new(outFile);
            using PdfDocument pdf = new(writer);
            PdfMerger merger = new(pdf);
            for (int i = 0; i < fileList.Length; i++)
            {
                PdfReader reader = new(fileList[i]);
                using PdfDocument pdfDocument = new(reader);
                merger.Merge(pdfDocument, 1, pdfDocument.GetNumberOfPages());
            }
            merger.Close();
            return Task.CompletedTask;
        }
    }
}

View Code

二、PdfSharpCore

复制代码
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfTest.PdfHelpers
{
    public class PdfSharpTool
    {
        // https://github.com/ststeiger/PdfSharpCore
        

        public static void PdfSharpCoreMerge(string[] files, string outFile)
        {
            using PdfDocument outPdfFile = new PdfDocument();
            foreach (var file in files)
            {
                using PdfDocument pdfFile = PdfReader.Open(file, PdfDocumentOpenMode.Import);

                foreach (var pdfPage in pdfFile.Pages)
                {
                    outPdfFile.AddPage(pdfPage);
                }
            }
            outPdfFile.Save(outFile);
        }

    }
}

View Code

三、Aspose.PDF

复制代码
using Aspose.Pdf.Facades;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfTest.PdfHelpers
{
    internal class AsposePdfHelper
    {
        //https://blog.aspose.com/zh/pdf/merge-multiple-pdf-files-in-csharp-net/
        //收费
        public static void Merge(string[] filesArray, string outFile)
        {
            // 创建 PdfFileEditor 对象
            PdfFileEditor pdfEditor = new PdfFileEditor();
            // 合并文件
            //pdfEditor.Concatenate("file1.pdf", "file2.pdf", "merged.pdf");
            // 合并文件
            pdfEditor.Concatenate(filesArray, outFile);

        }

        public static void MergeStream(FileStream[] streamArray, string outFile)
        {
            // 创建 PdfFileEditor 对象
            PdfFileEditor pdfEditor = new PdfFileEditor();
            // 输出流
            //FileStream outputStream = new FileStream("Merged.pdf", FileMode.Create);
            FileStream outputStream = new FileStream(outFile, FileMode.Create);
            //// 输入流
            //FileStream inputStream1 = new FileStream("file1.pdf", FileMode.Open);
            //FileStream inputStream2 = new FileStream("file2.pdf", FileMode.Open);
            //// 合并文件
            //pdfEditor.Concatenate(inputStream1, inputStream2, outputStream);


            // 创建流数组 
            //FileStream[] streamArray = new FileStream[3];
            //streamArray[0] = new FileStream("file1.pdf", FileMode.Open);
            //streamArray[1] = new FileStream("file2.pdf", FileMode.Open);
            //streamArray[2] = new FileStream("file3.pdf", FileMode.Open);
            // 合并文件
            pdfEditor.Concatenate(streamArray, outputStream);

        }

    }
}

View Code

四、Spire.PDF

复制代码
using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfTest.PdfHelpers
{
    public class SpirePdfHelper
    {
        //https://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html
        //https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html
        //https://www.cnblogs.com/dongweian/p/14305928.html
        //有水印

        /// <summary>
        /// 合并PDF文件
        /// </summary>
        /// <param name="files">待合并文件列表</param>
        /// <param name="outFile">合并生成的文件名称</param>
        public static void SpirePdfMerge(string[] files, string outFile)
        {
            var doc = PdfDocument.MergeFiles(files);
            doc.Save(outFile, FileFormat.PDF);
        }

        /// <summary>
        /// 按每页拆分PDF文件
        /// </summary>
        /// <param name="inFile">待拆分PDF文件名称</param>
        public static void SpirePdfSplit(string inFile)
        {
            var doc = new PdfDocument(inFile);
            doc.Split("SpirePdf_拆分-{0}.pdf");
            doc.Close();
        }

    }
}

View Code

在控制台上调用

复制代码
using PdfTest.PdfHelpers;

namespace PdfTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
        // 文件数组索引在前面的后,合并后的PDF,页面也在前面
            string [] files = Directory.GetFiles("Merge");
            #region Spire.Pdf
            //SpirePdfHelper.SpirePdfMerge(files, "SpirePdfMerge.pdf");
            //Console.WriteLine("使用 Spire.Pdf 合并文件完成...");
            #endregion

            #region PdfSharpCore
            //PdfSharpTool.PdfSharpCoreMerge(files, "PdfSharpCoreMerge.pdf");
            //Console.WriteLine("使用 PdfSharpCore 合并文件完成...");
            #endregion

            #region iText7
            //iText7Helper.MergePdf(files, "iText7PdfMerge.pdf");
            //Console.WriteLine("使用 iText7 合并文件完成...");
            #endregion

            #region AsposePdf
            AsposePdfHelper.Merge(files, "AsposePdfMerge.pdf");
            Console.WriteLine("使用 AsposePdf 合并文件完成...");
            #endregion

            Console.ReadKey();
        }
    }
}