ASP.NET VS2013 Office 转 PDF

摘要:
本文档适用于VS2013项目中Word到PDF、Excel到PDF以及PPT到PDF0的转换。这是一种更简单、更方便的方法。1.在本地机器上测试时,本页中使用的方法基本上没有问题。只是偶尔,PPT到PDF会失败,需要再次添加DLL。但是,服务器环境中会出现各种错误,需要进行各种配置。

本文适用于VS2013 项目中的Word转换为PDF、Excel转换为PDF、PPT转换为PDF

0.一种更加简单方便的方法

 


 

1.本页所用的方法在本机测试时基本不会出现问题,只是偶尔PPT转PDF失败,需要重新添加dll,但是在服务器环境就会出现各种错误,需要各种配置。

2.http://www.cnblogs.com/moonache/p/4991459.html 请参考此文,更简单实用。

 

1.添加Using


1.在后台添加using

  1. using Microsoft.Office.Interop.Word;
    using Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.PowerPoint;

2.添加引用


1.添加引用

ASP.NET VS2013 Office 转 PDF第1张

3.添加代码


1.在后台添加代码

  1. protected void Page_Load(object sender, EventArgs e)
    {
        string wordSource,wordTarget,excelSource,excelTarget,pptSource,pptTarget;
        wordSource = @"E:FileDownloadExplorer意向 0.3.docx";
        wordTarget = @"E:FileDownloadExplorerW2P.pdf";
        excelSource = @"E:FileDownloadExplorerxlsx.xlsx";
        excelTarget = @"E:FileDownloadExplorerE2P.pdf";
        pptSource = @"E:FileDownloadExplorer质量月活动1509.pptx";
        pptTarget = @"E:FileDownloadExplorerP2P.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.在项目中添加引用
ASP.NET VS2013 Office 转 PDF第2张
3.添加如下代码
  1. 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功能用不上,最后选择了方法二。

来自为知笔记(Wiz)
 

免责声明:文章转载自《ASP.NET VS2013 Office 转 PDF》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SqlServer数据库基本用法Xshell 上传文件到Ubuntu下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

json字符串转Map、json数组

json数组转map public static void main(String[] args){ String strArr = "[{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}," + "{"00":"zhangsan","11"...

OFFICE 文档转换为html在线预览

OFFICE 文档在线预览方案很多: 服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览,比如flexpaper Office文档直接转换为SWF,通过网页加载Flash预览 微软的Office365 在浏览器中直接打开 转换为html 今天,我们要用的方案是转换为html来预览。 技术方案: office文档转换为pdf:使用li...

Java:原生javaWeb下载pdf文件

鉴于网上许多下载pdf的代码下载的pdf都是无效pdf,我稍加修改: @RequestMapping("/downPdf") public void downPdf(HttpServletResponse response, HttpServletRequest request){ String pdfPath = "C:\...

.net mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法

FlexPaper插件可以实现在浏览器中在线预览pdf,word,excel等。 在网上看到很多关于这个插件实现预览的技术,但是很难做到word和excel在线预览。 pdf很好实现。   首先下载相关的插件信息,这里不多说了。 其中这个插件主要需要配合Aspose来实现将上传的excel和word来转换为pdf。再通过pdf2swf来将pdf转换为swf...

[PDFBox]后台操作pdf的工具类

PDFBox是Apache下的一个操作pdf的类库。其也提供了一个命令行的工具,也提供了java调用的第三方类库。 下载地址:https://pdfbox.apache.org/ 下面的实验基于JDK8+pdfbox-2.0.13.jar+pdfbox-app-2.0.13.jar(命令行工具库) 1.命令行使用 文档参考:https://pdfbox.a...

转:将字符串或表达式直接转为C#可执行代码的办法

原文地址:http://blog.csdn.net/qwlovedzm/article/details/6292147 近日有个项目有不少的计算公式,而且要求能定制,如何能将字符串或表达式直接转为C#的可执行代码就好了。 经过从网上查阅资料,发现有一个开源的工具类,将它修改后改为如下效果: [c-sharp]view plaincopyprint?...