繼續使用 DocNET: https://github.com/GowenGit/docnet
Nuget:
Install-Package Docnet.Core
切分 PDF
注意,DocNET 的 Split 方法的索引是從 0 開始的,所以我們認爲的獲取 PDF 的第三到第五頁,在 DocNET 的 Split 方法其實是從第二到第四頁…
public static bool SplitPdf(string inputPath,int pageFrom,int pageTo, string outputPath) { bool result = false; try { using (var docnet = DocLib.Instance) { var bytes = docnet.Split(inputPath, pageFrom - 1, pageTo - 1); File.WriteAllBytes(outputPath, bytes); } result = true; } catch (Exception e) { Console.WriteLine($"error:{e}"); } return result; }
調用下看看效果:
public IActionResult Index() { bool result = PdfHelper.SplitPdf("F:\\pdf\\compressed.tracemonkey-pldi-09.pdf",3,5, "F:\\pdf\\compressed.tracemonkey-pldi-10.pdf"); return Content(result.ToString()); }
合併PDF
合併就比較簡單
public static bool MergePdf(string inputPath1, string inputPath2, string outputPath) { bool result = false; try { using (var docnet = DocLib.Instance) { var bytes = docnet.Merge(inputPath1, inputPath2); File.WriteAllBytes(outputPath, bytes); } result = true; } catch (Exception e) { Console.WriteLine($"error:{e}"); } return result; }