• C#中PDF转图片


    把 O2S.Components.PDFRender4NET.dll(下载地址,密码hep9) 添加到引用中

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Windows.Forms;
     5 using O2S.Components.PDFRender4NET;
     6 using System.Drawing;
     7 using System.Drawing.Imaging;
     8 using System.IO;
     9 namespace O2S.Components.PDFRender4NET.pdf2image
    10 {
    11     public static class Program
    12     {
    13         public enum Definition
    14         {
    15             One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
    16         }
    17         /// <summary>
    18         /// 将PDF文档转换为图片的方法
    19         /// </summary>
    20         /// <param name="pdfInputPath">PDF文件路径</param>
    21         /// <param name="imageOutputPath">图片输出路径</param>
    22         /// <param name="imageName">生成图片的名字</param>
    23         /// <param name="startPageNum">从PDF文档的第几页开始转换</param>
    24         /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
    25         /// <param name="imageFormat">设置所需图片格式</param>
    26         /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
    27         public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
    28             string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
    29         {
    30             PDFFile pdfFile = PDFFile.Open(pdfInputPath);
    31             if (!Directory.Exists(imageOutputPath))
    32             {
    33                 Directory.CreateDirectory(imageOutputPath);
    34             }
    35             // validate pageNum
    36             if (startPageNum <= 0)
    37             {
    38                 startPageNum = 1;
    39             }
    40             if (endPageNum > pdfFile.PageCount)
    41             {
    42                 endPageNum = pdfFile.PageCount;
    43             }
    44             if (startPageNum > endPageNum)
    45             {
    46                 int tempPageNum = startPageNum;
    47                 startPageNum = endPageNum;
    48                 endPageNum = startPageNum;
    49             }
    50             // start to convert each page
    51             for (int i = startPageNum; i <= endPageNum; i++)
    52             {
    53                 Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
    54                 pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);
    55                 pageImage.Dispose();
    56             }
    57             pdfFile.Dispose();
    58         }
    59         public static void Main(string[] args)
    60         {
    61             ConvertPDF2Image("F:\Events.pdf", "F:\", "A", 1, 5, ImageFormat.Jpeg, Definition.One);
    62         }
    63     }
    64 }
  • 相关阅读:
    Java后端面试题大汇总,冲刺金三银四
    面试官:小伙子,Mybatis的本质和原理说一下
    面试官问:大量的 TIME_WAIT 状态 TCP 连接,对业务有什么影响?怎么处理?
    便捷搭建 Zookeeper 服务器的方法,好用,收藏~
    10 个冷门但又非常实用的 Docker 使用技巧
    mitmproxy 抓包工具(1)
    基于alpine创建Scrapy镜像
    强大的输入框-应用快速启动uTools
    Interceptor、Filter、Servlet的区别
    利用三层判断sql数据库中编码是否已经存在(个人拙作,不喜勿喷)
  • 原文地址:https://www.cnblogs.com/Cein/p/7280812.html
Copyright © 2020-2023  润新知