C# based on PdfSharp to split pdf files and get MemoryStream C#基于PdfSharp拆分pdf,并生成MemoryStream

install-package PdfSharp -v 1.51.5185-beta

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string rawPdfFile = @"C:UsersffuDownloads2.pdf";
            PpfSharpExample(rawPdfFile);
            Console.ReadLine();
        }

        static void PpfSharpExample(string rawPdfFile)
        {            
            PdfDocument inputDocument = PdfReader.Open(rawPdfFile, PdfDocumentOpenMode.Import);
            int count = inputDocument.PageCount;
            List<MemoryStream> msList = new List<MemoryStream>();

            for (int idx = 0; idx < count; idx++)
            { 
                PdfPage page = inputDocument.Pages[idx]; 
                PdfDocument outputDocument = new PdfDocument();
                outputDocument.AddPage(page);
                MemoryStream ms = new MemoryStream();
                outputDocument.Save(ms);
                string splitPdf =idx+1+"splitPdf"+ Guid.NewGuid().ToString().Replace("-", "") + ".pdf";
                outputDocument.Save(splitPdf);
                msList.Add(ms);
                Console.WriteLine(ms.ToArray().Length);
            }
        }         
    }
}
原文地址:https://www.cnblogs.com/Fred1987/p/11133136.html