Recently, I had a great interest in how to convert Office documents to PDF or XPS. Therefore, I collected lots of materials. Now, I want to share a method I think is useful.
First, Office SaveAs PDF and XPS (an Add-in for Office 2007) needs to be installed. You can download it from the Microsoft official website.
Then, open VS. Take VS 2005 as an example. Create a new Windows application project and add the following COM components as references.
Microsoft Word 12.0 Object Library
Microsoft PowerPoint 12.0 Object Library
Microsoft Excel 12.0 Object Library
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
We can use bool to determine the format of generated files.
Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
PowerPoint.PpSaveAsFileType ppType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
In this method, I use ExportAsFixedFormat for Word and Excel and SaveAs for Powerpoint.
Actually, the SaveAs method can support several formats. Therefore, we can also use SaveAs for Word. However, it cannot be used for Excel.
Convert Word to PDF
private bool Convert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
{
bool result;
object paramMissing = Type.Missing;
Word.ApplicationClass wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor =
Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks =
Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, 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)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
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;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
Convert Excel to PDF
private bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
{
bool result;
object missing = Type.Missing;
ApplicationClass application = null;
Workbook workBook = null;
try
{
application = new ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
tal nahariPosted Jun 11, 2015, 7:14 AM
Hi I run your code in my application and im getting an COMException that my file is read only so it cannot be converted however a checked the files properties and it isnt read-onlyDo you maybe have a solution to my problem?
Rakesh JoganiPosted Nov 8, 2013, 2:16 AM
Aspose .Net library is not working properly for ppt else other type document working fine ...
Former memberPosted Nov 8, 2013, 1:48 AM
You can convert MS office documents to PDF or XPS format with .Net Library from Aspose: http://www.aspose.com/.net/total-component.aspx
Rakesh JoganiPosted Jul 18, 2013, 7:12 AM
When i converting ppt file into pdf or xps format i get error "Error HRESULT E_FAIL has been returned from a call to a COM component."
Praveen ElathoorPosted Feb 25, 2013, 7:51 PM
Hi Landy, Thanks for the article. Very well documented with sample. I am facing a problem with this, that the conversion is not working on win 2008 64bit operating systems. Can you help me on this? Thanks once again for the article Praveen Kumar E
Sumit GoyalPosted Oct 16, 2012, 2:40 AM
Thanks Sir, This code is working But there is any alternate by which no need to install SaveAs PDF and XPS . And Can we add the reference of dll Created after installation of SaveAs PDF and XPS. Thanks in Advance.
sujith monyPosted Nov 16, 2011, 1:06 PM
when i try to conver in pdf i am gettign this error This file could not be found. (New Microsoft Word Document.docx) {"This file could not be found.\r (New Microsoft Word Document.docx)"} please help me to solve this problem
Dinesh BeniwalPosted Jun 13, 2011, 12:08 AM
Very useful article, thanks for sharing