C#实现Docx到Pdf的转换

发布时间 2023-09-19 11:22:50作者: wzwyc

1、安装Nuget包

Install-Package DocXToPdfConverter

2、下载LibreOffice软件

下载地址:
https://www.libreoffice.org/download/portable-versions/

因为只用来转换文件用,不需要全语言包,个人建议下载最新的MultilingualStandard版本即可。

下载完成后,安装到指定路径,我测试的时候是装到了“D:\Program Files”目录下。

3、示例代码

总共3句代码:

using DocXToPdfConverter;

namespace ExampleApplication
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            string libreOfficePath = @"D:\Program Files\LibreOfficePortable\App\libreoffice\program\soffice.exe";
            var test = new ReportGenerator(libreOfficePath);
            test.Convert("C#编码规范v0.5.docx", "C#编码规范v0.5.pdf", null);
        }
    }
}

如果是随软件一起发布,个人建议把“D:\Program Files\LibreOfficePortable”复制到应用的根目录,把libreOfficePath设置为相对路径。