With the ever increasing number of file formats supported by Microsoft Office, a common problem seems to be programmatically converting a document from one format to another. This is an issue Insight4 came up against recently while migrating some legacy code.
Previous to Office 2007, this programmatic PDF conversion was only available by printing the document out to postscript, then rendering to PDF using Adobe Distiller or similar.
Below is an example of how to use Microsoft Word 2007 to convert a file from Word document format to PDF, using C# the .NET Framework 2.0 and the new PDF/XPS exporter supplied by Microsoft.
The same technique could be applied using the .NET object models in Excel or Powerpoint to convert their files too. Similarly, you could output to other file formats, such as XPS or older Office formats.
In order to compile this you will need;
- Visual Studio 2005 (Professional or Team Studio)
- Visual Studio 2005 Tools for Office System
- Microsoft Word 2007
- Microsoft Office Addin: Save as PDF
- .NET Framework 2.0
The binary will execute on any machine that has;
- Microsoft Word 2007
- Microsoft Office Addin: Save as PDF
- Visual Studio 2005 Tools for Office System Second Edition Runtime
- .NET Framework 2.0
Attached is a complete example as a C# console applcation at the bottom of the post. Below is the crux of the application.
A word of caution, make sure you are aware of your Office licensing if you employ this solution.
private int convertToPDF(object sourceDoc, object targetDoc)
{ //create an instance of Word COM object
Microsoft.Office.Interop.Word.ApplicationClass msWord = new Microsoft.Office.Interop.Word.ApplicationClass();
//create an unknown object point to pass as reference into our functions
object paramUnknown = Type.Missing;
object paramSaveChangesNo = WdSaveOptions.wdDoNotSaveChanges;
object paramFormatPDF = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
object paramReadOnly = true;
int errorLevel = 0;
try
{
//ensure the COM object is hidden
msWord.Visible = false;
//open the document
msWord.Documents.Open(ref sourceDoc,
ref paramUnknown, ref paramReadOnly, ref paramUnknown,
ref paramUnknown,ref paramUnknown, ref paramUnknown,
ref paramUnknown,ref paramUnknown, ref paramUnknown,
ref paramUnknown,ref paramUnknown, ref paramUnknown,
ref paramUnknown, ref paramUnknown, ref paramUnknown);
//ensure the word application stays hidden and out of the taskbar
msWord.Application.Visible = false;
msWord.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;
//save the source document as the new target name
msWord.ActiveDocument.SaveAs(ref targetDoc,
ref paramFormatPDF,ref paramUnknown, ref paramUnknown,
ref paramUnknown,ref paramUnknown, ref paramUnknown,
ref paramUnknown,ref paramUnknown, ref paramUnknown,
ref paramUnknown,ref paramUnknown, ref paramUnknown,
ref paramUnknown,ref paramUnknown, ref paramUnknown);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
errorLevel = 1;
}
finally
{
if (msWord != null)
{
//close the reference to the Word object
msWord.Quit(ref paramSaveChangesNo, ref paramUnknown, ref paramUnknown);
}
}
return errorLevel;
}




