当前位置: 代码网 > it编程>编程语言>Asp.net > asp.net core调用wps实现word转pdf的过程

asp.net core调用wps实现word转pdf的过程

2024年09月08日 Asp.net 我要评论
安装wpshttps://www.wps.cn/创建.net core控制项目添加com引用,搜索wps准备word,名字叫001.docxword转pdf编写代码namespace wpsstu01

安装wps

https://www.wps.cn/

创建.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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com