欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-12-09 21:32:31
install-package PdfSharp -v 1.51.5185-beta ......

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:\users\ffu\downloads\2.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);
            }
        }         
    }
}