关闭

Word PDF转图片

标签: WordPDFAspose
130人阅读 评论(0) 收藏 举报
分类:
    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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:40031次
    • 积分:814
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:14篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论