一、添加引用

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. Matlab中编译C++文件

    今天在跑<Robust Object Tracking via Sparsity-based Collaborative Model>这篇文章的代码时候,发现出现如下错误: 发现错误时由于 ...

  2. CSDN代码片排版

    CSDN上发博文,尤其是有代码的,要用好“插入代码”功能(见下图).这是必须的,未来的IT程序员,需要用这种方式,展示出自己的代码.这是尊重读者,树立品牌的需要. 相关方法,参考见视频<发布第一 ...

  3. 开源调度框架Quartz最佳实践

    开源调度框架Quartz最佳实践 Quartz是一个Java调度框架,当前的最新版本为2.2.1. 以Quartz 2.2.1版为例,Quartz最佳实践(用于生产系统)总结如下: 1.跳过更新检查Q ...

  4. SimpleDateFormat日期格式化

    public class T { /** * @param args */ public static void main(String[] args) { // TODO Auto-generate ...

  5. 【C#设计模式——创建型模式】简单工场模式

    进入码农行列也有一年半载了,仍然感觉自己混混沌沌,无所事事,无所作为,,,想想都下气,下气归下气,仍要奋起潜行,像愤怒的小鸟一边又一遍的冲向猪头也好,像蜗牛一样往前蹭也罢,总之要有蚂蚁啃骨头的精神!! ...

  6. C#中默认的修饰符

    参考自Default visibility for C# classes and members (fields, methods, etc)? Classes and structs that ar ...

  7. 三个Timer

    System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer 关于这三者的区别,可以参见msdn     https:// ...

  8. vim常用命令 vim键盘布局

    vim键盘布局,vim快捷键 vim常用命令:

  9. return File

    public ActionResult DownloadMessage() { string strExportData = "无数据!"; byte[] data = Syste ...

  10. UVa 1625 Color Length

    思路还算明白,不过要落实到代码上还真敲不出来. 题意: 有两个由大写字母组成的颜色序列,将它们合并成一个序列:每次可以把其中一个序列开头的颜色放到新序列的尾部. 对于每种颜色,其跨度定义为合并后的序列 ...