Convert Office documents to pdf format (2)
In the previous article, we used Microsoft Office components to convert Word, Excel, and Powerpoint into pdf format. In this article, we will use the WPS Office component for conversion. The steps are as follows:
① Add relevant references to WPS components
Note: wpsapi.dll corresponds to the Word file API; etapi.dll corresponds to the Excel file API; wppapi corresponds to the PPT file API;
②Writing Office Help Class
public class WPSOfficeHelper { ////// Convert Word to pdf file , suitable for (.doc, .docx, .mht, .htm file types) /// /// Source file /// Target file /// public static bool WordToPdf(string sourceFileName, string targetFileName) { Word.Application wordApp = newWord.Application(); Word._Document wordDoc = null; try { wordApp.Visible = false; wordDoc = wordApp.Documents.Open(sourceFileName, false, true); wordDoc.ExportAsFixedFormat(targetFileName, Word.WdExportFormat.wdExportFormatPDF); return true span>; } catch (Exception ex ) { return false span>; } finally { if (wordDoc != null) { wordDoc.Close(false); wordDoc = null; } if (wordApp != null) { wordApp.Quit(false); wordApp = null; } } } /// /// Excel converted to pdf file /// /// Source file /// Target file /// public static bool ExcelToPdf(string sourceFileName, string targetFileName) { Excel.Application excelApp = new Excel.Application(); Excel._Workbook excelDoc = null; try { excelApp.Visible = false; excelDoc = excelApp.Workbooks.Open(sourceFileName, false, true); excelDoc.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, targetFileName); return true span>; } catch (Exception ex ) { return false span>; } finally { if (excelDoc != null) { excelDoc.Close(false); excelDoc = null; } if (excelApp != null) { excelApp.Quit(); excelApp = null; } } } /// /// Convert PPT to pdf file /// /// Source file /// Target file /// public static bool PPTToPdf(string sourceFileName, string targetFileName) { PowerPoint.Application pptApp = new PowerPoint.Application(); PowerPoint.Presentation pptDoc = null; try { pptDoc = pptApp.Presentations.Open(sourceFileName); pptDoc.ExportAsFixedFormat(targetFileName,PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF); return true span>; } catch (Exception ex ) { return false span>; } finally { if (pptDoc != null) { pptDoc.Close(); pptDoc = null; } if (pptApp != null) { pptApp.Quit(); pptApp = null; } } } }
View Code
Finally, you can call it for conversion.
Note:
①This method is currently only available for Windows systems
②This method relies on WPS Office software
③Can be used in both .net framework and .net core projects (taking the Win Form project as an example)
rgba(128, 128, 128, 1)”>
///
public static bool PPTToPdf(string sourceFileName, string targetFileName)
{
PowerPoint.Application pptApp = new PowerPoint.Application();
PowerPoint.Presentation pptDoc = null;
try
{
pptDoc = pptApp.Presentations.Open(sourceFileName);
pptDoc.ExportAsFixedFormat(targetFileName,PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
return true span>;
}
catch (Exception ex )
{
return false span>;
}
finally
{
if (pptDoc != null)
{
pptDoc.Close();
pptDoc = null;
}
if (pptApp != null)
{
pptApp.Quit();
pptApp = null;
}
}
}
}
View Code
Finally, you can call it for conversion.
Note:
①This method is currently only available for Windows systems
②This method relies on WPS Office software
③Can be used in both .net framework and .net core projects (taking the Win Form project as an example)