本文适用于VS2013 项目中的Word转换为PDF、Excel转换为PDF、PPT转换为PDF
0.一种更加简单方便的方法
1.本页所用的方法在本机测试时基本不会出现问题,只是偶尔PPT转PDF失败,需要重新添加dll,但是在服务器环境就会出现各种错误,需要各种配置。
2. 请参考此文,更简单实用。
1.添加Using
1.在后台添加using
-
using Microsoft.Office.Interop.Word;using Microsoft.Office.Interop.Excel;using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint;
2.添加引用
1.添加引用
3.添加代码
1.在后台添加代码
-
protected void Page_Load(object sender, EventArgs e){ string wordSource,wordTarget,excelSource,excelTarget,pptSource,pptTarget; wordSource = @"E:\FileDownload\Explorer\意向 0.3.docx"; wordTarget = @"E:\FileDownload\Explorer\W2P.pdf"; excelSource = @"E:\FileDownload\Explorer\xlsx.xlsx"; excelTarget = @"E:\FileDownload\Explorer\E2P.pdf"; pptSource = @"E:\FileDownload\Explorer\质量月活动1509.pptx"; pptTarget = @"E:\FileDownload\Explorer\P2P.pdf"; WordToPdf(wordSource,wordTarget); ExcelToPdf(excelSource,excelTarget); PPTConvertToPDF(pptSource, pptTarget);}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;}public static bool ExcelToPdf(string sourcePath, string targetPath){ bool result = false; XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf object missing = Type.Missing; Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null; Workbook workbook = null; try { applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass(); string inputfileName = sourcePath;//需要转格式的文件路径 string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称 XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式 XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量 bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。 bool openAfterPublish = false;//发布后不打开 workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); if (workbook!=null) { workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing); } result = true; } catch { result = false; } finally { if (workbook != null) { workbook.Close(true, missing, missing); workbook = null; } if (applicationClass != null) { applicationClass.Quit(); applicationClass = null; } } return result;}public static bool PPTConvertToPDF(string sourcePath, string targetPath){ bool result; PpSaveAsFileType ppSaveAsFileType = PpSaveAsFileType.ppSaveAsPDF;//转换成pdf object missing = Type.Missing; Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null; Presentation persentation = null; try { application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); if (persentation!=null) { persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue); } result = true; } catch { result = false; } finally { if (persentation != null) { persentation.Close(); persentation = null; } if (application != null) { application.Quit(); application = null; } } return result;}
2.单独的Excel2PDF(Word2PDF同理,不过PPT2PDF就要采用上面的方法)
1.在后台添加using
using Microsoft . Office . Interop . Excel ;
2.在项目中添加引用
3.添加如下代码
-
public static bool ExcelToPdf(string sourcePath, string targetPath){ bool result = false; XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf object missing = Type.Missing; Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null; Workbook workbook = null; try { applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass(); string inputfileName = sourcePath;//需要转格式的文件路径 string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称 XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式 XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量 bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。 bool openAfterPublish = false;//发布后不打开 workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); if (workbook!=null) { workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing); } result = true; } catch { result = false; } finally { if (workbook != null) { workbook.Close(true, missing, missing); workbook = null; } if (applicationClass != null) { applicationClass.Quit(); applicationClass = null; } } return result;}
4.如果遇到”无法嵌入互操作类型“……”,请改用适用的接口“
选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False。
PS
1.我在具体项目中使用时发现方法一种引入的DLL可以重复引用(感觉就像引用了没效果一样),而且每次关闭VS2013后重新打开编译工程都失败,都必须再重新引用一次。侥幸PPT转PDF功能用不上,最后选择了方法二。