c# pdf转图片

发布时间:2018-04-19编辑:佚名阅读(1499)

需要添加Acrobat.dll引用,Acrobat.dll来自Adobe Acrobat。

/// <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;
    //生成操作Pdf文件的Com对象
    pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
    //检查输入参数
    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;
    }
    //转换
    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", "");
        //计算当前页经缩放后的实际宽度和高度,zoom==1时,保持原比例大小
        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;
        //将当前页的裁剪区的内容编成图片后复制到剪贴板中
        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()) + ".jpg", imageFormat);
            pdfBitmap.Dispose();
        }
    }
    //关闭和释放相关COM对象
    pdfDoc.Close();
    Marshal.ReleaseComObject(pdfPage);
    Marshal.ReleaseComObject(pdfRect);
    Marshal.ReleaseComObject(pdfDoc);
    Marshal.ReleaseComObject(pdfPoint);
}


  关键字:c#pdf转图片


鼓掌

0

正能量

0

0

呵呵

0


评论区