C#wps转pdf(word、ppt、excel),在线预览pdf
wps转pdf
注:我是在wps试⽤期专业版,windows10系统 vs2019 webform( 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);
excel最强教科书完全版pdfdoc.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
以前⼀直搜索不到“在线预览pdf”⽅法,后来在使⽤Aspose时发现了在线预览的秘密,跟⼤家分享下。 注意:在线预览pdf主要使针对⽐较现代化的浏览器。
后台输出pdf时要设置输出
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论