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 @   sun8134  阅读(119)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
分享按钮
点击右上角即可分享
微信分享提示