首页 > 编程语言 >ASP.NET Core PNG 图片转 PDF

ASP.NET Core PNG 图片转 PDF

时间:2024-03-29 14:57:11浏览次数:20  
标签:Core ASP img using itext bouncy PDF new castle

上一篇 https://www.cnblogs.com/sun8134/p/18094489 用的 DocNET 可以将 JPG 图片转 PDF

但 PNG 图片会变成空白,如果 PNG 图片,就需要用 itext-dotnet :https://github.com/itext/itext-dotnet


继续 Nuget:


Install-Package itext

Install-Package itext.bouncy-castle-adapter

注意:如果不安装 itext.bouncy-castle-adapter,在 new PdfWriter() 的时候可能会报错:

Either com.itextpdf:bouncy-castle-adapter or com.itextpdf:bouncy-castle-fips-adapter dependency must be added in order to use BouncyCastleFactoryCreator


合并 PDF:


        public static bool Png2Pdf(List<string> imageList, string outputPath)
        {
            bool result = false;
            try
            {
                using (FileStream pdfStream = new FileStream(outputPath, FileMode.Create))
                {
                    using (PdfWriter pdfWriter = new PdfWriter(pdfStream))
                    {
                        using (PdfDocument pdfDocument = new PdfDocument(pdfWriter))
                        {
                            using (Document document = new Document(pdfDocument))
                            {
                                foreach (string imagePath in imageList)
                                {
                                    ImageData imageData = ImageDataFactory.Create(imagePath);
                                    iText.Layout.Element.Image img = new iText.Layout.Element.Image(imageData);
                                    PdfPage page = pdfDocument.AddNewPage(new iText.Kernel.Geom.PageSize(img.GetImageWidth(), img.GetImageHeight()));
                                    document.Add(img);
                                }
                            }
                        }
                    }
                }

                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine($"error:{e}");
            }
            return result;
        }



在控制器种调用:

        public IActionResult Index()
        {
            List<string> imageList = new List<string>();
            for (int pageNumber = 1; pageNumber <= 14; pageNumber++)
            {
                imageList.Add($"F:\\pdf\\images\\Page_{pageNumber}.png");
            }
            bool result = PdfHelper.Png2Pdf(imageList, "F:\\pdf\\compressed.tracemonkey-pldi-14.pdf");
            return Content(result.ToString());
        }


看下对比效果:

左边是jpg右边是png

image

标签:Core,ASP,img,using,itext,bouncy,PDF,new,castle
From: https://www.cnblogs.com/sun8134/p/18103848

相关文章

  • 使用nssm打包.net core api服务
     去官网下载nssm,然后cmd进入nssm的目录。下载地址:http://www.nssm.cc/download  命令行打开services.msc,就会发现成功了或者    这时候你的api就变成了windowsservice啦!......
  • .Net Core 改变响应值的几种方法
    1.中间件:usingMicrosoft.AspNetCore.Mvc;usingSystem.Text;varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddControllers();varapp=builder.Build();app.UseMiddleware<ReplaceMiddleware>();app.MapControllers();app.Run();public......
  • .net core 解析xml字符串
            stringxml=@"<root><element1>Text1</element1> <element2>Text2</element2</root>";        XDocumentxdoc=XDocument.Parse(xml);        //读取XML文件        XElementroot......
  • 8、.NET Core 实践 2024-03-29 11:44 CPU过高
    Windbg指令记录0:008>!runawayUserModeTimeThreadTime7:35c00days0:03:04.9538:111c0days0:03:01.6406:4d300days0:02:57.2815:84240days0:02:52.6400:6fe80days0:00:00.0312:6c280......
  • EFCore
    《1》数据更新方法//方法1批量更新数据库数据,直接使用SQL语句ctx.Database.ExecuteSql($"UPDATE[T_Books]SET[Price]=[Price]+2");//方法2EFCore仍会为每个本书发送UPDATE语句,并且数据库必须单独执行每个语句......
  • Aspose Cells 单元格 格式
    Aspose单元格格式编号///<summary>///单元格样式编号///0GeneralGeneral///1Decimal0///2Decimal0.00///3Decimal#,##0///4Decimal#,##0.00///5Currency$#,##0;$-#,##0///6Currency$#,##0;[Red]$-#,##0///7Currency$#,##0.00;$-#,#......
  • C# 适配 Maspter 不覆盖填充值
    publicstaticclassMapExtension{publicstaticvoidFill(thisobjectsrc,objectdest){if(src==null||dest==null)return;varsrcType=src.GetType();vardestType=dest.GetType();varproper......
  • CorelDraw (CDR) VBA 实现导出贴图坐标
    创作上位机动画时,喜欢用Corel做画面设计,毕竟不管是亚控还是力控还是wincc,画图都太难受了.贴图动画要贴准的话,最好用坐标精确对齐.所以写了这段代码,用来把Corel中的坐标写入文本文件,做上位机画面时,就可以使用这些坐标进行贴图了.上代码1SubMacro1()2Di......
  • 如何使用Python读取、旋转和和创建空白的PDF文件
    试想象一下,你正在处理一堆PDF文件,需要从中提取一些信息或者修改其中的内容。如果你不使用Python,你可能需要手动打开每个文件,复制粘贴你需要的内容,然后再保存为一个新的文件。这简直是一场噩梦!但是,有了Python,你可以轻松地编写一个脚本来自动化这个过程,节省大量时间和精力。那......
  • Python对PDF文件加密和添加水印大揭秘!
    ​Python这个编程语言,不仅因为它语法简洁易懂,还因为它能帮我解决各种实际问题。最近我就用Python给PDF文件加了密,还添了个酷炫的水印,感觉自己瞬间变成了文件安全的小能手!首先,得说说这个PDF加密。你知道吗,现在网上各种资料满天飞,保护自己的文档不被他人随意查看变得尤为重要......