ASP.NET實(shí)現(xiàn)將word文檔轉(zhuǎn)換成pdf的方法
本文實(shí)例講述了ASP.NET實(shí)現(xiàn)將word文檔轉(zhuǎn)換成pdf的方法,分享給大家供大家參考。具體實(shí)現(xiàn)步驟如下:
一、添加引用
二、轉(zhuǎn)換方法
1、方法
/// 把Word文件轉(zhuǎn)換成pdf文件
/// </summary>
/// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱</param>
/// <param name="targetPath">轉(zhuǎn)換完成后的文件的路徑和文件名名稱</param>
/// <returns>成功返回true,失敗返回false</returns>
public static bool WordToPdf(string sourcePath, string targetPath)
{
bool result = false;
WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//轉(zhuǎn)換格式1.wdExportFormatPDF轉(zhuǎn)換成pdf格式 2.wdExportFormatXPS轉(zhuǎn)換成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;//需要轉(zhuǎn)格式的文件路徑
string outputFileName = targetPath;//轉(zhuǎn)換完成后PDF或XPS文件的路徑和文件名名稱
WdExportFormat exportFormat = wdExportFormatPDF;//導(dǎo)出文件所使用的格式
bool openAfterExport = false;//轉(zhuǎn)換完成后是否打開
WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//導(dǎo)出方式1.wdExportOptimizeForPrint針對(duì)打印進(jìn)行導(dǎo)出,質(zhì)量較高,生成的文件大小較大。2.wdExportOptimizeForOnScreen 針對(duì)屏幕顯示進(jìn)行導(dǎo)出,質(zhì)量較差,生成的文件大小較小。
WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//導(dǎo)出全部?jī)?nèi)容(枚舉)
int from = 0;//起始頁碼
int to = 0;//結(jié)束頁碼
WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定導(dǎo)出過程中是否只包含文本或包含文本的標(biāo)記.1.wdExportDocumentContent:導(dǎo)出文件沒有標(biāo)記,2.導(dǎo)出文件有標(biāo)記
bool includeDocProps = true;//指定是否包含新導(dǎo)出的文件在文檔屬性
bool keepIRM = true;//
WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在導(dǎo)出文件中創(chuàng)建書簽,2.wdExportCreateHeadingBookmarks:標(biāo)題和文本框?qū)С龅奈募袆?chuàng)建一個(gè)書簽,3.wdExportCreateWordBookmarks每個(gè)字的書簽,其中包括除包含頁眉和頁腳中的所有書簽導(dǎo)出的文件中創(chuàng)建一個(gè)書簽。
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、簡(jiǎn)潔方法
/// 把Word文件轉(zhuǎn)換成pdf文件
/// </summary>
/// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱</param>
/// <param name="targetPath">轉(zhuǎn)換完成后的文件的路徑和文件名名稱</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;
}
三、調(diào)用
希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。
- C#實(shí)現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件
- asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
- Asp.net實(shí)現(xiàn)直接在瀏覽器預(yù)覽Word、Excel、PDF、Txt文件(附源碼)
- ASP.NET MVC 項(xiàng)目直接預(yù)覽PDF文件
- 詳解開源免費(fèi)且穩(wěn)定實(shí)用的.NET PDF打印組件itextSharp(.NET組件介紹之八)
- asp.net實(shí)現(xiàn)將ppt文檔轉(zhuǎn)換成pdf的方法
- ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫
- 如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
相關(guān)文章
.net任務(wù)調(diào)度框架Hangfire簡(jiǎn)介
這篇文章介紹了.net任務(wù)調(diào)度框架Hangfire的簡(jiǎn)單使用方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Asp.net 基于Cookie簡(jiǎn)易的權(quán)限判斷
基于Cookie簡(jiǎn)易的權(quán)限判斷代碼,需要的朋友可以參考下。2010-01-01
Entity Framework使用Code First模式管理視圖
本文詳細(xì)講解了Entity Framework使用Code First模式管理視圖的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
.NET 日志系統(tǒng)設(shè)計(jì)思路及實(shí)現(xiàn)代碼
這篇文章主要介紹了.NET 日志系統(tǒng)設(shè)計(jì)思路及實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12
.NET Core 基于Websocket的在線聊天室實(shí)現(xiàn)
這篇文章主要介紹了.NET Core 基于Websocket的在線聊天室實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
詳解ASP.NET Core 之 Identity 入門(二)
本篇文章主要介紹了ASP.NET Core 之 Identity 入門,主要負(fù)責(zé)對(duì)用戶的身份進(jìn)行認(rèn)證,有興趣的可以了解一下。2016-12-12
MVC實(shí)現(xiàn)下拉框聯(lián)動(dòng)效果(單選)
這篇文章主要為大家詳細(xì)介紹了MVC實(shí)現(xiàn)下拉框聯(lián)動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

