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
We can use bool to determine the format of generated files.
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
Convert Excel to PDF
By the way, I found a Free PDF Converter which can convert office files to PDF. That Free PDF Converter is also created via C#.
Convert PowerPoint to PDF
If we want to convert Word or Excel to XPS, we can use XPS virtual printer. After installing .Net Framework 3.5, the XPS virtual printer will be installed by default. And we set it as the default printer.
Microsoft XPS Document Writer
Word to XPS
Excel to XPS
In the same manner, if a PDF virtual printer is installed on the computer, for example, 5D PDF, we can set it as the default printer to convert other formats to PDF.
Also, we can put the methods in a Windows Service to do background conversion of documents.