Home > Programming > ASP.NET Convert Word to PDF using OpenOfficePortable

ASP.NET Convert Word to PDF using OpenOfficePortable

February 17th, 2010 Leave a comment Go to comments

One of the projects at work involves converting uploaded Microsoft Word Document to PDF. My co-worker points me to an online article of doing this in Windows Form. The idea is very similar to Website using .NET.
http://www.codeproject.com/KB/office/PortableOpenOffice.aspx (PDF Version)

I follow HALF of its instructions

  1. Download the OpenOfficePortable
  2. Created the Library, Module and Function

…and that is it. I didn’t bother to use appSettings section and I don’t need to change the .ini file at all. Just create a simple .aspx page with C# code below to test it.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConvertToPDF._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label_Message" runat="server"></asp:Label>
        <asp:Button ID="Button_Submit" runat="server" OnClick="Button_Submit_Click" Text="Submit" /></div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics; //for running Process

namespace ConvertToPDF
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Button_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                //change this to your file paths
                string docFile = @"C:\Documents and Settings\User\My Documents\Visual Studio 2005\ConvertToPDF\ConvertToPDF\signature.doc";
                string pdfFile = @"C:\Documents and Settings\User\My Documents\Visual Studio 2005\ConvertToPDF\ConvertToPDF\signature.pdf";
                ConvertToPDF(docFile, pdfFile);
                this.Label_Message.Text = "Success";
            }
            catch (Exception ex)
            {
                this.Label_Message.Text = ex.Message;
            }
        }

        public void ConvertToPDF(string SourceFilename, string DestinationFilename)
        {
            string OOPDirectory = @"C:\OpenOfficePortable\"; //OOP = Open Office Portable
            string OOPExecutable = @"OpenOfficePortable.exe";
            string OOPConversionLibrary = @"ConversionLibrary";
            string OOPConversionModule = @"PDFConversion";
            string OOPConversionFunction = @"ConvertWordToPDF";

            Process p = new Process();
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.CreateNoWindow = true;
                startInfo.FileName = OOPDirectory + OOPExecutable;
                startInfo.Arguments = String.Format("-invisible macro:///{0}.{1}.{2}(\"{3}\",\"{4}\")",
                    OOPConversionLibrary, OOPConversionModule, OOPConversionFunction, SourceFilename, DestinationFilename);

                p.StartInfo = startInfo;
                p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
            finally
            {
                if (!p.HasExited)
                {
                    p.Kill();
                    p.WaitForExit();
                }
            }
        }
    }
}
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Categories: Programming Tags: ,
  1. Israfel
    February 25th, 2010 at 05:24 | #1

    Hi, Thanks for the great post, I’m trying to concatenate various pdf and word documents into one printable pdf and this was exactly what I was looking for. Unfortunately after a file is generated once the function won’t work again unless I restart my computer, the process has definitely exited when I step through, I’m stumped

  2. admin
    February 25th, 2010 at 10:36 | #2

    @Israfel
    The code above only convert a Word document to a PDF document. After the conversion is completed, the process is terminated in the background. It doesn’t concatenate PDF documents. I don’t know why you need to restart your computer. When I test it, I can click the button again and it will just generate the same PDF again.

  3. Ravi
    September 9th, 2011 at 01:49 | #3

    Hi,
    When word files having any images, it won’t come in converted pdf file…Am i missing anything?? or is there any workaround for this?

  4. Kumar
    January 16th, 2012 at 08:33 | #4

    It works in windows XP system but in Windows 2008 R2 not working.

  1. No trackbacks yet.