Converting Document (Word\ Excel\ PowerPoint\ Visio\ text\ XML\ RTF\ CSV etc.) to PDF using MSOffice .NET API






4.67/5 (6 votes)
Easy steps for converting Word /Excel /PowerPoint /Visio file to PDF.
Contents
- Introduction
- System Requirement
- Solution Overview
- Source Code
- Code for Word \ TXT \ XML to PDF conversion
- Code for Excel to PDF conversion
- Code for PowerPoint to PDF conversion
- Code for Visio to PDF conversion
- Release Object
- How to Run Demo Program
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
- Open Document that need to convert.
- Export Document as PDF.
- Release occupied Object.
//
// Start MS word application
//
Microsoft.Office.Interop.Word.Application msWordDoc = null;
Microsoft.Office.Interop.Word.Document doc = null;
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
msWordDoc = new Microsoft.Office.Interop.Word.Application
{
Visible = false,
ScreenUpdating = false
};
//
//Open Document
//
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);
...
//
// save Document as PDF
//
if (doc != null)
{
doc.Activate();
// save Document as PDF
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");
}
...
//
// Quit Word and release the ApplicationClass object
//
finally
{
// Close and release the Document object.
if (doc != null)
{
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref saveChanges, ref oMissing, ref oMissing);
Util.releaseObject(doc);
}
// Quit Word and release the ApplicationClass object.
((_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
- Open Excel that need to convert.
- Export Excel as PDF.
- Release Occupied Object.
//
// Create COM Objects
//
Microsoft.Office.Interop.Excel.Application excelApplication = null;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
object unknownType = Type.Missing;
// Create new instance of Excel
//open excel application
excelApplication = new Microsoft.Office.Interop.Excel.Application
{
ScreenUpdating = false,
DisplayAlerts = false
};
...
//
//Open Excel \ CSV
//
if (excelApplication != null)
excelWorkbook = excelApplication.Workbooks.Open(originalXlsPath, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType,
unknownType, unknownType, unknownType);
...
//
// save Excel as PDF
//
// Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
pdfPath,unknownType, unknownType, unknownType, unknownType, unknownType,
unknownType, unknownType);
...
//
// Quit Excel and release the ApplicationClass object
//
finally
{
// Close the workbook, quit the Excel, and clean up regardless of the results...
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
- Open PowerPoint that need to convert.
- Export PowerPoint as PDF.
- Release Occupied Object.
//
// Create COM Objects
//
PowerPoint.Application pptApplication = null;
PowerPoint.Presentation pptPresentation = null;
object unknownType = Type.Missing;
//start power point
pptApplication = new PowerPoint.Application();
...
//
//open powerpoint document
//
pptPresentation = pptApplication.Presentations.Open((string)originalPptPath,
Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoFalse);
...
//
// save 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);
...
//
// Quit PowerPoint and release the ApplicationClass object
//
finally
{
// Close and release the Document object.
if (pptPresentation != null)
{
pptPresentation.Close();
Util.releaseObject(pptPresentation);
pptPresentation = null;
}
// Quit Word and release the ApplicationClass object.
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
- Open Visio that need to convert.
- Export Visio as PDF.
- Release occupied Object.
//
// Create COM Objects
//
Microsoft.Office.Interop.Visio.ApplicationClass msVisioDoc = null;
Visio.Document vsdDoc = null;
//Start application
msVisioDoc = new Visio.ApplicationClass { Visible = false };
...
//
//Open Visio Document
//
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);
...
//
// Quit Visio and release the ApplicationClass object
//
finally
{
// Close and release the Document object.
if (vsdDoc != null)
{
vsdDoc.Close();
Util.releaseObject(vsdDoc);
}
// Quit Word and release the ApplicationClass object.
msVisioDoc.Quit();
Util.releaseObject(msVisioDoc);
}
...
V. Release Object
Method releaseObject have step to release Office COM Object
//
// Create COM Objects
//
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.