Convert Word documents to PDF and XPS documents in .Net

In a previous old post actually two years ago I talked about converting HTML documents to Microsoft Word documents using the Microsoft Office application’s COM-based object model and it was very nice and easy task.

But what about PDF? actually there are tons of HTML to PDF converters out there for a long time, however, straggling with their feature lists, prices and support plans is not a fun thing at all; That’s  why two years ago I decided – in a project I was working on- to only export to Word and leave PDF generation to another point in future.

However things had changed now since Microsoft decided to add exporting to PDF and XPS document to its feature list starting from Office 2010. Of course we can use this in our applications by working with the Office application’s COM-based object model. This is a very handy method that converts a Word document to a PDF document.

public static void SaveAsPDF(string wordFilePath, string PdfFilePath)
{
    Application word = null;
    Document document = null;

    FileInfo SrcFile = new FileInfo(wordFilePath);
    FileInfo DestFile = new FileInfo(PdfFilePath);
    if (SrcFile.Exists == false)
    {
        throw new Exception(wordFilePath + " doesn't exist.");
    }

    try
    {
        word = new Application();
        document = new Document();

        document = word.Documents.Add();
        word.Visible = false;

        document = word.Documents.Open(SrcFile.FullName);

        document.Activate();

        document.ExportAsFixedFormat
            (
            DestFile.FullName,
            Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF
            );


        document.Close(false);
        word.Quit();
    }
    catch (Exception ex)
    {
        try
        {
            document.Close(false);
            word.Quit();
        }
        catch (Exception)
        {
            //nothing
        }
        throw ex;
    }
}

The only thing that we need to change if we want to export to XPS is this line:

Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF //PDF
Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatXPS //XPS

Finally one good thing I noticed; – but I didn’t try out before- you don’t have to own and install Microsoft Office on your server to make use of this, I noticed that Microsoft Office Primary Interop Assemblies are available for download for free, I will much appreciate if someone try these out and give me his feedback.

Advertisements
This entry was posted in Software and tagged , , . Bookmark the permalink.

One Response to Convert Word documents to PDF and XPS documents in .Net

  1. Vikas says:

    Does not works if office is not installed on the machine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s