基本信息
源码名称:批量pdf文件转jpg图片
源码大小:7.51M
文件格式:.zip
开发语言:C#
更新时间:2017-08-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

指定目录下的所有pdf文件批量转成jpg图片,生成日志。


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;

namespace PdfToImage
{
    public delegate int CallBack(int nMin, int nMax, int nCur);
   

    public class Pdf2Image
    {
        [DllImport("PDFToX.dll")]
        private static extern int OpenPDF(string szPDFFileName, string szPWD);
        [DllImport("PDFToX.dll")]
        private static extern int ClosePDF(int hPDF);
        [DllImport("PDFToX.dll")]
        private static extern int GetPageNum(int hPDF);
        [DllImport("PDFToX.dll")]
        private static extern int GetPageWidth(int hPDF, int lPage);
        [DllImport("PDFToX.dll")]
        private static extern int GetPageHeight(int hPDF, int lPage);
        [DllImport("PDFToX.dll")]
        private static extern int ExportIMG(int hPDF, string szDir, string szPrefix,
                    int nFormatA, int dpiX, int dpiY, int nFrom, int nTo, CallBack pfun);
        [DllImport("PDFtoX.dll", EntryPoint = "SetRCPath")]
        private static extern int SetRCPath(string rcPath);

        public static int Report(int nMin, int nMax, int nCur)
        {
            Console.Write("总共");
            Console.Write(nMax);
            Console.Write("页,当前第");
            Console.Write(nCur);
            Console.WriteLine("页。");
            return 1;
        }
        public static int PDFConvert(string strPdfFile, string strSavePath, string strPrename)
        {
            int pages=0;
            CallBack myCallBack = new CallBack(Report);
            //防止转换后乱码,设置解晰路径
            string strRcPath = Application.StartupPath;
            if (!strRcPath.EndsWith("\\"))
            {
                strRcPath = "\\RC";
            }
            //MessageBox.Show(strRcPath);
            SetRCPath(strRcPath);

            int hPDF = OpenPDF(strPdfFile, "");//第二个参数是密码

            if (hPDF > 0)
            {
                int nFrom = 1;
                int nTo = GetPageNum(hPDF); //获得PDF总页数
                pages = nTo;
                ExportIMG(
                    hPDF,           //PDF文件句柄
                    strSavePath,    //目标路径
                    strPrename,          //生成的文件名前缀,Pre00001.jpg
                    2,              //文件格式 BMP:1,JPG:2,TIF:3,PNG:4,GIF:5,PCX:6,TGA:7
                    150,             //图像分辨率,常用的是72或96,但一般不能超过1000
                    150,             //图像分辨率,常用的是72或96,但一般不能超过1000
                    nFrom,          //要转换的起始页
                    nTo,            //要转换的终止页
                    myCallBack      //回调函数
                    );
                int nWidth = GetPageWidth(hPDF, 1);
                int nHeight = GetPageHeight(hPDF, 1);
                ClosePDF(hPDF);
            }
            return pages;
        }

        //string dir;

        //ListFiles(new DirectoryInfo(dir));


        public static void ListFiles(FileSystemInfo info)
        {
           

            if (!info.Exists) return;

            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录
            if (dir == null) return;

            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i )
            {
                FileInfo file = files[i] as FileInfo;
                //是文件
                if (file != null)
                {
                    PdftoJpgNum = PdftoJpgNum 1;
                   
                    //MessageBox.Show(file.Name.Substring(0,file.Name.Length-4));
                    //MessageBox.Show(file.Name.Substring(file.Name.Length-11, file.Name.Length-8));
                    //Console.WriteLine(file.FullName "\t " file.Length);
                    if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".pdf")
                    //此处为显示JPG格式,不加IF可遍历所有格式的文件
                    {
                        //this.list1.Items.Add(file);
                        //MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
                        int p=Pdf2Image.PDFConvert(file.FullName, file.DirectoryName,file.Name.Substring(0,file.Name.Length-4));
                        whritelog(file.FullName "(" p ")");
                       
                    }
                    //if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg")
                    //{
                    //    if (file.Name.IndexOf(".pdf0")>0)
                    //    {
                    //        file.Delete();
                    //    }
                    //}
                   
                   
                }
                //对于子目录,进行递归调用
                else
                {
                    ListFiles(files[i]);
                }

            }
           

        }


        //日志
        public static void whritelog(string msg)
        {
           
            StreamWriter sw = new StreamWriter("txtwriter.txt", true);
            sw.WriteLine(msg);
            sw.Close();//写入
        }

        public static int PdftoJpgNum;
       

 

    }
}