首页 / C# / C#调用WPS将文档转换成pdf进行预览
C#调用WPS将文档转换成pdf进行预览
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#调用WPS将文档转换成pdf进行预览,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6209字,纯文字阅读大概需要9分钟。
内容图文
![C#调用WPS将文档转换成pdf进行预览](/upload/InfoBanner/zyjiaocheng/628/e5f749ea5b0f4fdf897c64fdaa45a17a.jpg)
vs启动项目可以生成wps实例
本地iis部署的站点却不行
原因是vs是管理员权限,而iis没有权限
解决方法
启动IIS,应用程序池-“选定的应用程序池”-高级设置-进程模型-标识:设置为管理员账号administrator
?
![C#调用WPS将文档转换成pdf进行预览 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501064815909.jpg)
代码
1.安装WPS 2016 专业版
2.方法一:在项目中引用etapi.dll,wpsapi.dll,wppapi.dll,在WPS的安装目录中,如C:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6666\office6
方法二:根据实际需要科添加下面的COM引用
原文:https://blog.csdn.net/xqf222/article/details/81237915
添加引用 -> COM -> Kingsoft Add-In Designer
添加引用 -> COM -> Microsoft Office 11.0 Object Library
添加引用 -> COM -> Upgrade WPS Office 3.0 Object Library(Beta)
添加引用 -> COM -> Upgrade WPS Presentation 3.0 Object Library(Beta)
添加引用 -> COM -> Upgrade Kingsoft WPS 3.0 Object Library(Beta)
添加引用 -> COM -> Kingsoft WPS Extend Apo 1.0 Object Library(Beta)
public class ToPdfHelper : IDisposable
{
dynamic wps;
public ToPdfHelper(string typeName)
{
if (typeName == "xls")
typeName = "KET.Application";
else if (typeName == "ppt")
typeName = "KWPP.Application";
else
typeName = "KWps.Application";
//创建wps实例,需提前安装wps
Type type = Type.GetTypeFromProgID(typeName);
if (type == null)
type = Type.GetTypeFromProgID("wps.Application");
wps = Activator.CreateInstance(type);
}
/// <summary>
/// 使用wps将Word转PDF
/// </summary>
/// <param name="saveUrl">文件路径</param>
/// <param name="targetPath">源文件路径</param>
/// <returns></returns>
public string WordWpsToPdf(string saveUrl, string targetPath)
{
if (targetPath == null)
{
throw new ArgumentNullException("wpsFilename");
}
var wordPath = saveUrl + targetPath;
var pdfPath = Path.ChangeExtension(wordPath, "pdf");
try
{
//用wps 打开word不显示界面
dynamic doc = wps.Documents.Open(wordPath, Visible: false);
//doc 转pdf
doc.ExportAsFixedFormat(pdfPath, WdExportFormat.wdExportFormatPDF);
//设置隐藏菜单栏和工具栏
//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
doc.Close();
doc = null;
}
catch (Exception e)
{
targetPath = GetEXCELtoPDF.CreatePDFs(saveUrl, targetPath);
}
finally
{
Dispose();
}
return Path.ChangeExtension(targetPath, "pdf");
}
/// <summary>
/// 使用wps将xls转PDF
/// </summary>
/// <param name="saveUrl">文件路径</param>
/// <param name="targetPath">源文件路径</param>
/// <returns></returns>
public string XlsWpsToPdf(string saveUrl, string targetPath)
{
if (targetPath == null)
{
throw new ArgumentNullException("wpsFilename");
}
var wordPath = saveUrl + targetPath;
var pdfPath = Path.ChangeExtension(wordPath, "pdf");
try
{
XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
//xls 转pdf
dynamic doc = wps.Application.Workbooks.Open(wordPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
doc.ExportAsFixedFormat(targetType, pdfPath, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
//设置隐藏菜单栏和工具栏
//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
doc.Close();
doc = null;
}
catch (Exception e)
{
targetPath = GetEXCELtoPDF.CreatePDFs(saveUrl, targetPath);
}
finally
{
Dispose();
}
return Path.ChangeExtension(targetPath, "pdf");
}
/// <summary>
/// 使用ppt将xls转PDF
/// </summary>
/// <param name="saveUrl">文件路径</param>
/// <param name="targetPath">源文件路径</param>
/// <returns></returns>
public string PptWpsToPdf(string saveUrl, string targetPath)
{
if (targetPath == null)
{
throw new ArgumentNullException("wpsFilename");
}
var wordPath = saveUrl + targetPath;
var pdfPath = Path.ChangeExtension(wordPath, "pdf");
try
{
//ppt 转pdf
dynamic doc = wps.Presentations.Open(wordPath, MsoTriState.msoCTrue,
MsoTriState.msoCTrue, MsoTriState.msoCTrue);
object missing = Type.Missing;
//doc.ExportAsFixedFormat(pdfPath, PpFixedFormatType.ppFixedFormatTypePDF,
// PpFixedFormatIntent.ppFixedFormatIntentPrint,
// MsoTriState.msoCTrue, PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst,
// PpPrintOutputType.ppPrintOutputBuildSlides,
// MsoTriState.msoCTrue, null, PpPrintRangeType.ppPrintAll,"",
// false, false, false, false, false, missing);
doc.SaveAs(pdfPath, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
//设置隐藏菜单栏和工具栏
//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
doc.Close();
doc = null;
}
catch (Exception e)
{
targetPath = GetEXCELtoPDF.CreatePDFs(saveUrl, targetPath);
}
finally
{
Dispose();
}
return Path.ChangeExtension(targetPath, "pdf");
}
public void Dispose()
{
if (wps != null) { wps.Quit(); wps = null; }
}
}
内容总结
以上是互联网集市为您收集整理的C#调用WPS将文档转换成pdf进行预览全部内容,希望文章能够帮你解决C#调用WPS将文档转换成pdf进行预览所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。