当前位置: 首页 > news >正文

c# 系列pdf转图片 各种处理2--net3.1到net8

net3.1 pdf转图片目前只有两种

Acrobat.dll

要准备的:下载Adobe Acrobat Professional 9

添加引用:Microsoft.CSharp.dll,Acrobat.dll 和Microsoft.VisualBasic.dll

/// <summary>
/// 将PDF文档转换为图片的方法,你可以像这样调用该方法:ConvertPDF2Image("F:\\A.pdf", "F:\\", "A", 0, 0, null, 0);
/// 因为大多数的参数都有默认值,startPageNum默认值为1,endPageNum默认值为总页数,
/// imageFormat默认值为ImageFormat.Jpeg,resolution默认值为1
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出路径</param>
/// <param name="imageName">图片的名字,不需要带扩展名</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换,默认值为1</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换,默认值为PDF总页数</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="resolution">设置图片的分辨率,数字越大越清晰,默认值为1</param>
public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
{
    Acrobat.CAcroPDDoc pdfDoc = null;
    Acrobat.CAcroPDPage pdfPage = null;
    Acrobat.CAcroRect pdfRect = null;
    Acrobat.CAcroPoint pdfPoint = null;
 
    // Create the document (Can only create the AcroExch.PDDoc object using late-binding)
    // Note using VisualBasic helper functions, have to add reference to DLL
    pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
 
    // validate parameter
    if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); }
    if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
    if (startPageNum <= 0) { startPageNum = 1; }     if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }     if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
    if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; }
    if (resolution <= 0) { resolution = 1; }
 
    // start to convert each page
    for (int i = startPageNum; i <= endPageNum; i++)
    {
        pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
        pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
        pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
 
        int imgWidth = (int)((double)pdfPoint.x * resolution);
        int imgHeight = (int)((double)pdfPoint.y * resolution);
 
        pdfRect.Left = 0;
        pdfRect.right = (short)imgWidth;
        pdfRect.Top = 0;
        pdfRect.bottom = (short)imgHeight;
 
        // Render to clipboard, scaled by 100 percent (ie. original size)
        // Even though we want a smaller image, better for us to scale in .NET
        // than Acrobat as it would greek out small text
        pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));
 
        IDataObject clipboardData = Clipboard.GetDataObject();
 
        if (clipboardData.GetDataPresent(DataFormats.Bitmap))
        {
            Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
            pdfBitmap.Save(Path.Combine(imageOutputPath, imageName) + ".jpg", imageFormat);
            pdfBitmap.Dispose();
        }
    }
 
    pdfDoc.Close();
    Marshal.ReleaseComObject(pdfPage);
    Marshal.ReleaseComObject(pdfRect);
    Marshal.ReleaseComObject(pdfDoc);
    Marshal.ReleaseComObject(pdfPoint);
}

注意:转换速度、图片质量都还不错,但是无法跨平台使用。

Ghostscript.dll 功能很强大,也能跨平台,但是必须安装 gs10031w64.exe,请加大酌情使用

https://download.csdn.net/download/zuoming120/90575414?spm=1001.2014.3001.5501 这个是资源包

net8 有两种产品

1  Docnet.Core

https://download.csdn.net/download/zuoming120/90575430 这个是我的测试demo

有些时候图片清晰度不够。但是可用

2 itext7.pdfimage

下载 https://github.com/thombrink/itext7.pdfimage

新建core8项目,安装itext7和system.drawing.common

修改 imageListener下有不安全的代码

  unsafe
  {
      for (int y = 0; y < image.Height; y++)
      {
          byte* ptrMask = (byte*)bitsMask.Scan0 + y * bitsMask.Stride;
          byte* ptrInput = (byte*)bitsInput.Scan0 + y * bitsInput.Stride;
          byte* ptrOutput = (byte*)bitsOutput.Scan0 + y * bitsOutput.Stride;
          for (int x = 0; x < image.Width; x++)
          {
              ptrOutput[4 * x] = ptrInput[4 * x];           // blue
              ptrOutput[4 * x + 1] = ptrInput[4 * x + 1];   // green
              ptrOutput[4 * x + 2] = ptrInput[4 * x + 2];   // red
              ptrOutput[4 * x + 3] = ptrMask[4 * x];        // alpha
          }
      }
  }

