ASP.NET实现将word文档转换成pdf的方法
程序员文章站
2024-02-20 13:29:28
本文实例讲述了asp.net实现将word文档转换成pdf的方法,分享给大家供大家参考。具体实现步骤如下:
一、添加引用
复制代码 代码如下:using micro...
本文实例讲述了asp.net实现将word文档转换成pdf的方法,分享给大家供大家参考。具体实现步骤如下:
一、添加引用
复制代码 代码如下:
using microsoft.office.interop.word;
二、转换方法
1、方法
复制代码 代码如下:
/// <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;
}
/// 把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、简洁方法
复制代码 代码如下:
/// <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;
}
/// 把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程序设计有所帮助。