• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c# PDF转jpg

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

C#和Java的现成的程序都有,用C++的有,是调用Acrobat中的文件自己编写的,但不是生成图片,只是用VC显示,网址是: http://www.evget.com/zh-CN/Info/catalog/6594.html 有个用C#实现的网址是: http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx 这里面有具体的步骤及如何调用别人的库,但C++使用起来不太好办。 这里有一个pdflib的操作,可以操作页,也是C#的: http://www.codeproject.com/KB/graphics/AnotherPDFLib.aspx下面还有用C++如何调用C#的:

http://blog.csdn.net/suoxd123/archive/2010/01/08/5157668.aspx

 

下面提供一个完整的事例代码:

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

namespace PDFLibSharp
{
    public class PDFLibSharp
    {
        private string filename;
        int page;

        /// <summary>
        /// the construct function to create the filename and page
        /// </summary>
        /// <param name="filenamesource"></param>
        /// <param name="pagesource"></param>       
        public int ConvertPtoI(string filenamesource, int pagesource)
        {
            filename = filenamesource;
            page = pagesource;
            //assert the file is pdf or not
            if(!(filename.EndsWith(".pdf") || filename.EndsWith(".PDF")))
            {
                return 0;  //0 represent there is no file
            }               

            //begin to change
            try
            {
                Acrobat.CAcroPDDoc pdfDoc;
                pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
                bool ret = pdfDoc.Open(filename);
                if(!ret)
                {
                    return 1;  //1 can not open to read
                }

                //the page is too big
                int pageCount = pdfDoc.GetNumPages();
                if(page >= pageCount || page <= 0)
                {
                    return 2;
                }

                //get the page and the size
                Acrobat.CAcroPDPage pdfPage;
                pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(page);
                Acrobat.CAcroPoint pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
                Acrobat.CAcroRect pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

                pdfRect.Left = 0;
                pdfRect.right = pdfPoint.x;
                pdfRect.Top = 0;
                pdfRect.bottom = pdfPoint.y;

                pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);
                IDataObject clipboardData = Clipboard.GetDataObject();

                if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);

                    int thumbnailWidth = 1000;
                    int thumbnailHeight = 1000;
                 
                    Image pdfImage = pdfBitmap.GetThumbnailImage(thumbnailWidth, thumbnailHeight,null, IntPtr.Zero);
 
                    Bitmap thumbnailBitmap = new Bitmap(thumbnailWidth + 7, thumbnailHeight + 7,System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnailBitmap))
                    {
                        thumbnailGraphics.DrawImage(pdfImage, 2, 2, thumbnailWidth, thumbnailHeight);
                        string outputFile = filename.Substring(0, filename.Length - 4) + \'_\' + page.ToString() + ".png";
                        thumbnailBitmap.Save(outputFile, System.Drawing.Imaging.ImageFormat.Png);
                        Console.WriteLine("Generated thumbnail... {0}", outputFile);
                    }
                    pdfDoc.Close();

                    Marshal.ReleaseComObject(pdfPage);
                    Marshal.ReleaseComObject(pdfRect);
                    Marshal.ReleaseComObject(pdfDoc);
                }

            }
            catch (System.Exception ex)
            {
                return 10;
            }
            return 5;
           
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#了解如何使用泛型列表类型管理数据集合发布时间:2022-07-10
下一篇:
Java进击C#——语法之基础发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap