C# 用wps(api v9) 将word转成pdf

我们不产生代码只是代码的搬运工

 我们先来看一段跑不起来的代码  ..各种未将对象应用到实例..

 

using System;

 

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace word2PdfWithWps
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var word = @"D:\C#小工具\word2PdfWithWps\word2PdfWithWps\76C309855C1E240242693FD7D7C74D7E.docx";
            var pdf = @"D:\C#小工具\1.pdf";
            WordExportAsPdf(word, pdf);
            Console.ReadKey();
        }
        public static bool WordToPdfWithWps(string sourcePath, string targetPath)
        {
            Word.ApplicationClass app = new Word.ApplicationClass();
            Word.Document doc = null;
            try
            {
                doc = app.Documents.Open(sourcePath, true, true, false, null, null, false, "", null, 100, 0, true, true, 0, true);
                doc.SaveAs(targetPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            finally
            {
                // doc.Close();
            }
            return true;
        }
        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;
        }
    }
}


 

 

然后 到wps  官方论坛下载了一个别人写好的  发现只要装了wps  就可以直接使用

 地址: http://bbs.wps.cn/forum.php?mod=viewthread&tid=22434594&highlight=C%23 

 主要转换代码

using System;
using System.IO;
using Word;
namespace WpsToPdf
{
    class Wps2Pdf : IDisposable
    {
        dynamic wps;
        public Wps2Pdf()
        {

             //这里创建wps实例不知道用了什么骚操作就没有报错过 本机安装的是wps2016

            Type type = Type.GetTypeFromProgID("KWps.Application");
            wps = Activator.CreateInstance(type);
        }
        public void ToPdf(string wpsFilename, string pdfFilename = null)
        {
            if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); }
            if (pdfFilename == null)
            {
                pdfFilename = Path.ChangeExtension(wpsFilename, "pdf");
            }
            Console.WriteLine(string.Format(@"正在转换 [{0}]
      -> [{1}]", wpsFilename, pdfFilename));
             //到处都是dynamic   看的我一脸懵逼
            dynamic doc = wps.Documents.Open(wpsFilename, Visible: false);//这句大概是用wps 打开  word  不显示界面
            doc.ExportAsFixedFormat(pdfFilename, WdExportFormat.wdExportFormatPDF);//doc  转pdf 
            doc.Close();
        }
        public void Dispose()
        {
            if (wps != null) { wps.Quit(); }
        }
    }
}

 

转载于:https://www.cnblogs.com/qbz95/p/wpsapi.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于使用 EvoWordToPdfWord 换为 PDF,我可以提供以下 C# .NET Core 代码示例供参考: ```csharp using System.IO; using EvoPdf; namespace WordToPdfConverter { class Program { static void Main(string[] args) { // 输入 Word 文件路径和输出 PDF 文件路径 string inputFilePath = @"C:\input.docx"; string outputFilePath = @"C:\output.pdf"; // 创建 WordToPdfConverter 对象 WordToPdfConverter wordToPdfConverter = new WordToPdfConverter(); // 设置 PDF 文档的页面大小和方向 wordToPdfConverter.PdfDocumentOptions.PageSize = PdfPageSize.A4; wordToPdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Portrait; // 将 Word 文件换为 PDF 文件 byte[] pdfBytes = wordToPdfConverter.ConvertWord(inputFilePath); // 将 PDF 字节流写入文件 File.WriteAllBytes(outputFilePath, pdfBytes); } } } ``` 在上述代码中,我们首先指定了输入 Word 文件和输出 PDF 文件的路径。然后,我们创建了一个 WordToPdfConverter 对象,并设置了 PDF 文档的页面大小和方向。最后,我们调用 ConvertWord 方法将 Word 文件换为 PDF 文件,并将 PDF 字节流写入输出文件。 需要注意的是,为了使用 EvoWordToPdf,我们需要先安装 EvoPdf 库。您可以通过 NuGet 包管理器来安装 EvoPdf 库,或者手动下载 EvoPdf.dll 并将其添加到项目引用中。 希望这个代码示例可以帮助到您!如果您有任何问题或疑问,欢迎继续向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值