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

 更新时间:2024年08月16日 09:51:56   作者:假装我不帅  
这篇文章主要介绍了asp.net core调用wps实现word转pdf的过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

安装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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • asp.net中倒计时自动跳转页面的实现方法(使用javascript)

    asp.net中倒计时自动跳转页面的实现方法(使用javascript)

    本篇文章介绍了,asp.net中倒计时自动跳转页面的实现方法(使用javascript)。需要的朋友参考下
    2013-05-05
  • Visual Studio 2017新版发布 更强大!

    Visual Studio 2017新版发布 更强大!

    Visual Studio 2017新版发布 更强大!对Visual Studio 2017感兴趣的小伙伴们可以参考一下
    2017-05-05
  • visual studio 2012安装配置方法图文教程 附opencv配置教程

    visual studio 2012安装配置方法图文教程 附opencv配置教程

    这篇文章主要为大家分享了visual studio 2012安装配置方法图文教程,文中附opencv配置教程,文中安装步骤介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • 更改.NET中的默认时区的操作方法

    更改.NET中的默认时区的操作方法

    除了"在操作系统中修改时区信息,然后重启.NET应用程序,使其生效"之外,如何在不修改操作系统时区的前提下,修改.NET中的默认时区呢,下面小编给大家分享更改.NET中的默认时区的操作方法,感兴趣的朋友一起看看吧
    2024-06-06
  • .NET发布网站详细步骤

    .NET发布网站详细步骤

    这篇文章主要为大家介绍了.NET发布网站详细步骤,包括web网站发布、IIS6 安装方法、ASP.NET v4.0 安装方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • asp.net Mvc4 使用ajax结合分页插件实现无刷新分页

    asp.net Mvc4 使用ajax结合分页插件实现无刷新分页

    本篇文章主要介绍了 asp.net Mvc4 使用ajax结合分页插件实现无刷新分页,ajax通过回调函数把控制器返回的分部视图内容加载到主视图中显示,有兴趣的可以了解一下。
    2017-01-01
  • 浅谈ASP.NET Core 2.0 部分视图(译)

    浅谈ASP.NET Core 2.0 部分视图(译)

    本篇文章主要介绍了浅谈ASP.NET Core 2.0 部分视图(译),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • VS2022 .NET5一键发布到远程腾讯云IIS服务器的详细步骤

    VS2022 .NET5一键发布到远程腾讯云IIS服务器的详细步骤

    这篇文章主要介绍了VS2022 .NET5一键发布到远程腾讯云IIS服务器,首先需要添加服务器相关功能,文中通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • ASP.NET Core应用中与第三方IoC/DI框架的整合

    ASP.NET Core应用中与第三方IoC/DI框架的整合

    ASP.NET Core应用中,针对第三方DI框架的整合可以通过在定义Startup类型的ConfigureServices方法返回一个ServiceProvider来实现。但是并不是那么容易的,下面通过实例给大家分享一下
    2017-04-04
  • ASP.NET WebForms实现全局异常捕获与处理的最佳实践

    ASP.NET WebForms实现全局异常捕获与处理的最佳实践

    文章介绍了在ASP.NET WebForms中实现全局异常捕获与处理的最佳实践,包括在Global.asax中使用Application_Error、在Web.config中配置customErrors、在代码中使用try-catch、全局异常过滤以及使用日志记录库等方法,感兴趣的朋友一起看看吧
    2025-01-01

最新评论