Word PDF转图片

    private void ToImage(string fileInputPath, out List<string> imgPaths)
    {
        fileInputPath = MapPath(fileInputPath);
        imgPaths = new List<string>();
        MemoryStream stream = new MemoryStream();
        if (Path.GetExtension(fileInputPath).ToLower().Contains("doc") || Path.GetExtension(fileInputPath).ToLower().Contains("docx"))
        {

            WordToImage(fileInputPath, "/temp/", 0, 0, 0, out imgPaths);
        }
        else if (Path.GetExtension(fileInputPath).ToLower().Contains("pdf"))
        {
            PDFToImage(fileInputPath, "/temp/", 0, 0, 0, out imgPaths);
        }
        else
        {

        }

        //Response.ClearContent();
        //Response.ContentType = "image/jpeg";
        //Response.BinaryWrite(stream.ToArray());
        //stream.Dispose();
        System.IO.File.Delete(Server.MapPath("/a.jpg"));
        //Response.End();

    }
    /// <summary>
    ///  将pdf文档转换为图片的方法  
    /// </summary>
    /// <param name="originFilePath">pdf文件路径</param>  
    /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>  
    /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为pdf总页数</param>         
    /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>  
    /// <returns></returns>
    private void PDFToImage(string originFilePath, string newImagePath, int startPageNum, int endPageNum, int resolution, out List<string> imagePaths)
    {
        imagePaths = new List<string>();
        try
        {
            Aspose.Pdf.Document doc = new Aspose.Pdf.Document(originFilePath);
            if (resolution <= 0)
            {
                resolution = 128;
            }
            string imageNamePrefix = Path.GetFileNameWithoutExtension(originFilePath);
            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }

            if (endPageNum > doc.Pages.Count || endPageNum <= 0)
            {
                endPageNum = doc.Pages.Count;
            }

            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;
            }

            if (resolution <= 0)
            {
                resolution = 128;
            }
            if (!System.IO.Directory.Exists(Server.MapPath(newImagePath)))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(newImagePath));
            }
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                MemoryStream stream = new MemoryStream();
                Aspose.Pdf.Devices.Resolution reso = new Aspose.Pdf.Devices.Resolution(resolution);
                Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(reso, 100);
                jpegDevice.Process(doc.Pages[i], stream);
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
                Bitmap bm = new Bitmap(img);
                var name = newImagePath + DateTime.Now.Ticks + ".png";
                imagePaths.Add(name);
                bm.Save(Server.MapPath(name), ImageFormat.Png);
            }
        }
        catch (Exception ex)
        {
        }
    }

    private void WordToImage(string originFilePath, string newImagePath, int startPageNum, int endPageNum, int resolution, out List<string> imagePaths)
    {
        imagePaths = new List<string>();
        try
        {
            Aspose.Words.Document doc = new Aspose.Words.Document(originFilePath);
            if (resolution <= 0)
            {
                resolution = 128;
            }
            string imageNamePrefix = Path.GetFileNameWithoutExtension(originFilePath);

            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }

            if (endPageNum > doc.PageCount || endPageNum <= 0)
            {
                endPageNum = doc.PageCount;
            }

            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;
            }

            if (!System.IO.Directory.Exists(Server.MapPath(newImagePath)))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(newImagePath));
            }

            for (int i = startPageNum; i <= endPageNum; i++)
            {
                MemoryStream stream = new MemoryStream();
                string imageName = Path.GetFileNameWithoutExtension(originFilePath);
                ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.Png);
                imageSaveOptions.PageIndex = i - 1;
                doc.Save(stream, imageSaveOptions);
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
                Bitmap bm = new Bitmap(img);
                var name = newImagePath + DateTime.Now.Ticks + ".png";
                imagePaths.Add(name);
                bm.Save(Server.MapPath(name), ImageFormat.Png);


            }
        }
        catch (Exception ex)
        {
        }

    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼儿哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值