C# 将PDF文档转换成图片

在网上找遍了有关 C# 如何将PDF文档转换成图片的资料,最终在CSDN上面得到了一位高手的指点,根据他的指点,我做了这个项目,希望这个项目能对和我之前有同样需求的人有所帮助!

1、项目中用到了GhostScript,有关GhostScript的详细说明,请参考:http://downloads.sourceforge.net/ghostscript/gs861w32.exe?modtime=1196280996&big_mirror=1

2、在运行本项目前,你必须确保在本地计算机上已安装GhostScript,至于GhostScript的安装目录你可随意选择,但在项目中的配置文件中的也要和你的安装目录同步。

3、根目录中的 gs871w32.exe 为 GhostScript 安装文件,可直点击安装到本地计算机。

4、根目录中还有 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll、IKVM.Runtime.dll、FontBox-0.1.0-dev.dll 这四个程序集文件,它们是PDFBox(有关PDFBox的详细说明请参考:http://sourceforge.net/projects/pdfbox/)中的程序集文件,主要用于辅助操作pdf文档,因为使用PDFBox将PDF文档转为图片的.net版本目前还有些BUG,所以在项目中用了GhostScript来实现转换。请将 IKVM.GNU.Classpath.dll、PDFBox-0.7.3.dll引入项目中,再把 IKVM.Runtime.dll、FontBox-0.1.0-dev.dll置于运行目录下,具体请看项目及其代码。

 

项目很简单,下面贴出了主要的代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Diagnostics;
using org.pdfbox.util;
using org.pdfbox.pdmodel;

 

        #region 转换PDF文档

        ///


        /// 将PDF文档转换成文本
        ///

        /// PDF文档物理路径
        /// 返回从PDF文档剥离的文本
        public string PdfToText(string pdfFile)
        {
            PDDocument doc = PDDocument.load(pdfFile);
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(doc);
        }

        ///


        /// 将PDF文档转换成图片
        ///

        /// PDF文档物理路径
        /// 转换成的图片文件的存放物理路径
        /// 转换成图片文件以后是否删除原PDF文档
        /// 返回转换成的图片文件物理路径的集合
        public IList PdfToImages(string pdfFile, string imgPath, bool isDeletePDF)
        {
            IList imgList = new List();
            PDDocument doc = PDDocument.load(pdfFile);
            int pageCount = doc.getDocumentCatalog().getAllPages().size();//计算pdf文档的总页数

            string pdfFileName = Path.GetFileName(pdfFile);
            int index = pdfFileName.LastIndexOf('.');
            if (index != -1)
                pdfFileName = pdfFileName.Substring(0, index);

            string imgFile = Path.Combine(imgPath, pdfFileName);//转换成的图片文件

            if (pageCount == 0) return null;
            if (pageCount == 1)
            {
                imgFile += ".jpg";
                imgList.Add(imgFile);
                if (File.Exists(imgFile)) File.Delete(imgFile);
            }
            else
            {
                for (int i = 0; i < pageCount; i++)
                {
                    string _imgFile = imgFile + (i + 1).ToString() + ".jpg";
                    imgList.Add(_imgFile);
                    if (File.Exists(_imgFile)) File.Delete(_imgFile);
                }
                imgFile += "%d.jpg";
            }

            ProcessStartInfo info = new ProcessStartInfo();
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"];
            info.Arguments = System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + "  " + pdfFile;
            info.FileName = @"gswin32c.exe";
            Process subProcess = new Process();
            subProcess.StartInfo = info;
            subProcess.Start();
            subProcess.WaitForExit(int.MaxValue);
            if (isDeletePDF)
            {
                File.Delete(pdfFile);
            }

            return imgList;
        }

        #endregion

 

代码中会用到的配置信息:


  
  
 

 

简单吧,下面请看看本项目运行的效果图:

 

 注:完整项目解决方案我已上传为资源,欢迎大家前去下载使用!

你可能感兴趣的:(Asp.net,文档,c#,string,subprocess,.net,null)