ASP.NET Core 切分、合并 PDF

继续使用 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());
        }


image



合并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;
        }
posted @ 2024-03-25 11:02  sun8134  阅读(53)  评论(0编辑  收藏  举报
分享按钮