源码下载地址:http://download.csdn.net/detail/kingmax54212008/9920401
将Word / Excel / PowerPoint / Visio文件转换为PDF的简单步骤。
内容
介绍
将Word / Excel / PowerPoint / Visio文件转换为PDF的简单步骤。.NET提供了一个简单的MSOffice API来导出PDF。
系统要求
- 框架3.5
- MS Office 2010
- Visual Studio 2010
解决方案概述
添加Microsoft Office库(版本:14.0.0.0),步骤如下:
- 右键点击引用并选择'添加参考..'
- 在.NET选项卡选择库中列出如下:
- 的Microsoft.Office.Interop.Excel
- Microsoft.Office.Interop。幻灯片
- Microsoft.Office.Interop.Visio
- 的Microsoft.Office.Interop.Word
- 办公室
I. Word \ TXT \ XML到PDF转换的代码
方法Word2Pdf
有四个步骤将Word \ TXT \ XML转换为PDF。
- 以隐藏模式启动Word应用程序
隐藏 复制代码
// // Start MS word application // Microsoft.Office.Interop.Word.Application msWordDoc = null; Microsoft.Office.Interop.Word.Document doc = null; // C# doesn't have optional arguments so we'll need a dummy value object oMissing = System.Reflection.Missing.Value; msWordDoc = new Microsoft.Office.Interop.Word.Application { Visible = false, ScreenUpdating = false };
- 打开文档需要转换。
隐藏 复制代码
// //Open Document // doc = msWordDoc.Documents.Open(ref originalDocPath, ref oMissing , ref oMissing, ref oMissing, ref oMissing, ref oMissing , ref oMissing,ref oMissing, ref oMissing, ref oMissing , ref oMissing, ref oMissing,ref oMissing, ref oMissing , ref oMissing, ref oMissing); ...
- 导出文件为PDF。
隐藏 复制代码
// // save Document as PDF // if (doc != null) { doc.Activate(); // save Document as PDF object fileFormat = WdSaveFormat.wdFormatPDF; doc.SaveAs(ref pdfPath, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); } else { Console.WriteLine("Error occured while converting office Word to PDF"); } ...
- 释放占用对象。
隐藏 复制代码
// // Quit Word and release the ApplicationClass object // finally { // Close and release the Document object. if (doc != null) { object saveChanges = WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref saveChanges, ref oMissing, ref oMissing); Util.releaseObject(doc); } // Quit Word and release the ApplicationClass object. ((_Application)msWordDoc).Quit(ref oMissing, ref oMissing, ref oMissing); Util.releaseObject(msWordDoc); msWordDoc = null; } ...
II。Excel转换PDF代码
方法Excel2Pdf有4个步骤将Excel转换为PDF
- 以隐藏模式启动Excel应用程序
隐藏 复制代码
// // Create COM Objects // Microsoft.Office.Interop.Excel.Application excelApplication = null; Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null; object unknownType = Type.Missing; // Create new instance of Excel //open excel application excelApplication = new Microsoft.Office.Interop.Excel.Application { ScreenUpdating = false, DisplayAlerts = false }; ...
- 打开需要转换的Excel。
隐藏 复制代码
// //Open Excel \ CSV // if (excelApplication != null) excelWorkbook = excelApplication.Workbooks.Open(originalXlsPath, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType); ...
- 将Excel导出为PDF。
隐藏 复制代码
// // save Excel as PDF // // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK) excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, pdfPath,unknownType, unknownType, unknownType, unknownType, unknownType, unknownType, unknownType); ...
- 释放被占用对象。
隐藏 复制代码
// // Quit Excel and release the ApplicationClass object // finally { // Close the workbook, quit the Excel, and clean up regardless of the results... if (excelWorkbook != null) excelWorkbook.Close(unknownType, unknownType, unknownType); if (excelApplication != null) excelApplication.Quit(); Util.releaseObject(excelWorkbook); Util.releaseObject(excelApplication); } ...
III。PowerPoint到PDF转换的代码
方法Powerpoint 2Pdf有4个步骤将PowerPoint转换为PDF。
- 以隐藏模式启动PowerPoint应用程序
隐藏 复制代码
// // Create COM Objects // PowerPoint.Application pptApplication = null; PowerPoint.Presentation pptPresentation = null; object unknownType = Type.Missing; //start power point pptApplication = new PowerPoint.Application(); ...
- 打开需要转换的PowerPoint。
隐藏 复制代码
// //open powerpoint document // pptPresentation = pptApplication.Presentations.Open((string)originalPptPath, Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse); ...
- 将PowerPoint导出为PDF。
隐藏 复制代码
// // save PowerPoint as PDF // pptPresentation.ExportAsFixedFormat((string)pdfPath, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF, PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint, MsoTriState.msoFalse,PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PowerPoint.PpPrintOutputType.ppPrintOutputSlides,MsoTriState.msoFalse, null, PowerPoint.PpPrintRangeType.ppPrintAll, string.Empty,true, true, true, true, false, unknownType); ...
- 释放被占用对象。
隐藏 复制代码
// // Quit PowerPoint and release the ApplicationClass object // finally { // Close and release the Document object. if (pptPresentation != null) { pptPresentation.Close(); Util.releaseObject(pptPresentation); pptPresentation = null; } // Quit Word and release the ApplicationClass object. pptApplication.Quit(); Util.releaseObject(pptApplication); pptApplication = null; } ...
IV。Visio转PDF代码
方法Visio2Pdf有4个步骤将Visio转换为PDF
- 以隐藏模式启动Visio应用程序
隐藏 复制代码
// // Create COM Objects // Microsoft.Office.Interop.Visio.ApplicationClass msVisioDoc = null; Visio.Document vsdDoc = null; //Start application msVisioDoc = new Visio.ApplicationClass { Visible = false }; ...
- 打开需要转换的Visio。
隐藏 复制代码
// //Open Visio Document // vsdDoc = msVisioDoc.Documents.Open(originalVsdPath); ...
- 导出Visio为PDF。
隐藏 复制代码
// // Export Visio as PDF // vsdDoc.ExportAsFixedFormat(Visio.VisFixedFormatTypes.visFixedFormatPDF, pdfPath, Visio.VisDocExIntent.visDocExIntentScreen,Visio.VisPrintOutRange.visPrintAll, 1, vsdDoc.Pages.Count, false, true, true, true, true,System.Reflection.Missing.Value); ...
- 释放占用对象。
隐藏 复制代码
// // Quit Visio and release the ApplicationClass object // finally { // Close and release the Document object. if (vsdDoc != null) { vsdDoc.Close(); Util.releaseObject(vsdDoc); } // Quit Word and release the ApplicationClass object. msVisioDoc.Quit(); Util.releaseObject(msVisioDoc); } ...
释放对象
方法releaseObject具有发布Office COM对象的步骤
隐藏
复制代码
//
// Create COM Objects
//
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception exReleaseObject)
{
obj = null;
Console.WriteLine("Release of COM Object Fail:"+ exReleaseObject);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
...
如何运行演示程序
- 提取“592957 / Converter_demo.zip”文件,并运行“Converter.exe”。
- 新窗口打开,点击“浏览”按钮。
- 选择要转换的文件类型。
- 选择文件,然后按“打开”。
- 单击“转换”按钮,将输入文件转换为PDF,并显示“成功邮件”PDF文件的输出文件。