改成 

// 遍历每一行  
for (int y = 0; y < image.Height; y++)  
{  
    // 计算每行的起始字节位置  
    int scanOffsetInput = y * bitsInput.Stride;  
    int scanOffsetMask = y * bitsMask.Stride;  
    int scanOffsetOutput = y * bitsOutput.Stride;  

    // 获取当前行的字节数组  
    byte[] scanlineInput = new byte[bitsInput.Stride];  
    byte[] scanlineMask = new byte[bitsMask.Stride];
    byte[] scanlineOutput = new byte[bitsOutput.Stride];
    Marshal.Copy(bitsInput.Scan0 + scanOffsetInput, scanlineInput, 0, scanlineInput.Length);  
    Marshal.Copy(bitsMask.Scan0 + scanOffsetMask, scanlineMask, 0, scanlineMask.Length);  

    // 遍历每一列(或每一像素)  
    for (int x = 0; x < image.Width; x++)  
    {  
        // 计算当前像素在字节数组中的位置(基于每个像素4个字节)  
        int pixelOffsetInput = x * 4;  
        int pixelOffsetMask = x * 4;  
        int pixelOffsetOutput = x * 4;  

        // 读取输入图像的RGB值  
        byte blue = scanlineInput[pixelOffsetInput];  
        byte green = scanlineInput[pixelOffsetInput + 1];  
        byte red = scanlineInput[pixelOffsetInput + 2];  

        // 读取遮罩图像的alpha值  
        byte alpha = scanlineMask[pixelOffsetMask];  

        // 将值写入输出图像的对应位置  
        scanlineOutput[pixelOffsetOutput] = blue;  
        scanlineOutput[pixelOffsetOutput + 1] = green;  
        scanlineOutput[pixelOffsetOutput + 2] = red;  
        scanlineOutput[pixelOffsetOutput + 3] = alpha;  
    }  

    // 将处理过的行复制回输出图像的BitmapData中  
    Marshal.Copy(scanlineOutput, 0, bitsOutput.Scan0 + scanOffsetOutput, scanlineOutput.Length);  
}  

然后运行两个demo,就可以了,切记必须是net8项目

相关文章:

  • OpenCV鼠标事件
  • TDengine 与 taosAdapter 的结合(二)
  • Elasticsearch8.x集成SpringBoot3.x
  • AI Agent开发大全第十九课-神经网络入门 (Tensorflow)
  • Oracle数据库数据编程SQL<8.1 文本编辑器Notepad++和UltraEdit(UE)对比>
  • UE5 蓝图里的反射
  • 进程内存分布--之smaps呈现memory-layout.cpp内存分布
  • RK3568下GPIO模拟串口
  • 详解数据结构线性表 c++实现
  • 算法进阶指南 货仓选址
  • redis导入成功,缺不显示数据
  • 华为ensp中路由器IPSec VPN原理及配置命令(超详解)
  • Python爬虫教程013:使用CrawlSpider爬取读书网数据并保存到mysql数据库
  • Windows 系统下用 VMware 安装 CentOS 7 虚拟机超详细教程(包含VMware和镜像安装包)
  • 基于 springboot 的在线考试系统
  • React中的跨组件通信
  • 助力三农服务平台设计与实现
  • NVR接入录像回放平台EasyCVR用智能分析高新技术赋能,打造营业厅人员/物品安全的堡垒
  • 代码随想录算法训练营第五十二天|图论专题: 101. 孤岛的总面积、102. 沉没孤岛、103. 水流问题、104. 建造最大岛屿
  • 接口异常数组基础题
  • 半数以上中国人都缺这几种营养,吃什么能补回来?
  • 中美博弈新阶段,这个“热带中国”火了
  • 从《缶翁的世界》开始,看吴昌硕等湖州籍书画家对海派的影响
  • 广西桂林、百色、河池等地表态:全力配合中央对蓝天立的审查调查
  • 大风+暴雨,中央气象台双预警齐发
  • 新疆多地市民拍到不明飞行物:几秒内加速消失,气象部门回应