首页 > 代码库 > 关于Asp.Net调用Office组件将Word文件转换为Pdf文件

关于Asp.Net调用Office组件将Word文件转换为Pdf文件

1、首先安装 Microsoft Office 2007加载项:Microsoft Save as PDF-简体中文版:下载地址:

http://download.microsoft.com/download/3/8/8/388812b2-0d3f-474e-a7ef-b095d3d0d3cd/SaveAsPDF.exe

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public int ConvertWordToPdfByPlugin(string sourceFilePath, string destFilePath)
{
    int result = 0;
    object paramMissing = Type.Missing;
    Word.ApplicationClass wordApplication = new Word.ApplicationClass();
    Word.Document wordDocument = null;
    try
    {
        object paramSourceFilePath = sourceFilePath;
        Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;
        wordDocument = wordApplication.Documents.Open(
                                ref paramSourceFilePath, ref paramMissing, ref paramMissing,
                                ref paramMissing, ref paramMissing, ref paramMissing,
                                ref paramMissing, ref paramMissing, ref paramMissing,
                                ref paramMissing, ref paramMissing, ref paramMissing,
                                ref paramMissing, ref paramMissing, ref paramMissing,
                                ref paramMissing);
        if (wordDocument != null)
        {
            Word.WdExportOptimizeFor paramExportOptimizeFor =
                Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
            Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
            Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
            Word.WdExportCreateBookmarks paramCreateBookmarks =
                Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            wordDocument.ExportAsFixedFormat(destFilePath,
                paramExportFormat, false,
                paramExportOptimizeFor, paramExportRange, 0,
                0, paramExportItem, true,
                true, paramCreateBookmarks, true,
                true, false,
                ref paramMissing);
        }
        else
        {
            ScriptUtil.Alert("读取源word文件出错!");
        }
    }
    catch (Exception ex)
    {
        LogUtil.WriteLog(ex);
        result = 1;
    }
    finally
    {
        if (wordDocument != null)
        {
            wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
            wordDocument = null;
        }
        if (wordApplication != null)
        {
            wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
            wordApplication = null;
        }
    }
 
    return result;
}