使用DocXToPdfConverter把Docx轉成Pdf檔案
阿新 • • 發佈:2022-12-02
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 = newReportGenerator(locationOfLibreOfficeSoffice); // convert DOCX to PDF test.Convert(fileName, fileName + ".pdf", placeholders); Process.Start("explorer.exe", "/select," + fileName + ".pdf"); }
其中locationOfLibreOfficeSoffice引數的路徑換成你自己LibreOffice的安裝路徑。