C#pdf转图片并去水印

发布时间 2023-05-04 10:12:05作者: 小魔仙变身

需要用到这个破解的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(); }