001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Linq;
004.
using
System.Web;
005.
using
Word = Microsoft.Office.Interop.Word;
006.
using
Excel = Microsoft.Office.Interop.Excel;
007.
using
PowerPoint = Microsoft.Office.Interop.PowerPoint;
008.
using
Microsoft.Office.Core;
009.
namespace
Wolfy.OfficePreview
010.
{
011.
/// <summary>
012.
/// Office2Pdf 将Office文档转化为pdf
013.
/// </summary>
014.
public
class
Office2PDFHelper
015.
{
016.
public
Office2PDFHelper()
017.
{
018.
//
019.
// TODO: 在此处添加构造函数逻辑
020.
//
021.
}
022.
/// <summary>
023.
/// Word转换成pdf
024.
/// </summary>
025.
/// <param name="sourcePath">源文件路径</param>
026.
/// <param name="targetPath">目标文件路径</param>
027.
/// <returns>true=转换成功</returns>
028.
public
static
bool
DOCConvertToPDF(
string
sourcePath,
string
targetPath)
029.
{
030.
bool
result =
false
;
031.
Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
032.
object
paramMissing = Type.Missing;
033.
Word.ApplicationClass wordApplication =
new
Word.ApplicationClass();
034.
Word.Document wordDocument =
null
;
035.
try
036.
{
037.
object
paramSourceDocPath = sourcePath;
038.
string
paramExportFilePath = targetPath;
039.
Word.WdExportFormat paramExportFormat = exportFormat;
040.
bool
paramOpenAfterExport =
false
;
041.
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
042.
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
043.
int
paramStartPage = 0;
044.
int
paramEndPage = 0;
045.
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
046.
bool
paramIncludeDocProps =
true
;
047.
bool
paramKeepIRM =
true
;
048.
Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
049.
bool
paramDocStructureTags =
true
;
050.
bool
paramBitmapMissingFonts =
true
;
051.
bool
paramUseISO19005_1 =
false
;
052.
wordDocument = wordApplication.Documents.Open(
053.
ref
paramSourceDocPath,
ref
paramMissing,
ref
paramMissing,
054.
ref
paramMissing,
ref
paramMissing,
ref
paramMissing,
055.
ref
paramMissing,
ref
paramMissing,
ref
paramMissing,
056.
ref
paramMissing,
ref
paramMissing,
ref
paramMissing,
057.
ref
paramMissing,
ref
paramMissing,
ref
paramMissing,
058.
ref
paramMissing);
059.
if
(wordDocument !=
null
)
060.
wordDocument.ExportAsFixedFormat(paramExportFilePath,
061.
paramExportFormat, paramOpenAfterExport,
062.
paramExportOptimizeFor, paramExportRange, paramStartPage,
063.
paramEndPage, paramExportItem, paramIncludeDocProps,
064.
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
065.
paramBitmapMissingFonts, paramUseISO19005_1,
066.
ref
paramMissing);
067.
result =
true
;
068.
}
069.
catch
070.
{
071.
result =
false
;
072.
}
073.
finally
074.
{
075.
if
(wordDocument !=
null
)
076.
{
077.
wordDocument.Close(
ref
paramMissing,
ref
paramMissing,
ref
paramMissing);
078.
wordDocument =
null
;
079.
}
080.
if
(wordApplication !=
null
)
081.
{
082.
wordApplication.Quit(
ref
paramMissing,
ref
paramMissing,
ref
paramMissing);
083.
wordApplication =
null
;
084.
}
085.
GC.Collect();
086.
GC.WaitForPendingFinalizers();
087.
GC.Collect();
088.
GC.WaitForPendingFinalizers();
089.
}
090.
return
result;
091.
}
092.
093.
/// <summary>
094.
/// 把Excel文件转换成PDF格式文件
095.
/// </summary>
096.
/// <param name="sourcePath">源文件路径</param>
097.
/// <param name="targetPath">目标文件路径</param>
098.
/// <returns>true=转换成功</returns>
099.
public
static
bool
XLSConvertToPDF(
string
sourcePath,
string
targetPath)
100.
{
101.
bool
result =
false
;
102.
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
103.
object
missing = Type.Missing;
104.
Excel.ApplicationClass application =
null
;
105.
Excel.Workbook workBook =
null
;
106.
try
107.
{
108.
application =
new
Excel.ApplicationClass();
109.
object
target = targetPath;
110.
object
type = targetType;
111.
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
112.
missing, missing, missing, missing, missing, missing, missing, missing, missing);
113.
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard,
true
,
false
, missing, missing, missing, missing);
114.
result =
true
;
115.
}
116.
catch
117.
{
118.
result =
false
;
119.
}
120.
finally
121.
{
122.
if
(workBook !=
null
)
123.
{
124.
workBook.Close(
true
, missing, missing);
125.
workBook =
null
;
126.
}
127.
if
(application !=
null
)
128.
{
129.
application.Quit();
130.
application =
null
;
131.
}
132.
GC.Collect();
133.
GC.WaitForPendingFinalizers();
134.
GC.Collect();
135.
GC.WaitForPendingFinalizers();
136.
}
137.
return
result;
138.
}
139.
///<summary>
140.
/// 把PowerPoint文件转换成PDF格式文件
141.
///</summary>
142.
///<param name="sourcePath">源文件路径</param>
143.
///<param name="targetPath">目标文件路径</param>
144.
///<returns>true=转换成功</returns>
145.
public
static
bool
PPTConvertToPDF(
string
sourcePath,
string
targetPath)
146.
{
147.
bool
result;
148.
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
149.
object
missing = Type.Missing;
150.
PowerPoint.ApplicationClass application =
null
;
151.
PowerPoint.Presentation persentation =
null
;
152.
try
153.
{
154.
application =
new
PowerPoint.ApplicationClass();
155.
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
156.
result =
true
;
157.
}
158.
catch
159.
{
160.
result =
false
;
161.
}
162.
finally
163.
{
164.
if
(persentation !=
null
)
165.
{
166.
persentation.Close();
167.
persentation =
null
;
168.
}
169.
if
(application !=
null
)
170.
{
171.
application.Quit();
172.
application =
null
;
173.
}
174.
GC.Collect();
175.
GC.WaitForPendingFinalizers();
176.
GC.Collect();
177.
GC.WaitForPendingFinalizers();
178.
}
179.
return
result;
180.
}
181.
}
182.
}
183.
184.
Office2PDFHelper