当前位置: 首页 > article >正文

asp.net core调用wps实现word转pdf的方法

1,首先安装wps,从官网下载安装包

2,创建.net core控制项目

添加com引用,搜索wps

 

 

准备一个word文档,名字叫001.docx,随便编写一些文字内容

3,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也可以问题根本原因是路径的问题,不能些相对路径,必须绝对路径 

 

4,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();


http://www.kler.cn/news/314642.html

相关文章:

  • Python Selenium 自动化爬虫 + Charles Proxy 抓包
  • 十四,在Spring Boot当中对应“ Tomcat 服务器的相关配置”和“服务器的切换”的详细说明
  • 掌握 JavaScript 中的函数表达式
  • 页面布局实现-左侧横向滑动展示隐藏数据,右侧固定展示操作按钮。可上下滑动联动
  • 常用的图像增强的算法之间的联系和区别
  • Python3网络爬虫开发实战(17)爬虫的管理和部署(第一版)
  • Samba服务
  • 传统到AI 大数据分析的演变,颠覆智慧水电的未来?
  • 【JavaEE初阶】多线程(4)
  • 2024/9/21黑马头条跟学笔记(十)
  • Ubuntu 安装和使用 Fcitx 中文输入法;截图软件flameshot
  • 从零到一:打造安全高效敦煌测评自养号体系
  • 《C++高效字符串拼接之道:解锁性能与优雅的完美结合》
  • mac系统加密文件
  • Flutter Android Package调用python
  • 基于深度学习的因果推理与决策
  • 【JVM】运行时数据区
  • Ubuntu 20.04 内核升级后网络丢失问题的解决过程
  • 《DevOps实践指南》笔记-Part 3
  • Swift里的数值变量的最大值和最小值
  • 分布式光伏发电站数据采集设备管理硬件解决方案
  • 机器学习——Stacking
  • C++速通LeetCode中等第21题-排序链表(空间O(1))
  • 828华为云征文 | 华为云X实例的镜像管理详解
  • Unexpected end of file from server 错误
  • 系统架构设计师教程 第5章 5.4 软件测试 笔记
  • 论文阅读笔记:Sapiens: Foundation for Human Vision Models
  • MoCo和SimCLR【CV双雄】
  • mxnet算子调用kernel示例(MINIST)
  • Java面试篇-AOP专题(什么是AOP、AOP的几个核心概念、AOP的常用场景、使用AOP记录操作日志、Spring中的事务是如何实现的)