注:我是在wps试用期专业版,windows10系统 vs2019 webform(.net framework4.5)测试。
前提:需要下载安装wps专业版、企业版。
项目中需要引用wps的com组件
com组件Upgrade WPS Spreadsheets 3.0 Object Library (Beta) ,对应“Excel”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Etapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Etapi.dll
com组件 Upgrade WPS Presentation 3.0 Object Library (Beta),对应“PowerPoint”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wppapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wppapi.dll
com组件Upgrade Kingsoft WPS 3.0 Object Library (Beta),对应“Word”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wpsapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wpsapi.dll
public static class WpsToPdf
{
/// <summary>
/// word转pdf
/// </summary>
/// <param name="source">源<see cref="string"/>.</param>
/// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
/// <returns>The <see cref="bool"/>.</returns>
public static bool WordWpsToPdf(string source, string newFilePath)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (newFilePath == null) throw new ArgumentNullException(nameof(newFilePath));
var type = Type.GetTypeFromProgID("KWps.Application");
dynamic wps = Activator.CreateInstance(type);
try
{
//用wps打开word不显示界面
dynamic doc = wps.Documents.Open(source, Visible: false);
//转pdf
doc.ExportAsFixedFormat(newFilePath, WdExportFormat.wdExportFormatPDF);
//设置隐藏菜单栏和工具栏
//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
doc.Close();
}
catch (Exception e)
{
//添加你的日志代码
return false;
}
finally
{
wps.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return true;
}
/// <summary>
/// excel转pdf
/// </summary>
/// <param name="source">源<see cref="string"/>.</param>
/// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
/// <returns>The <see cref="bool"/>.</returns>
public static bool ExcelToPdf(string source, string newFilePath)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (newFilePath == null) throw new ArgumentNullException(nameof(newFilePath));
var type = Type.GetTypeFromProgID("KET.Application");
dynamic wps = Activator.CreateInstance(type);
try
{
var targetType = XlFixedFormatType.xlTypePDF;
var missing = Type.Missing;
//转pdf
var doc = wps.Application.Workbooks.Open(source, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
doc.ExportAsFixedFormat(targetType, newFilePath, XlFixedFormatQuality.xlQualityStandard, true, false,
missing, missing, missing, missing);
doc.Close();
}
catch (Exception e)
{
//添加你的日志代码
return false;
}
finally
{
wps.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return true;
}
/// <summary>
/// ppt转pdf
/// </summary>
/// <param name="source">源<see cref="string"/>.</param>
/// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
/// <returns>The <see cref="bool"/>.</returns>
public static bool PptToPdf(string source, string newFilePath)
{
var type = Type.GetTypeFromProgID("KWPP.Application");
dynamic wps = Activator.CreateInstance(type);
try
{
//转pdf
var doc = wps.Presentations.Open(source, MsoTriState.msoCTrue, MsoTriState.msoCTrue,
MsoTriState.msoCTrue);
doc.SaveAs(newFilePath, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
doc.Close();
}
catch (Exception e)
{
//添加你的日志代码
return false;
}
finally
{
wps.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return true;
}
}
以前一直搜索不到“在线预览pdf”方法,后来在使用Aspose时发现了在线预览的秘密,跟大家分享下。
注意:在线预览pdf主要使针对比较现代化的浏览器。

后台输出pdf时要设置输出

C# wps转pdf(word、ppt、excel),在线预览pdf
原文:https://www.cnblogs.com/youlicc/p/12699412.html