使用DocXToPdfConverter把Docx转成Pdf文件
1、安装DocXToPdfConverter
Install-Package DocXToPdfConverter
2、下载LibreOffice
https://www.libreoffice.org/download/portable-versions/
下载后安装软件。
3、编写代码进行转换
var fileDialog = new OpenFileDialog(); fileDialog.Filter = "所有文件(*.*)|*.*"; if (fileDialog.ShowDialog() == true) { var fileName = fileDialog.FileName; // initialize LibreOffice soffice.exe filepath string locationOfLibreOfficeSoffice = @"D:\Program Files\LibreOfficePortable\App\libreoffice\program\soffice.exe"; // define placeholders var placeholders = new Placeholders(); placeholders.NewLineTag = "<br/>"; placeholders.TextPlaceholderStartTag = "##"; placeholders.TextPlaceholderEndTag = "##"; placeholders.TablePlaceholderStartTag = "=="; placeholders.TablePlaceholderEndTag = "=="; placeholders.ImagePlaceholderStartTag = "++"; placeholders.ImagePlaceholderEndTag = "++"; // initialize report generator var test = new ReportGenerator(locationOfLibreOfficeSoffice); // convert DOCX to PDF test.Convert(fileName, fileName + ".pdf", placeholders); Process.Start("explorer.exe", "/select," + fileName + ".pdf"); }
其中locationOfLibreOfficeSoffice参数的路径换成你自己LibreOffice的安装路径。