ASP.NET Convert Word to PDF using OpenOfficePortable
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
- Download the OpenOfficePortable
- 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();
}
}
}
}
}
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
@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.
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?
It works in windows XP system but in Windows 2008 R2 not working.