
Contents
Introduction
Easy steps for converting Word /Excel /PowerPoint /Visio file to PDF. .NET provides an easy MSOffice API for exporting PDF.
System Requirement
- Framework 3.5
- MS Office 2010
- Visual Studio 2010
Solution Overview
Add Library of Microsoft Office (version: 14.0.0.0) by step below:
- Right click to References & select ‘Add Reference..’

- In .NET tab select library listed below:
- Microsoft.Office.Interop.Excel
- Microsoft.Office.Interop.PowerPoint
- Microsoft.Office.Interop.Visio
- Microsoft.Office.Interop.Word
- Office

I. Code for Word \ TXT \ XML to PDF conversion
Method Word2Pdf
has four steps to convert Word \ TXT \ XML to PDF.
- Start Word Application in hidden mode
Microsoft.Office.Interop.Word.Application msWordDoc = null;
Microsoft.Office.Interop.Word.Document doc = null;
object oMissing = System.Reflection.Missing.Value;
msWordDoc = new Microsoft.Office.Interop.Word.Application
{
Visible = false,
ScreenUpdating = false
};
- Open Document that need to convert.
doc = msWordDoc.Documents.Open(ref originalDocPath, ref oMissing
, ref oMissing, ref oMissing, ref oMissing, ref oMissing
, ref oMissing,ref oMissing, ref oMissing, ref oMissing
, ref oMissing, ref oMissing,ref oMissing, ref oMissing
, ref oMissing, ref oMissing);
...
- Export Document as PDF.
if (doc != null)
{
doc.Activate();
object fileFormat = WdSaveFormat.wdFormatPDF;
doc.SaveAs(ref pdfPath,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
else
{
Console.WriteLine("Error occured while converting office Word to PDF");
}
...
- Release occupied Object.
finally
{
if (doc != null)
{
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref saveChanges, ref oMissing, ref oMissing);
Util.releaseObject(doc);
}
((_Application)msWordDoc).Quit(ref oMissing, ref oMissing, ref oMissing);
Util.releaseObject(msWordDoc);
msWordDoc = null;
}
...
II. Code for Excel to PDF conversion
Method Excel2Pdf have 4 steps to convert Excel to PDF
- Start Excel Application in hidden mode
Microsoft.Office.Interop.Excel.Application excelApplication = null;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
object unknownType = Type.Missing;
excelApplication = new Microsoft.Office.Interop.Excel.Application
{
ScreenUpdating = false,
DisplayAlerts = false
};
...
- Open Excel that need to convert.
if (excelApplication != null)
excelWorkbook = excelApplication.Workbooks.Open(originalXlsPath, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType);
...
- Export Excel as PDF.
excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
pdfPath,unknownType, unknownType, unknownType, unknownType, unknownType,
unknownType, unknownType);
...
- Release Occupied Object.
finally
{
if (excelWorkbook != null)
excelWorkbook.Close(unknownType, unknownType, unknownType);
if (excelApplication != null) excelApplication.Quit();
Util.releaseObject(excelWorkbook);
Util.releaseObject(excelApplication);
}
...
III. Code for PowerPoint to PDF conversion
Method Powerpoint2Pdf have 4 steps to convert PowerPoint to PDF.
- Start PowerPoint Application in hidden mode
PowerPoint.Application pptApplication = null;
PowerPoint.Presentation pptPresentation = null;
object unknownType = Type.Missing;
pptApplication = new PowerPoint.Application();
...
- Open PowerPoint that need to convert.
pptPresentation = pptApplication.Presentations.Open((string)originalPptPath,
Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoFalse);
...
- Export PowerPoint as PDF.
pptPresentation.ExportAsFixedFormat((string)pdfPath,
PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF,
PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint,
MsoTriState.msoFalse,PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
PowerPoint.PpPrintOutputType.ppPrintOutputSlides,MsoTriState.msoFalse, null,
PowerPoint.PpPrintRangeType.ppPrintAll, string.Empty,true, true, true,
true, false, unknownType);
...
- Release Occupied Object.
finally
{
if (pptPresentation != null)
{
pptPresentation.Close();
Util.releaseObject(pptPresentation);
pptPresentation = null;
}
pptApplication.Quit();
Util.releaseObject(pptApplication);
pptApplication = null;
}
...
IV. Code for Visio to PDF conversion
Method Visio2Pdf have 4 steps to convert Visio to PDF
- Start Visio Application in hidden mode
Microsoft.Office.Interop.Visio.ApplicationClass msVisioDoc = null;
Visio.Document vsdDoc = null;
msVisioDoc = new Visio.ApplicationClass { Visible = false };
...
- Open Visio that need to convert.
vsdDoc = msVisioDoc.Documents.Open(originalVsdPath);
...
- Export Visio as PDF.
vsdDoc.ExportAsFixedFormat(Visio.VisFixedFormatTypes.visFixedFormatPDF, pdfPath,
Visio.VisDocExIntent.visDocExIntentScreen,Visio.VisPrintOutRange.visPrintAll,
1, vsdDoc.Pages.Count, false, true, true, true, true,System.Reflection.Missing.Value);
...
- Release occupied Object.
finally
{
if (vsdDoc != null)
{
vsdDoc.Close();
Util.releaseObject(vsdDoc);
}
msVisioDoc.Quit();
Util.releaseObject(msVisioDoc);
}
...
V. Release Object
Method releaseObject have step to release Office COM Object
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception exReleaseObject)
{
obj = null;
Console.WriteLine("Release of COM Object Fail:"+ exReleaseObject);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
...
How to Run Demo Program
- Extract "592957/Converter_demo.zip" file, & run "Converter.exe".
- New Window will Open, click "Browse" Button.

- Select Type of File to Convert.

- Select file & Press "Open".

- Click "Convert" Button, its will convert input file to PDF & show you Output file of PDF file with Success Message.
