安装wps


创建.net core控制项目
添加com引用,搜索wps



准备word,名字叫001.docx

word转pdf
编写代码
namespace wpsstu01
{
internal class program
{
static void main(string[] args)
{
console.writeline("转化开始");
var inputfile = "001.docx";
var outputfile = "001.pdf";
wordexportaspdf(inputfile, outputfile);
console.writeline("转化成功");
console.readkey();
}
/// <summary>
/// 转换为pdf文件,适合(.doc、.docx、.mht、.htm文件类型)
/// </summary>
/// <param name="filename"></param>
/// <param name="outputfilename"></param>
/// <returns></returns>
public static string wordexportaspdf(string filename, string outputfilename)
{
string issucceed = "ok";
word.wdexportformat fileformat = word.wdexportformat.wdexportformatpdf;
word.application wordapp = null;
if (wordapp == null) wordapp = new word.application();
word._document worddoc = null;
try
{
worddoc = wordapp.documents.open(filename, false, true);
worddoc.exportasfixedformat(outputfilename, fileformat);
}
catch (exception ex)
{
issucceed = ex.message;
}
finally
{
if (worddoc != null)
{
worddoc.close(false);
worddoc = null;
}
if (wordapp != null)
{
wordapp.quit(false);
wordapp = null;
}
}
return issucceed;
}
}
}启动项目报错

选择一下32位程序

发现还是不行,最后换成.net framework 4.8的控制台项目
添加dll的引用,dll需要去安装的wps里面查找

console.writeline("转化开始");
var exepath = system.appdomain.currentdomain.basedirectory;
var inputfile = path.combine(exepath, "001.docx");
var outputfile = path.combine(exepath, "001.pdf");
wordexportaspdf(inputfile, outputfile);
console.writeline("转化成功");
console.readkey();


asp.net core也可以问题根本原因是路径的问题,不能些相对路径,必须绝对路径

excel转pdf
/// <summary>
/// excel转换为pdf文件
/// </summary>
/// <param name="filename"></param>
/// <param name="outputfilename"></param>
/// <returns></returns>
public static string excelexportaspdf(string filename, string outputfilename)
{
string issucceed = "ok";
excel.application excelapp = null;
if (excelapp == null)
excelapp = new excel.application();
excel.workbook workbook = null;
try
{
workbook = excelapp.workbooks.open(filename, false, true);
workbook.exportasfixedformat(excel.xlfixedformattype.xltypepdf,outputfilename);
}
catch (exception ex)
{
issucceed = ex.message;
}
finally
{
if (workbook != null)
{
workbook.close(false);
workbook = null;
}
if (excelapp != null)
{
excelapp.quit();
excelapp = null;
}
}
return issucceed;
}调用
console.writeline("转化开始");
var exepath = system.appdomain.currentdomain.basedirectory;
var inputfile = path.combine(exepath, "002.xls");
var outputfile = path.combine(exepath, "002.pdf");
excelexportaspdf(inputfile, outputfile);
console.writeline("转化成功");
console.readkey();ppt转pdf
/// <summary>
/// ppt转换为pdf文件
/// </summary>
/// <param name="filename"></param>
/// <param name="outputfilename"></param>
/// <returns></returns>
public static string pptexportaspdf(string filename, string outputfilename)
{
string issucceed = "ok";
powerpoint.application pptapp = null;
if (pptapp == null)
pptapp = new powerpoint.application();
powerpoint.presentation presentation = null;
try
{
presentation = pptapp.presentations.open(filename);
presentation.exportasfixedformat(outputfilename,powerpoint.ppfixedformattype.ppfixedformattypepdf);
}
catch (exception ex)
{
issucceed = ex.message;
}
finally
{
if (pptapp != null)
{
presentation.close();
pptapp = null;
}
if (pptapp != null)
{
pptapp.quit();
pptapp = null;
}
}
return issucceed;
}调用
console.writeline("转化开始");
var exepath = system.appdomain.currentdomain.basedirectory;
var inputfile = path.combine(exepath, "003.pptx");
var outputfile = path.combine(exepath, "003.pdf");
pptexportaspdf(inputfile, outputfile);
console.writeline("转化成功");
console.readkey();到此这篇关于asp.net core 调用wps实现word转pdf的文章就介绍到这了,更多相关asp.net core word转pdf内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论