前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF中使用Aspose进行文档处理

WPF中使用Aspose进行文档处理

作者头像
码客说
发布2021-08-05 11:11:19
1K0
发布2021-08-05 11:11:19
举报
文章被收录于专栏:码客

安装

代码语言:javascript
复制
Install-Package Aspose.PDF -Version 19.1.0
Install-Package Aspose.Slides.NET -Version 19.10.0
Install-Package Aspose.Words -Version 19.1.0

激活

代码语言:javascript
复制
byte[] license = Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4xOTA4MjYwODA3NTM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+M2U0NGRlMzAtZmNkMi00MTA2LWIzNWQtNDZjNmEzNzE1ZmMyPC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjAwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT53UGJtNUt3ZTYvRFZXWFNIY1o4d2FiVEFQQXlSR0pEOGI3L00zVkV4YWZpQnd5U2h3YWtrNGI5N2c2eGtnTjhtbUFGY3J0c0cwd1ZDcnp6MytVYk9iQjRYUndTZWxsTFdXeXNDL0haTDNpN01SMC9jZUFxaVZFOU0rWndOQkR4RnlRbE9uYTFQajhQMzhzR1grQ3ZsemJLZFZPZXk1S3A2dDN5c0dqYWtaL1E9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=");
new Aspose.Words.License().SetLicense(new MemoryStream(license));
new Aspose.Pdf.License().SetLicense(new MemoryStream(license));
new Aspose.Slides.License().SetLicense(new MemoryStream(license));

转换

PPT转PDF

代码语言:javascript
复制
//PPT转PDF
Presentation ppt = new Presentation(filepath);
ppt.Save(path, Aspose.Slides.Export.SaveFormat.Pdf);

PDF转图片

代码语言:javascript
复制
// 图片绝对路径集合
List<string> images = new List<string>();
string directoryPath = pathTemp;

//定义Jpeg转换设备
Aspose.Pdf.Document document = new Aspose.Pdf.Document(path);
Aspose.Pdf.Devices.JpegDevice device = new Aspose.Pdf.Devices.JpegDevice();

Directory.CreateDirectory(directoryPath);
//默认质量为100,设置质量的好坏与处理速度成反比
device = new Aspose.Pdf.Devices.JpegDevice(100);
//遍历每一页转为jpg
for (int i = 1; i <= document.Pages.Count; i++)
{
    long ii = Timestamp();
    string filePathOutPut = Path.Combine(directoryPath, string.Format("{0}{1}.jpg", ii, i));
    images.Add(filePathOutPut);
    FileStream fs = new FileStream(filePathOutPut, FileMode.OpenOrCreate);
    try
    {
        device.Process(document.Pages[i], fs);
        fs.Close();
    }
    catch (Exception)
    {
        fs.Close();
        File.Delete(filePathOutPut);
    }
}

Word转图片

代码语言:javascript
复制
using Aspose.Words.Saving;

using Common.system;

using System;
using System.Collections.Generic;
using System.IO;

namespace XHWK.WKTool.Utils
{
    class ZAsposeUtil
    {
        /// <summary>
        /// 将Word文档转换为图片的方法(该方法基于第三方DLL),
        /// 你可以像这样调用该方法: ConvertPDF2Image("F:\\PdfFile.doc", "F:\\","ImageFile", 1, 20);
        /// </summary>
        /// <param name="pdfInputPath">Word文件路径</param>
        /// <param name="imageOutputPath">图片输出路径,如果为空,默认值为Word所在路径</param>
        /// <param name="imageName">图片的名字,不需要带扩展名,如果为空,默认值为Word的名称</param>
        /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
        /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数</param>
        /// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param>
        /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
        public static List<string> ConvertWordToImage(
            string wordInputPath,
            string imageOutputPath,
            string imageName,
            int startPageNum = 0,
            int endPageNum = 0,
            Aspose.Words.SaveFormat imageFormat = Aspose.Words.SaveFormat.Png,
            float resolution = 128
            )
        {
            // 返回的图片绝对路径集合
            List<string> images = new List<string>();
            try
            {
                // open word file
                Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);

                // validate parameter
                if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); }
                if (imageOutputPath.Trim().Length == 0) { imageOutputPath = System.IO.Path.GetDirectoryName(wordInputPath); }
                if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
                if (imageName.Trim().Length == 0) { imageName = System.IO.Path.GetFileNameWithoutExtension(wordInputPath); }
                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 (resolution <= 0) { resolution = 128; }

                ImageSaveOptions imageSaveOptions = new ImageSaveOptions(imageFormat)
                {
                    Resolution = resolution
                };

                // start to convert each page
                for (int i = startPageNum; i <= endPageNum; i++)
                {
                    imageSaveOptions.PageIndex = i - 1;
                    string savepath = System.IO.Path.Combine(imageOutputPath, imageName) + "_" + i + "." + imageFormat.ToString();
                    doc.Save(savepath, imageSaveOptions);
                    images.Add(savepath);
                }
                imageSaveOptions = null;
                doc = null;
            }
            catch (Exception ex)
            {
                MessageWindow.Show("该文件无法使用!");
                LogHelper.WriteErrLog("【导入方法(ConvertWordToImage)】错误日志:" + ex.Message, ex);
            }
            return images;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-07-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • 转换
    • PPT转PDF
      • PDF转图片
        • Word转图片
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档