找回密码
 加入
搜索
查看: 10889|回复: 17

[系统综合] C# 改 Au3

  [复制链接]
发表于 2014-9-25 10:57:15 | 显示全部楼层 |阅读模式
using System;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace Acrobat.pdf2image
{
    public class Program
    {
        /// <summary>
        /// 将PDF文档转换为图片的方法,你可以像这样调用该方法:ConvertPDF2Image("F:\\A.pdf", "F:\\", "A", 0, 0, null, 0);
        /// 因为大多数的参数都有默认值,startPageNum默认值为1,endPageNum默认值为总页数,
        /// imageFormat默认值为ImageFormat.Jpeg,resolution默认值为1
        /// </summary>
        /// <param name="pdfInputPath">PDF文件路径</param>
        /// <param name="imageOutputPath">图片输出路径</param>
        /// <param name="imageName">图片的名字,不需要带扩展名</param>
        /// <param name="startPageNum">从PDF文档的第几页开始转换,默认值为1</param>
        /// <param name="endPageNum">从PDF文档的第几页开始停止转换,默认值为PDF总页数</param>
        /// <param name="imageFormat">设置所需图片格式</param>
        /// <param name="resolution">设置图片的分辨率,数字越大越清晰,默认值为1</param>
        public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
            string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
        {
            Acrobat.CAcroPDDoc pdfDoc = null;
            Acrobat.CAcroPDPage pdfPage = null;
            Acrobat.CAcroRect pdfRect = null;
            Acrobat.CAcroPoint pdfPoint = null;

            // Create the document (Can only create the AcroExch.PDDoc object using late-binding)
            // Note using VisualBasic helper functions, have to add reference to DLL
            pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

            // validate parameter
            if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); }
            if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
            if (startPageNum <= 0) { startPageNum = 1; }
            if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }
            if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
            if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; }
            if (resolution <= 0) { resolution = 1; }

            // start to convert each page
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
                pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
                pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

                int imgWidth = (int)((double)pdfPoint.x * resolution);
                int imgHeight = (int)((double)pdfPoint.y * resolution);

                pdfRect.Left = 0;
                pdfRect.right = (short)imgWidth;
                pdfRect.Top = 0;
                pdfRect.bottom = (short)imgHeight;

                // 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
                pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));

                IDataObject clipboardData = Clipboard.GetDataObject();

                if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
                    pdfBitmap.Save(Path.Combine(imageOutputPath, imageName) + i.ToString() + "." + imageFormat.ToString(), imageFormat);
                    pdfBitmap.Dispose();
                }
            }

            pdfDoc.Close();
            Marshal.ReleaseComObject(pdfPage);
            Marshal.ReleaseComObject(pdfRect);
            Marshal.ReleaseComObject(pdfDoc);
            Marshal.ReleaseComObject(pdfPoint);

        }

        [STAThread]
        public static void Main(string[] args)
        {
            ConvertPDF2Image("F:\\Events.pdf", "F:\\", "A", 0, 0, null, 0);
        }

    }
}
发表于 2014-9-25 21:59:10 | 显示全部楼层
楼主你啥也不说,是想让我们把你这段C#的转换成AU3么? 好歹描述下吧。
 楼主| 发表于 2014-9-26 09:00:52 | 显示全部楼层
回复 2# MicroBlue


    不好意思,这段代码是C# 调用一个dll,实现PDF转图片,或者有其他办法吗?
 楼主| 发表于 2014-9-26 12:33:25 | 显示全部楼层
给自己顶一下
 楼主| 发表于 2014-9-28 12:55:49 | 显示全部楼层
给自己顶一下  貌似要沉了
发表于 2014-9-28 13:22:48 | 显示全部楼层
太高深了,只能飘过,等待高手来吧!
 楼主| 发表于 2014-9-29 11:01:59 | 显示全部楼层
这个没人可以解决吗?向上推一下
 楼主| 发表于 2014-9-29 16:04:39 | 显示全部楼层
没事就推一下,坐等大神
 楼主| 发表于 2014-9-30 09:48:42 | 显示全部楼层
每日一推,坐等大神
发表于 2014-9-30 10:27:10 | 显示全部楼层
推一推 推一推
发表于 2014-9-30 21:58:48 | 显示全部楼层
好像很厉害的样子啊,汗。顶帖
发表于 2014-10-2 16:47:28 | 显示全部楼层
回复 12# 樱花雪月
 楼主| 发表于 2014-10-8 09:19:53 | 显示全部楼层
推一推 推一推
 楼主| 发表于 2014-10-10 09:18:48 | 显示全部楼层
我可以帮自己推一下吗
 楼主| 发表于 2014-10-13 12:58:43 | 显示全部楼层
还给自己顶一下,希望能解决
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-2 16:04 , Processed in 0.082394 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表