From d5075183d45e5dcbd779cf9dae8d1c1d4e968975 Mon Sep 17 00:00:00 2001 From: chenjie Date: Tue, 14 Nov 2023 18:13:25 +0800 Subject: [PATCH] feat: issues/25 --- PDFiumSharp/PdfSupport.cs | 146 ++++++++++++++++++++++++++++++++ Samples/Test.Console/Program.cs | 72 +++++++++++----- 2 files changed, 198 insertions(+), 20 deletions(-) create mode 100644 PDFiumSharp/PdfSupport.cs diff --git a/PDFiumSharp/PdfSupport.cs b/PDFiumSharp/PdfSupport.cs new file mode 100644 index 0000000..a36e692 --- /dev/null +++ b/PDFiumSharp/PdfSupport.cs @@ -0,0 +1,146 @@ +using PDFiumSharp.Types; +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; + +namespace PDFiumSharp +{ + /* + https://github.com/ArgusMagnus/PDFiumSharp/issues/25 + */ + public class PdfSupport + { + /// + /// Split pdf + /// + /// The destination document which add the pages. + /// from 1 + /// + public static MemoryStream GetPDFPage(PdfDocument dest, int pageNumber) + { + var newDocument = PDFium.FPDF_CreateNewDocument(); + if (PDFium.FPDF_ImportPages(newDocument, dest.Handle, pageNumber.ToString(), 0)) + { + var ms = new MemoryStream(); + { + PDFium.FPDF_SaveAsCopy(newDocument, ms, SaveFlags.Incremental); + PDFium.FPDF_CloseDocument(newDocument); + } + return ms; + } + return null; + } + /// + /// Split pdf + /// + /// The destination document which add the pages. + /// + ///eg. "1,1,1,1" 4 1page + /// "1-3" from 1 page to 3 + /// "1,3" 1page、3page + /// + /// + public static MemoryStream GetPDFPage(PdfDocument dest, string pagerange) + { + var newDocument = PDFium.FPDF_CreateNewDocument(); + if (PDFium.FPDF_ImportPages(newDocument, dest.Handle, pagerange, 0)) + { + var ms = new MemoryStream(); + { + PDFium.FPDF_SaveAsCopy(newDocument, ms, SaveFlags.Incremental); + PDFium.FPDF_CloseDocument(newDocument); + } + return ms; + } + + return null; + } + /// + /// Merge pdf + /// + /// + /// + public static MemoryStream MergePDF(params PdfDocument[] documents) + { + if (documents.Length == 0) + { + throw new ArgumentException("arg: documents not be 0 count"); + } + var newDocument = PDFium.FPDF_CreateNewDocument(); + var index = 0; + var res = false; + for (int i = 0; i < documents.Length; i++) + { + var dest_doc = documents[i]; + if (dest_doc is PdfDocument dest) + { + var pageCount = PDFium.FPDF_GetPageCount(dest.Handle); + var page2 = $"{1}-{pageCount}"; + res = PDFium.FPDF_ImportPages(newDocument, dest.Handle, page2, index); + index += pageCount; + } + } + if (!res) + { + return null; + } + var ms = new MemoryStream(); + { + PDFium.FPDF_SaveAsCopy(newDocument, ms, SaveFlags.Incremental); + PDFium.FPDF_CloseDocument(newDocument); + } + return ms; + } + + /// + /// Import some pages to a PDF document. + /// + /// The destination document which add the pages. + /// A document to be imported. + /// The page index wanted to insert from. + /// + public static MemoryStream ImportPage(PdfDocument dest, PdfDocument src, int index) + { + var pageCount = PDFium.FPDF_GetPageCount(dest.Handle); + if (index < 0) + index = -1; + if (index >= pageCount) + index = pageCount; + var sheet = index + 1; + var newDocument = PDFium.FPDF_CreateNewDocument(); + var preSheet = ""; + var afterSheet = ""; + bool res = false; + var iindex = 0; + if (sheet > 1) + { + preSheet = $"{1}-{sheet - 1}"; + res = PDFium.FPDF_ImportPages(newDocument, dest.Handle, preSheet, iindex); + iindex += sheet - 1; + } + var currCount = PDFium.FPDF_GetPageCount(src.Handle); + var currSheet = $"{1}-{currCount}"; + res = PDFium.FPDF_ImportPages(newDocument, src.Handle, currSheet, iindex); + iindex += currCount; + if (sheet < pageCount) + { + afterSheet = $"{sheet}-{pageCount}"; + res = PDFium.FPDF_ImportPages(newDocument, dest.Handle, afterSheet, iindex); + } + + if (res) + { + var ms = new MemoryStream(); + { + PDFium.FPDF_SaveAsCopy(newDocument, ms, SaveFlags.Incremental); + PDFium.FPDF_CloseDocument(newDocument); + } + return ms; + } + return null; + } + + } +} diff --git a/Samples/Test.Console/Program.cs b/Samples/Test.Console/Program.cs index 1391655..81a680c 100644 --- a/Samples/Test.Console/Program.cs +++ b/Samples/Test.Console/Program.cs @@ -5,27 +5,59 @@ using System.Threading.Tasks; using PDFiumSharp; using System.IO; +using System.Security.Cryptography; namespace TestConsole { - class Program - { - static void Main(string[] args) - { - using (var doc = new PdfDocument("TestDoc.pdf", "password")) - { - int i = 0; - foreach (var page in doc.Pages) - { - using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, true)) - using (var stream = new FileStream($"{i++}.bmp", FileMode.Create)) - { - page.Render(bitmap); - bitmap.Save(stream); - } - } - } - Console.ReadKey(); - } - } + class Program + { + static void Main(string[] args) + { + using (var doc = new PdfDocument("TestDoc.pdf", "password")) + { + int i = 0; + foreach (var page in doc.Pages) + { + using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, true)) + using (var stream = new FileStream($"{i++}.bmp", FileMode.Create)) + { + page.Render(bitmap); + bitmap.Save(stream); + } + } + } + TestSplit(); + TestMerge(); + Console.ReadKey(); + } + + static void TestSplit() + { + using (var doc = new PdfDocument("TestDoc.pdf", "password")) + { + if (!Directory.Exists("TestDoc")) + { + Directory.CreateDirectory("TestDoc"); + } + for (global::System.Int32 i = 0; i < doc.Pages.Count; i++) + { + var num = i + 1; + using (var stream = PdfSupport.GetPDFPage(doc, num)) + { + File.WriteAllBytes($@"TestDoc\{num}.pdf", stream.ToArray()); + } + } + } + } + + static void TestMerge() + { + var files = Directory.GetFiles("TestDoc", "*.pdf")?.ToList(). + ConvertAll(file => new PdfDocument(file)); + using (var stream = PdfSupport.MergePDF(files.ToArray())) + { + File.WriteAllBytes("TestDocNew.pdf", stream.ToArray()); + } + } + } }