zoukankan      html  css  js  c++  java
  • .Net的PDF转图片

    用的是破解版的 O2S.Components.PDFRender4NET.dll 插件, 简单引用即可

        public static class PdfToImage
        {
            public static MemoryStream GetPdfImagePageStream(string pdfInputPath, int pageIndex, ImageFormat format, int width = 1600, int height = 2560, int quality = 10)
            {
                try
                {
                    //pdf处理插件
                    PDFFile pdfFile = PDFFile.Open(pdfInputPath);
                    int total = pdfFile.PageCount;
    
                    #region 防止异常参数
                    if (pageIndex < 0)
                    {
                        pageIndex = 0;
                    }
                    if (pageIndex > total)
                    {
                        pageIndex = total - 1;
                    }
                    if (quality < 1)
                    {
                        quality = 1;
                    }
                    if (quality > 10)
                    {
                        quality = 10;
                    }
                    if (width <= 0)
                    {
                        width = 1;
                    }
    
                    if (height <= 0)
                    {
                        height = 1;
                    }
                    #endregion
    
                    //pdf转换图片
                    SizeF pageSize = pdfFile.GetPageSize(pageIndex);
    
                    Bitmap pageImage = pdfFile.GetPageImage(pageIndex, 56 * quality);
    
                    MemoryStream ms = new MemoryStream();
    
                    pageImage.Save(ms, format);
    
                    //原图
                    Image img = Image.FromStream(ms, true);
    
                    double ratio = (double)width / (double)height;
    
                    double oRatio = (double)img.Width / (double)img.Height;
    
                    int sbWidth = 0;
    
                    int sbHeight = 0;
    
                    int outX = 0;
                    int outY = 0;
    
                    if (oRatio < ratio)
                    {
                        sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));
                        sbHeight = height;
    
                        outX = (width - sbWidth) / 2;
                    }
                    else
                    {
                        sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));
                        sbWidth = width;
    
                        outY = (height - sbHeight) / 2;
                    }
    
                    //缩放
                    Image sbImg = new Bitmap(sbWidth, sbHeight);
                    Graphics sbGra = Graphics.FromImage(sbImg);
                    sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    sbGra.Clear(Color.White);
                    sbGra.DrawImage(img, new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
    
                    //补白
                    Image outImg = new System.Drawing.Bitmap(width, height);
                    Graphics outGra = System.Drawing.Graphics.FromImage(outImg);
                    outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    outGra.Clear(Color.White);
                    outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel);
    
                    MemoryStream outMs = new MemoryStream();
    
                    outImg.Save(outMs, format);
    
                    sbImg.Dispose();
                    outImg.Dispose();
                    img.Dispose();
    
                    return outMs;
    
                }
                catch (Exception ex)
                {
    
                }
    
                return new MemoryStream();
            }
    
            public static MemoryStream GetPdfImagePageStream(Stream stream, int pageIndex, ImageFormat format, int width = 1600, int height = 2560, int quality = 10)
            {
                try
                {
                    //pdf处理插件
                    PDFFile pdfFile = PDFFile.Open(stream);
                    int total = pdfFile.PageCount;
    
                    #region 防止异常参数
                    if (pageIndex < 0)
                    {
                        pageIndex = 0;
                    }
                    if (pageIndex > total)
                    {
                        pageIndex = total - 1;
                    }
                    if (quality < 1)
                    {
                        quality = 1;
                    }
                    if (quality > 10)
                    {
                        quality = 10;
                    }
                    if (width <= 0)
                    {
                        width = 1;
                    }
    
                    if (height <= 0)
                    {
                        height = 1;
                    }
                    #endregion
    
                    //pdf转换图片
                    SizeF pageSize = pdfFile.GetPageSize(pageIndex);
    
                    Bitmap pageImage = pdfFile.GetPageImage(pageIndex, 56 * quality);
    
                    MemoryStream ms = new MemoryStream();
    
                    pageImage.Save(ms, format);
    
                    //原图
                    Image img = Image.FromStream(ms, true);
    
                    double ratio = (double)width / (double)height;
    
                    double oRatio = (double)img.Width / (double)img.Height;
    
                    int sbWidth = 0;
    
                    int sbHeight = 0;
    
                    int outX = 0;
                    int outY = 0;
    
                    if (oRatio < ratio)
                    {
                        sbWidth = (int)(img.Width * ((double)height / (double)(img.Height)));
                        sbHeight = height;
    
                        outX = (width - sbWidth) / 2;
                    }
                    else
                    {
                        sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));
                        sbWidth = width;
    
                        outY = (height - sbHeight) / 2;
                    }
    
                    //缩放
                    Image sbImg = new Bitmap(sbWidth, sbHeight);
                    Graphics sbGra = Graphics.FromImage(sbImg);
                    sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    sbGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    sbGra.Clear(Color.White);
                    sbGra.DrawImage(img, new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
    
                    //补白
                    Image outImg = new System.Drawing.Bitmap(width, height);
                    Graphics outGra = System.Drawing.Graphics.FromImage(outImg);
                    outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    outGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    outGra.Clear(Color.White);
                    outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel);
    
                    MemoryStream outMs = new MemoryStream();
    
                    outImg.Save(outMs, format);
    
                    sbImg.Dispose();
                    outImg.Dispose();
                    img.Dispose();
    
                    return outMs;
    
                }
                catch (Exception ex)
                {
    
                }
    
                return new MemoryStream();
            }
        }
    View Code

     DLL下载: O2S.Components.PDFRender4NET.dll

  • 相关阅读:
    Java中导入、导出Excel
    ExtJS框架基础:事件模型及其常用功能
    sql索引的优缺点
    Sword框架解析——知识采集流程页面初始化
    ObjectMapper处理从远程获取的Object对象 (http://bbs.csdn.net/topics/390337813?page=1)这个网址也有讲解
    SQL语言基本操作(聚合函数)
    Java并发性和多线程介绍目录
    Hibernate中load与get,update与merge方法的区别
    JDK各个版本的新特性jdk1.5-jdk8
    [LintCode] Longest Increasing Continuous subsequence
  • 原文地址:https://www.cnblogs.com/xachary/p/4200761.html
Copyright © 2011-2022 走看看