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)
        {
        }

    }

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_17255515/article/details/62429985
文章标签: Word PDF Aspose
个人分类: C#
上一篇jquery 获取当前浏览器
下一篇SQL中把单引号拼接到一列
想对作者说点什么? 我来说一句

wordpdf图片完美保存不丢失!

2011年06月02日 6.85MB 下载

Delphi实现WordPDF文档转Image

2016年03月09日 373KB 下载

没有更多推荐了,返回首页

关闭
关闭