首页 > 编程语言 >C#使用Aspose将Word\HTML 转换成PDF文件

C#使用Aspose将Word\HTML 转换成PDF文件

时间:2022-12-27 16:58:49浏览次数:48  
标签:Word string C# Aspose error Words new PDF

写在前面

  1. Aspose 这个是收费的,直接使用是有水印的
  2. 需要用到的dll文件 ==> Aspose.Words.dll、Aspose.HTML.dll、Aspose.Total.lic(授权文件)
  3. 我使用的是.NET Framework 4.0 ,.NET Core 使用nuget安装

Word转换成PDF

  1、引用 Aspose.Words.dll  文件 

    

  2、授权。先将 Aspose.Total.lic 添加到项目中,然后设置文件属性

    

  3、转换代码

public static class PdfHelper
    {
        static PdfHelper()
        { 
            Aspose.Words.License license = new Aspose.Words.License();
            license.SetLicense("Aspose.Total.lic"); //授权
        } 

        /// <summary>
        /// word 转换成 pdf 文件
        /// </summary>
        /// <param name="sourcePath">原文件</param>
        /// <param name="targetPath">转换后的pdf存储路径</param>
        /// <param name="error"></param>
        /// <returns></returns>
        private static string WordToPdf(string sourcePath, string targetPath)
        {
            string error = "";
            try
            {
                using (FileStream localFileStream = new FileInfo(sourcePath).Open(FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var opt = new Aspose.Words.LoadOptions();
                    opt.Encoding = Encoding.UTF8;
                    var doc = new Aspose.Words.Document(localFileStream, opt);

                    //如果word文档有自己安装的字体需要追加一下,不然转换出来的pdf文件字体格式会错误
                    string userfontsfoloder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Microsoft\\Windows\\Fonts\\";

                    ArrayList fontSources = new ArrayList(Aspose.Words.Fonts.FontSettings.DefaultInstance.GetFontsSources());
                    //将用户目录字体添加到字体源中
                    Aspose.Words.Fonts.FolderFontSource folderFontSource = new Aspose.Words.Fonts.FolderFontSource(userfontsfoloder, true);
                    fontSources.Add(folderFontSource);
                    Aspose.Words.Fonts.FontSourceBase[] updatedFontSources = (Aspose.Words.Fonts.FontSourceBase[])fontSources.ToArray(typeof(Aspose.Words.Fonts.FontSourceBase));
                    Aspose.Words.Fonts.FontSettings.DefaultInstance.SetFontsSources(updatedFontSources);

                    //doc.Sections[0].PageSetup.PageWidth = 2479 / 2.5;
                    //doc.Sections[0].PageSetup.PageHeight = 3508 / 2.5;
                    //doc.Sections[0].PageSetup.OddAndEvenPagesHeaderFooter = false;
                    doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
                }
            }
            catch (Exception ex)
            {
                error = "转换pdf失败,error:" + ex.Message;
            }
            return error;
        }  
    }

HTML转换成PDF

1、引用Aspose.HTML.dll文件

  参考Word转换PDF步骤1

2、授权

  参考Word转换PDF步骤2

3、转换代码

  在Word转换PDF的Helper类里添加代码

  在构造函数追加授权代码

static PdfHelper()
{ 
    Aspose.Words.License license = new Aspose.Words.License();
    license.SetLicense("Aspose.Total.lic"); //授权
    Aspose.Html.License licenseHtml = new Aspose.Html.License();
    licenseHtml.SetLicense("Aspose.Total.lic"); //授权
} 

  新增HTML转换PDF代码

private static string HtmlToPdf(string sourcePath, string targetPath)
{
    string error = "";
    try
    {
        var doc = new Aspose.Html.HTMLDocument(sourcePath);
        var opt = new Aspose.Html.Saving.PdfSaveOptions();
        Aspose.Html.Converters.Converter.ConvertHTML(doc, opt, targetPath);
    }
    catch (Exception ex)
    {
        error = "转换pdf失败,error:" + ex.Message;
    }
    return error;
}

4、如果html 转换成pdf文件格式有问题也可以尝试使用【Select.HtmlToPdf】

 

标签:Word,string,C#,Aspose,error,Words,new,PDF
From: https://www.cnblogs.com/Zing/p/17008194.html

相关文章

  • Selenium32-PageObject模式
    代码分层原因公司小:一个人负责所有工作→业务简单,代码量少,单文件公司大:分多个部门,分工协作→业务复杂,代码量大,多文件PageObject模式引入PageObject的原因......
  • ASEMI肖特基二极管MBR30100CT和MBR40200PT有什么区别
    编辑-Z别看ASEMI肖特基二极管MBR30100CT和MBR40200PT两种型号从名字上看很像,其实他们的参数和封装都是不一样的,具体MBR30100CT和MBR40200PT有什么区别呢? 肖特基二极管M......
  • Activity的创建
    Activity的创建:1.layout内写入相关代码此处为显示的页面  2.Java内创建相关类写入代码  3.在清单内写入   快捷方法:直接完成上面步骤  layout:......
  • java try...catch
    解释:    ......
  • AcWing361. 观光奶牛
    传送门题目描述给定一张\(L\)个点、\(P\)条边的有向图,每个点都有一个权值\(f[i]\),每条边都有一个权值\(t[i]\)。求图中的一个环,使“环上各点的权值之和”除以“环......
  • centos7部署dotnetcore webpai
    1.进入宝塔ssh2.安装dotnetcoresdksudoyuminstalldotnet-sdk-6.03.上传文件4.新建一个网站,网址www.localhost8892.com:88925.设置反向代理,填写目标URL:http://lo......
  • fc设备识别使用
    1.存储映射2.multipath下载启用yuminstalldevice-mapper-multipath/sbin/mpathconf--enable  ......
  • Vue3 企业级优雅实战 - 组件库框架 - 9 实现组件库 cli - 上
    上文搭建了组件库cli的基础架子,实现了创建组件时的用户交互,但遗留了cli/src/command/create-component.ts中的createNewComponent函数,该函数要实现的功能就是上文开......
  • 如何在 Manjaro Linux 上安装 ONLYOFFICE 桌面编辑器
    ​​ONLYOFFICE桌面编辑器​​是一款免费开源办公套件,其中包括适用于文本文档、电子表格与演示文稿的离线编辑器。同时,您还可将应用程序连接至云端(ONLYOFFICE、ONLYOFFICE......
  • 欧标插头CE认证EN50075测试项目
    欧标插头在德国、奥地利、荷兰、瑞典、挪威、芬兰、俄罗斯等大部分欧洲国家使用,由于这个标准(EN50075)在整个欧洲普遍使用,我们把它称为是“欧洲大陆”的标准。​欧标插头E......