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)換完成后是否打開(kāi)
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;//起始頁(yè)碼
int to = 0;//結(jié)束頁(yè)碼
WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定導(dǎo)出過(guò)程中是否只包含文本或包含文本的標(biāo)記.1.wdExportDocumentContent:導(dǎo)出文件沒(méi)有標(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)建書(shū)簽,2.wdExportCreateHeadingBookmarks:標(biāo)題和文本框?qū)С龅奈募袆?chuàng)建一個(gè)書(shū)簽,3.wdExportCreateWordBookmarks每個(gè)字的書(shū)簽,其中包括除包含頁(yè)眉和頁(yè)腳中的所有書(shū)簽導(dǎo)出的文件中創(chuàng)建一個(gè)書(shū)簽。
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文件
- 詳解開(kāi)源免費(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ù)庫(kù)
- 如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
相關(guān)文章
asp.net 虛擬主機(jī)時(shí)常出現(xiàn)MAC驗(yàn)證失敗錯(cuò)誤之解決方法
驗(yàn)證視圖狀態(tài) MAC 失敗。如果此應(yīng)用程序由網(wǎng)絡(luò)場(chǎng)或群集承載,請(qǐng)確保 <machineKey> 配置指定了相同的 validationKey 和驗(yàn)證算法。不能在群集中使用 AutoGenerate。2009-05-05Win2008 server + IIS7 設(shè)置身份模擬(ASP.NET impersonation)
IIS7 與 IIS 6 相比有了很大的改動(dòng),原來(lái)在 IIS 6 下可以的設(shè)置到了 IIS 7 下有的會(huì)發(fā)生變化。身份模擬的配置上,IIS7 和 IIS6有很大不同,網(wǎng)上IIS6的身份模擬的文章比較多,但介紹IIS7的比較少,我把的一些折騰的經(jīng)驗(yàn)在這篇博客中寫(xiě)下來(lái),以供參考2011-10-10用vs調(diào)試sql存儲(chǔ)過(guò)程圖文介紹
想必大家應(yīng)該有給存儲(chǔ)過(guò)程找錯(cuò)誤的經(jīng)歷吧,一遍遍的去讀sql代碼,一句一句的測(cè)試,發(fā)現(xiàn)一個(gè)小錯(cuò)誤可能都要用很長(zhǎng)的時(shí)間,接下來(lái)將介紹使用vs2010調(diào)試存儲(chǔ)過(guò)程,感興趣的朋友可以不要錯(cuò)過(guò)了啊2013-02-02ASP.NET?Core設(shè)置Ocelot網(wǎng)關(guān)限流
這篇文章介紹了ASP.NET?Core設(shè)置Ocelot網(wǎng)關(guān)限流的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04Asp.net中Response.Charset與Response.ContentEncoding區(qū)別示例分析
這篇文章主要介紹了Asp.net中Response.Charset與Response.ContentEncoding區(qū)別示例分析,對(duì)于深入理解Asp.net程序設(shè)計(jì)有一定的幫助,需要的朋友可以參考下2014-08-08asp.net TreeView遞歸循環(huán)子節(jié)點(diǎn)生成樹(shù)形菜單實(shí)例
這篇文章主要介紹了asp.net TreeView遞歸循環(huán)子節(jié)點(diǎn)生成樹(shù)形菜單的方法,涉及asp.net遞歸算法及節(jié)點(diǎn)操作相關(guān)技巧,需要的朋友可以參考下2016-07-07asp.net中強(qiáng)制取消TFS2008中其它成員的簽出文件的方法
有個(gè)項(xiàng)目,以前的成員離職了,剛好又簽出了一個(gè)文件在TFS中并且上了鎖,導(dǎo)致后面的維護(hù)無(wú)法簽入和生成。在網(wǎng)上查了一下,找到了如下解決辦法2012-08-08