winForm 读取pdf文件并,转换成图片

      最近弄了一个项目,需要读取本机的pdf文件并转换成图片放置到另外一个文件夹下,现在把代码分享下:

     一、看看如何读取本机的pdf文件到窗体中

      首先,我们下载一个pdf阅读AdbeRdr器到电脑上,最好是安装完整版的,要不然vs获得到组件

      其次,我们就可以在工具箱中获取Adobe提供的Active控件了

         

       

     

     然后我们就可以拖动一个控件放到窗体中了,然后会生成两个文件

    

       然后我们就可以通过代码来读取pdf文件,下面这个例子选择pdf文件夹,然后读取多个pdf文件通过按钮完成切换pdf文件和翻页

     

   #region 选择pdf文件目录
        private void btnBrowse_Click(object sender, EventArgs e)
        {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                pdfLoad();

            }
        }
        #endregion

        #region 读取pdf文件
        private void pdfLoad()
        {
            txtFile.Text = folderBrowserDialog1.SelectedPath;
            DirectoryInfo directoryInfo = new DirectoryInfo(txtFile.Text);
            string strFile = txtFile.Text.Trim();
            files = directoryInfo.GetFiles();
            //获得文件夹里的所有pdf文件
            alist = new ArrayList();
            for (int n = 0; n < files.Count(); n++)
            {
                string filename = files[n].ToString();
                string piff = filename.Substring(filename.LastIndexOf("."));
                if (piff == ".pdf")
                {
                    alist.Add(filename);
                }
            }
            lblMessage.Visible = true;
            lblMessage.Text = "提示:此目录共有pdf文件" + alist.Count + "";
            if (alist.Count > 0)
            {

                panel1.Visible = true;

                i = 0;
                axAcroPDF1.LoadFile(txtFile.Text + "\\" + alist[i].ToString());
            
            }
            else
            {

                             panel1.Visible = false;

            }
        }
        #endregion

也可以使用代码创建Adobe PDF Reader组件

 

View Code
string fileName = MyOpenFileDialog(); 
AxAcroPDFLib.AxAcroPDF axAcroPDF = new AxAcroPDFLib.AxAcroPDF(); 
axAcroPDF.Location = new System.Drawing.Point(024); 
axAcroPDF.Size = new System.Drawing.Size(292242); 
axAcroPDF.Dock = DockStyle.Fill; 
Controls.Add(axAcroPDF); 
axAcroPDF.LoadFile(fileName); 

在编译的时候,VS会Adobe PDF Reader ActiveX组件转换为2个.net组件:AxInterop.AcroPDFLib.dll和Interop.AcroPDFLib.dll

    

所以在写代码创建Adobe PDF Reader 组件的时候,需要手动把Adobe PDF Reader   ActiveX组件转换为.net组件并引用!最好的办法是,托一个Adobe PDF Reader 组件到窗体上,然后删除,这样就不需要手动了!

其他的切换和翻页都很好实现,这里就不在多说了

 

二、下面看看如何将pdf文件转换成图片格式

      在项目中添加lib文件,用来添加gsdll32.dll  、PDFLibNet.dll  、PDFView.dll三个文件

      下载:/Files/shuang121/pdf三个文件.rar

     项目添加PDFLibNet.dll和PDFView.dll两个引用,并将gsddll32.dll拷贝到bin目录下面即可

     下面就可以直接选择一个pdf文件进行转换了

    

View Code
/// <summary>
        
/// 将PDF 相应的页转换为图片
        
/// </summary>
        
/// <param name="strPDFpath">PDF 路径</param>
        
/// <param name="Page">需要转换的页页码</param>
        private void GetImage(string strPDFpath,int Page)
        {
            FileInfo file=new FileInfo (strPDFpath);
            string strSavePath=file.Directory.FullName;
            byte[] ImgData = GetImgData(strPDFpath, Page);
            MemoryStream ms = new MemoryStream(ImgData, 0, ImgData.Length);
            Bitmap returnImage = (Bitmap)Bitmap.FromStream(ms);
            string strImgPath=Path.Combine(strSavePath,string.Format("PDFConvert{0}.jpg",Page));
            returnImage.Save(strImgPath);
        }
        /// <summary>
        
/// 从PDF中获取首页的图片
        
/// </summary>
        
/// <param name="PDFPath">PDF 文件路径</param>
        
/// <param name="Page">需要获取的第几页</param>
        
/// <returns></returns>
        private byte[] GetImgData(string PDFPath,int Page)
        {
            System.Drawing.Image img = PDFView.ConvertPDF.PDFConvert.GetPageFromPDF(PDFPath,Page, 300""true);
            return GetDataByImg(img);//读取img的数据并返回
        }
        /// <summary>
        
/// 将单页的PDF转换为图片
        
/// </summary>
        
/// <param name="_image"></param>
        
/// <returns></returns>
        private byte[] GetDataByImg(System.Drawing.Image _image)
        {
            System.IO.MemoryStream Ms = new MemoryStream();
            _image.Save(Ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imgdata = new byte[Ms.Length];
            Ms.Position = 0;
            Ms.Read(imgdata, 0, Convert.ToInt32(Ms.Length));
            Ms.Close();
            return imgdata;
        }

        string Pdfpath = "";
        /// <summary>
        
/// 选择需要转换的PDF
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Pdfpath = openFileDialog1.FileName;
            }
            else
            {
                Pdfpath = "";
            }
            textBox1.Text = Pdfpath;
        }
        /// <summary>
        
/// 转换
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Pdfpath))
            {
                GetImage(Pdfpath,2);
            }
        }
    }
}
 

 

 以上就是整个读取和转换的过程

 

posted @ 2012-12-01 14:23  双魂人生  阅读(6822)  评论(7编辑  收藏  举报