C#pdf转图片并去水印
需要用到这个破解的dll哦,在官网下载的会有个丑丑的水印,破解的网盘提取码给上哈:
链接: https://pan.baidu.com/s/1tIOZrq8FnbPPhHgB2qhJ2w 提取码: bp24
怎么用呢,看这里~
1 public static MemoryStream GetPdfImagePageStream(string pdfInputPath, int pageIndex, ImageFormat format, int width = 1600, int height = 2560, int quality = 10) 2 { 3 try 4 { 5 //pdf处理插件 6 PDFFile pdfFile = PDFFile.Open(pdfInputPath); 7 int total = pdfFile.PageCount; 8 9 #region 防止异常参数 10 if (pageIndex < 0) 11 { 12 pageIndex = 0; 13 } 14 if (pageIndex > total) 15 { 16 pageIndex = total - 1; 17 } 18 if (quality < 1) 19 { 20 quality = 1; 21 } 22 if (quality > 10) 23 { 24 quality = 10; 25 } 26 if (width <= 0) 27 { 28 width = 1; 29 } 30 31 if (height <= 0) 32 { 33 height = 1; 34 } 35 #endregion 36 37 //pdf转换图片 38 SizeF pageSize = pdfFile.GetPageSize(pageIndex); 39 40 Bitmap pageImage = pdfFile.GetPageImage(pageIndex, 56 * quality); 41 42 MemoryStream ms = new MemoryStream(); 43 44 pageImage.Save(ms, format); 45 46 //原图 47 Image img = Image.FromStream(ms, true); 48 49 double ratio = (double)width / (double)height; 50 51 double oRatio = (double)img.Width / (double)img.Height; 52 53 int sbWidth = 0; 54 55 int sbHeight = 0; 56 57 int outX = 0; 58 int outY = 0; 59 60 if (oRatio < ratio) 61 { 62 sbWidth = (int)(img.Width * ((double)height / (double)(img.Height))); 63 sbHeight = height; 64 65 outX = (width - sbWidth) / 2; 66 } 67 else 68 { 69 sbHeight = (int)(img.Height * ((double)width / (double)(img.Width))); 70 sbWidth = width; 71 72 outY = (height - sbHeight) / 2; 73 } 74 75 //缩放 76 Image sbImg = new Bitmap(sbWidth, sbHeight); 77 Graphics sbGra = Graphics.FromImage(sbImg); 78 sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 79 sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 80 sbGra.Clear(Color.White); 81 sbGra.DrawImage(img, new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel); 82 83 //补白 84 Image outImg = new System.Drawing.Bitmap(width, height); 85 Graphics outGra = System.Drawing.Graphics.FromImage(outImg); 86 outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 87 outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 88 outGra.Clear(Color.White); 89 outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel); 90 91 MemoryStream outMs = new MemoryStream(); 92 93 outImg.Save(outMs, format); 94 95 sbImg.Dispose(); 96 outImg.Dispose(); 97 img.Dispose(); 98 99 return outMs; 100 101 } 102 catch (Exception ex) 103 { 104 105 } 106 107 return new MemoryStream(); 108 }
PS:这是从别的大佬那里看到的,今天整理笔记看到了但忘了大佬emmm,,,大佬的方法简单的提供了几个功能,我实际只用到了图片的转换,图片格式什么的没多做调整。。。
PDFFile file = PDFFile.Open(file.FullName); for (int i = 0; i < file.Pages.Count; i++)
{ var img = file.GetPageImage(i, 648);// 逐页生成图片,648是清晰度 img.Save(filename); img.Dispose(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人