架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 18644|回复: 1

[资料] .net把word文档转换成pdf文件

[复制链接]
发表于 2015-12-16 15:49:24 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Microsoft.Office.Interop.Word;

  7. namespace MvcApplication1.Controllers
  8. {
  9.     public class HomeController : Controller
  10.     {
  11.         //
  12.         // GET: /Home/
  13.         public ActionResult Index(string type, int id = 0)
  14.         {
  15.             ViewBag.ID = id;
  16.             ViewBag.Type = type;
  17.             return View();
  18.         }

  19.         [HttpPost]
  20.         public ActionResult Index(FormCollection collection)
  21.         {
  22.             Toprint();

  23.             return View();
  24.         }

  25.         public void Toprint()
  26.         {
  27.             WriteIntoWord wiw = new WriteIntoWord();
  28.             string FilePath = Server.MapPath("jkxy01.dot"); //模板路径

  29.             var filename = "qjjf" + DateTime.Now.ToString("yyyyMMddHHmmss");
  30.             string Bookmarkjkqx = "jkqx";
  31.             string Filljkqx = "400";

  32.             string SaveDocPath = Server.MapPath(filename+".doc");
  33.             wiw.OpenDocument(FilePath);

  34.             //写入书签
  35.             wiw.WriteIntoDocument("IdCard", "123");
  36.             wiw.WriteIntoDocument("chujieren", "借款人");
  37.             wiw.WriteIntoDocument(Bookmarkjkqx, Filljkqx);

  38.             wiw.Save_CloseDocument(SaveDocPath);

  39.             //转成pdf
  40.            string SavePdfPath = Server.MapPath("contract/"+filename+".pdf");
  41.            var flag= wiw.WordToPdf(SaveDocPath, SavePdfPath);
  42.             if (flag)
  43.             {
  44.                 //删除临时word,
  45.                 System.IO.File.Delete(SaveDocPath);
  46.             }
  47.         }
  48.     }

  49.     public class WriteIntoWord
  50.     {
  51.         private ApplicationClass app = null; //定义应用程序对象
  52.         private Document doc = null; //定义 word 文档对象
  53.         private Object missing = System.Reflection.Missing.Value; //定义空变量
  54.         private Object isReadOnly = false;
  55.         // 向 word 文档写入数据
  56.         public void OpenDocument(string FilePath)
  57.         {
  58.             object filePath = FilePath; //文档路径
  59.             app = new ApplicationClass(); //打开文档
  60.             doc = app.Documents.Open(ref filePath, ref missing, ref missing, ref missing,
  61.                                      ref missing, ref missing, ref missing, ref missing);
  62.             doc.Activate(); //激活文档
  63.         }

  64.         /// <summary>
  65.         ///   打开word,将对应数据写入word里对应书签域
  66.         /// </summary>
  67.         ///<param name="parLableName">域标签</param>
  68.         /// <param name="parFillName">写入域中的内容</param>  
  69.      
  70.         public void WriteIntoDocument(string BookmarkName, string FillName)
  71.         {
  72.             object bookmarkName = BookmarkName;
  73.             Bookmark bm = doc.Bookmarks.get_Item(ref bookmarkName); //返回书签
  74.             bm.Range.Text = FillName; //设置书签域的内容
  75.         }

  76.         /// <summary>
  77.         /// 保存并关闭
  78.         /// </summary>
  79.         /// <param name="parSaveDocPath">文档另存为的路径</param>
  80.         ///
  81.         public void Save_CloseDocument(string SaveDocPath)
  82.         {
  83.             object savePath = SaveDocPath; //文档另存为的路径
  84.             Object saveChanges = app.Options.BackgroundSave; //文档另存为
  85.             doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing,
  86.                        ref missing, ref missing, ref missing);
  87.             doc.Close(ref saveChanges, ref missing, ref missing); //关闭文档
  88.             app.Quit(ref missing, ref missing, ref missing); //关闭应用程序

  89.         }

  90.         /// <summary>

  91.         /// 把Word文件转换成pdf文件

  92.         /// </summary>

  93.         /// <param name="sourcePath">需要转换的文件路径和文件名称</param>

  94.         /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>

  95.         /// <returns>成功返回true,失败返回false</returns>

  96.         public  bool WordToPdf(object sourcePath, string targetPath)
  97.         {

  98.             bool result = false;

  99.             WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;

  100.             object missing = Type.Missing;

  101.             Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;

  102.             Document document = null;

  103.             try
  104.             {

  105.                 applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();

  106.                 document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

  107.                 if (document != null)
  108.                 {

  109.                     document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);

  110.                 }

  111.                 result = true;

  112.             }

  113.             catch
  114.             {

  115.                 result = false;

  116.             }

  117.             finally
  118.             {

  119.                 if (document != null)
  120.                 {

  121.                     document.Close(ref missing, ref missing, ref missing);

  122.                     document = null;

  123.                 }

  124.                 if (applicationClass != null)
  125.                 {

  126.                     applicationClass.Quit(ref missing, ref missing, ref missing);

  127.                     applicationClass = null;

  128.                 }

  129.             }

  130.             return result;

  131.         }
  132.     }
  133. }
复制代码
需要引用using Microsoft.Office.Interop.Word;


Microsoft.Office.Interop.Word.rar (381.81 KB, 下载次数: 0)




上一篇:net使用HttpListener监听端口
下一篇:WPF更改启动窗口默认项
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2015-12-17 01:52:00 | 显示全部楼层
{:1_1:
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-12-30 08:14

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表