一、添加引用

using Microsoft.Office.Interop.Word;

二、转换方法

1、方法

C# 代码

/// <summary>

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

/// </summary>

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

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

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

public static bool WordToPdf(string sourcePath, string targetPath)

{

bool result = false;

WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式

1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式

object missing = Type.Missing;

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

Document document = null;

try

{

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

object inputfileName = sourcePath;//需要转格式的文件路径

string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称

WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式

bool openAfterExport = false;//转换完成后是否打开

WdExportOptimizeFor wdExportOptimizeForPrint =

WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进

行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,

质量较差,生成的文件大小较小。

WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全

部内容(枚举)

int from = 0;//起始页码

int to = 0;//结束页码

WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//

指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.

导出文件有标记

bool includeDocProps = true;//指定是否包含新导出的文件在文档属性

bool keepIRM = true;//

WdExportCreateBookmarks wdExportCreateWordBookmarks =

WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导

出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,

3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中

创建一个书签。

bool docStructureTags = true;

bool bitmapMissingFonts = true;

bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)

document = applicationClass.Documents.Open(ref inputfileName, 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);

if (document != null)

{

document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport,

wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent,

includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags,

bitmapMissingFonts, UseISO19005_1, ref missing);

}

result = true;

}

catch

{

result = false;

}

finally

{

if (document != null)

{

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

document = null;

}

if (applicationClass != null)

{

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

applicationClass = null;

}

}

return result;

}

2、简洁方法

C# 代码

/// <summary>

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

/// </summary>

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

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

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

public static bool WordToPdf(object sourcePath, string targetPath)

{

bool result = false;

WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;

object missing = Type.Missing;

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

Document document = null;

try

{

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

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);

if (document != null)

{

document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false,

WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0,

WdExportItem.wdExportDocumentContent, true, true,

WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);

}

result = true;

}

catch

{

result = false;

}

finally

{

if (document != null)

{

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

document = null;

}

if (applicationClass != null)

{

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

applicationClass = null;

}

}

return result;

}

三、调用

OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");

ASP.NET将word文档转换成pdf的代码的更多相关文章

  1. Python将word文档转换成PDF文件

    如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...

  2. Java利用aspose-words将word文档转换成pdf(破解 无水印)

    首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...

  3. asp.net将ppt文档转换成pdf

    一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法   C# 代码   复制 // ...

  4. Java实现批量将word文档转换成PDF

    先导入words的jar包 需要jar包的私聊我发你 代码如下:import com.aspose.words.Document;import java.io.File; public class W ...

  5. C# word文档转换成PDF格式文档

    最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...

  6. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

  7. C#实现文档转换成PDF

    网上有很多将doc.ppt.xls等类型的文档转换成pdf的方法,目前了解到的有两大类: 1.使用虚拟打印机将doc.ppt.xls等类型的文档 2.使用OFFICE COM组件 我采用了第二种方法实 ...

  8. JAVA:借用OpenOffice将上传的Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

  9. OpenOffice Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

随机推荐

  1. java split IP地址要用双斜杠

    示例代码: public void test() { String address = "11.12.13.14:800"; System.out.println(address. ...

  2. 将集成spring的项目从tomcat上移植到weblogic下存在的问题

    当在weblogic下部署时, 1.需要jersey-servlet-xx.jar,jersey-core-xx.jar,jersey-server-xx.jar: 2.在web.xml中全局参数co ...

  3. [itint5]最短路径遍历点

    http://www.itint5.com/oj/#50 此题有点难,参考了这篇文章,是个两条路的DP: http://blog.csdn.net/a83610312/article/details/ ...

  4. QString->string->wstring->LPCWSTR

    QFileInfo info("./records.db"); std::string str = info.absoluteFilePath().toStdString(); / ...

  5. 在java程序中访问windows有用户名和密码保护的共享目录

    在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0)  编辑  收藏 --> Jav ...

  6. Hibernate框架简述

    Hibernate的核心组件在基 于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.properties或 hibernate ...

  7. Android加速度传感器实现“摇一摇”,带手机振动

    由于代码有点多,所以就分开写了,注释还算详细,方便学习 Activity package com.lmw.android.test;   import android.app.Activity; im ...

  8. 基于XMPP的即时通信系统的建立(六)— 开发环境搭建

    服务器端 新建空工程 使用Eclipse新建名为openfire的空java工程. 导入源代码 这里使用的是openfire的openfire_src_3_10_3.zip源码. 导入后将目录src/ ...

  9. Qt之进程间通信(Windows消息)

    简述 通过上一节的了解,我们可以看出进程通信的方式很多,今天分享下如何利用Windows消息机制来进行不同进程间的通信. 简述 效果 发送消息 自定义类型与接收窗体 发送数据 接收消息 设置标题 重写 ...

  10. 无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败。

    无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败. 在Asp.Net项目中使用WebClient或HttpWebRequest时出现以上错误 解决方案:把以下代码放在 ...