pdf转图片

2016/5/9 16:43:33

   

说明:本方法 用  acrobat 的官方接口 


需要安装  acrobat   professional  8.0或更高版本然后再vs中添加com组件  abode  acrobat  版本号   type  library  只有安装了上面软件才会有。


web页 头部 添加   AspCompat="true"    控制台程序 要在main函数上  [STAThread]  保证单线程模型下 才能访问 剪贴板

using System.IO;
using System.Reflection;
using DataHelp;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

/// <summary>

        /// Pdf导图片

        /// </summary>

        /// <param name="pdfInputPathDirectory"></param>

        /// <param name="pngOutputPath"></param>

        /// <param name="format"></param>

        /// <returns></returns>

        

        public string PDFToPic(string pdfInputPathDirectory, string pngOutputPath, ImageFormat format)

        {

            StringBuilder b = new StringBuilder();

            ImageFormat formate = format;

            // Acrobat objects

            Acrobat.CAcroPDDoc pdfDoc = null;

            Acrobat.CAcroPDPage pdfPage = null;

            Acrobat.CAcroRect pdfRect = null;

            Acrobat.CAcroPoint pdfPoint = null;

            try

            {

                string[] files = new string[] { pdfInputPathDirectory };

                string dic = Path.GetDirectoryName(pdfInputPathDirectory);

                //string[] files = Directory.GetFiles(dic, "*.pdf");

                for (int n = 0; n < files.Length; n++)

                {

                    string inputFile = files[n].ToString();

                    string fileName = files[n].Substring(files[n].LastIndexOf(@"\") + 1).Replace(".pdf", "");

                    // Will always be available as .NET framework ships with all

                    pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

                    bool ret = pdfDoc.Open(inputFile);

                    if (!ret)

                    {

                        throw new FileNotFoundException();

                    }

                    else

                    {

 

                        b = b.AppendLine("<span>pdfDoc成功打开文件!!!</span>");

                    }

                    // Get the number of pages (to be used later if you wanted to store that information)

                    int pageCount = pdfDoc.GetNumPages();

                    b = b.AppendLine("<ul style='azimuth:center; list-style-type:none;' >");

 

 

                    for (int i = 0; i < pageCount; i++)

                    {

                        // Get the first page

                        pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);

 

                        pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();

                        b = b.AppendLine("<li>  <span>pdfPoint.x=" + pdfPoint.x + ";pdfPoint.y=" + pdfPoint.y + "</span>   </li>");

                        pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

                        pdfRect.Left = 0;

                        pdfRect.right = pdfPoint.x;

                        pdfRect.Top = 0;

                        pdfRect.bottom = pdfPoint.y;

                        // 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

                        // see http://www.adobe.com/support/techdocs/1dd72.htm

                        //清空剪贴板

                        //Clipboard.Clear();

                       //b = b.AppendLine("<li>  <span>Clipboard.Clear();清空剪贴板 </span>   </li>");

 

                       pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);

 

                        //b = b.AppendLine("<li>  <span>pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)=" +pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)+ ";复制到剪贴板</span>   </li>");

 

 

                        System.Windows.Forms.IDataObject clipboardData = Clipboard.GetDataObject();

                        bool c = false;

                        if (clipboardData!=null )

                        {

                            c = true;

                        }

 

 

                        b = b.AppendLine("<li>  <span> Clipboard.GetDataObject()=" + c + "剪贴板中是否有数据;</span>   </li>");

 

 

 

                       b = b.AppendLine("<li>  <span>clipboardData.GetDataPresent(DataFormats.Bitmap)=" + clipboardData.GetDataPresent(DataFormats.Bitmap) + ";</span>   </li>");

 

                        if (clipboardData.GetDataPresent(DataFormats.Bitmap))

                        {

                            Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);

 

                            b = b.AppendLine("<li>  <span>生成图片的大小: pdfBitmap.Size.Height=" + pdfBitmap.Size.Height + "; pdfBitmap.Size.Width=" + pdfBitmap.Size.Width + "</span>   </li>");

 

 

                           

                            string dPath = Path.Combine(pngOutputPath, i.ToString("0000") + "." + formate.ToString());

 

                            b = b.AppendLine("<li>  <span>生成图片的路径:" + dPath + ";</span>   </li>");

 

 

                            pdfBitmap.Save(

                           dPath, formate);

                            pdfBitmap.Dispose();

                        }

                        b = b.AppendLine("<li>  <img src='..\\" + imgDire + "\\" + i.ToString("0000") + "." + formate.ToString() + "'  />       </li><span>" + (i + 1) + "</span>");

                    }

 

                    pdfDoc.Close();

                    // Not sure how why it is to do this, but Acrobat is not the best behaved COM object

                    // see http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx

                    Marshal.ReleaseComObject(pdfPage);

                    Marshal.ReleaseComObject(pdfRect);

                    Marshal.ReleaseComObject(pdfDoc);

                }

                b = b.AppendLine("</ul>");

            }

            catch (System.Exception ex)

            {

                Response.Write(ex.ToString());

            }

            return b.ToString();

        }