用C#将多个jpg合成一个pdf

nuget安装iTextSharp

static void MergePDF(string picPath,string pdfPath)
        {
            string[] picFileNames=Directory.GetFiles(picPath, "*");
            List<string> fileNames = picFileNames.ToList();
            fileNames.Sort((l,r)=> {
                int lnum = int.Parse(Path.GetFileName(l).Replace(".jpg",""));
                int rnum = int.Parse(Path.GetFileName(r).Replace(".jpg",""));
                return lnum.CompareTo(rnum);
            });
            Document document = new Document(new Rectangle(2100,1488));//设定pdf的大小
            PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
            document.Open();
            foreach (string jpgPath in fileNames)
            {
                Image jpgImage = Image.GetInstance(jpgPath);

                jpgImage.ScaleToFit(document.PageSize.Width, document.PageSize.Height);//调整图片的大小

                document.Add(jpgImage);
            }
            document.Close();
        }

如果报错:PdfWriter.GetInstance未将对象引用设置到对象的实例.
VS设置为调试仅我的代码。

posted @   JohnYang819  阅读(270)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2021-05-09 安装Scikit-learn 0.24.2版本
点击右上角即可分享
微信分享提示