C#使用Aspose将Word\HTML 转换成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】

 

posted @ 2022-12-27 16:44  朝闲  阅读(1711)  评论(0编辑  收藏  举报