1. 程式人生 > 其它 >使用DocXToPdfConverter把Docx轉成Pdf檔案

使用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的安裝路徑。