爱编程 - WORD
type: channel
link: https://masoft.cn/tag/WORD/
items: C#调用WPS转换Word、Excel、PPT文件为PDF

C#调用WPS转换Word、Excel、PPT文件为PDF
type: item
date: 2019-06-11T15:56:00+08:00
description: C#调用WPS转换Word、Excel、PPT文件为PDF，耗时稍微长一点，但是文件转换格式不存在问题。需要安装WPSusing System; using Excel; using PowerPoint; using Word; namespace SharePint.Code { public class FileTransform { /// /// Word转PDF /// /// word文件地址 /// 保存PDF地址 /// public static bool WordToPdf(string pWordFile, string pSavePdf) { var type = Type.GetTypeFromProgID("KWps.Application"); dynamic wps = Activator.CreateInstance(type); try { var doc = wps.Documents.Open(pWordFile, Visible: false); doc.ExportAsFixedFormat(pSavePdf, WdExportFormat.wdExportFormatPDF); doc.Close(); } catch (Exception e) { return false; } finally { wps.Quit(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return true; } /// /// Excel转PDF /// /// Excel文件地址 /// 保存PDF地址 /// public static bool ExcelToPdf(string pExcelFile, string pSavePdf) { var type = Type.GetTypeFromProgID("KET.Application"); dynamic wps = Activator.CreateInstance(type); try { var targetType = XlFixedFormatType.xlTypePDF; var missing = Type.Missing; var doc = wps.Application.Workbooks.Open(pExcelFile, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); doc.ExportAsFixedFormat(targetType, pSavePdf, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); doc.Close(); } catch (Exception e) { return false; } finally { wps.Quit(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return true; } /// /// PPT转PDF /// /// PPT文件地址 /// 保存PDF地址 /// public static bool PptToPdf(string pPptFile, string pSavePdf) { var type = Type.GetTypeFromProgID("KWPP.Application"); dynamic wps = Activator.CreateInstance(type); try { var doc = wps.Presentations.Open(pPptFile, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoCTrue); doc.SaveAs(pSavePdf, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue); doc.Close(); } catch (Exception e) { return false; } finally { wps.Quit(); GC.Collect(); GC.WaitF…