Click here to Skip to main content
15,897,718 members
Articles / Programming Languages / XML

Converting Microsoft Word Document to PDF format using OpenOffice.org (Portable)

Rate me:
Please Sign up or sign in to vote.
3.67/5 (18 votes)
6 Oct 2006CPOL5 min read 313.5K   7.3K   65  
An article on using OpenOffice.org Portable to convert a Word document to PDF without requiring you to install OpenOffice.org on client machines.
using System;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace PortableOpenOffice
{
	public class OpenOffice
	{
		private static OpenOffice mInstance;
		public static OpenOffice Instance
		{
			get
			{
				if(mInstance==null)
					mInstance=new OpenOffice();
				return mInstance;
			}
		}
		private OpenOffice()
		{
		}

		public void ConvertToPDF(string SourceFilename,string DestinationFilename)
		{
			ProcessStartInfo startInfo=new ProcessStartInfo();
			startInfo.CreateNoWindow=true;
			startInfo.FileName=GetPortableOpenOfficeExecutable();
			startInfo.Arguments=String.Format("-invisible macro:///"+ConversionLibrary+"."+ConversionModule+"."+ConversionFunction+"(\"{0}\",\"{1}\")",
				SourceFilename,DestinationFilename);
			Process p=new Process();
			p.StartInfo=startInfo;
			p.Start();
			p.WaitForExit();
		}
		private string GetApplicationDirectory()
		{
			return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName)+@"\";
		}
		private string GetPortableOpenOfficeExecutable()
		{
			if((ConfigurationSettings.AppSettings["PortableOpenOfficeDirectory"]==null)||
				(ConfigurationSettings.AppSettings["PortableOpenOfficeExecutable"]==null))
				throw new Exception("Settings variables not configured: PortableOpenOfficeDirectory,PortableOpenOfficeExecutable");

			return GetApplicationDirectory()+ConfigurationSettings.AppSettings["PortableOpenOfficeDirectory"]+
				ConfigurationSettings.AppSettings["PortableOpenOfficeExecutable"];
		}
		private string ConversionLibrary
		{
			get
			{
				if(ConfigurationSettings.AppSettings["PortableOpenOfficeConversionLibrary"]==null)
					throw new Exception("Settings variable not configured: PortableOpenOfficeConversionLibrary");

				return ConfigurationSettings.AppSettings["PortableOpenOfficeConversionLibrary"];
			}
		}
		private string ConversionModule
		{
			get
			{
				if(ConfigurationSettings.AppSettings["PortableOpenOfficeConversionModule"]==null)
					throw new Exception("Settings variable not configured: PortableOpenOfficeConversionModule");

				return ConfigurationSettings.AppSettings["PortableOpenOfficeConversionModule"];
			}
		}
		private string ConversionFunction
		{
			get
			{
				if(ConfigurationSettings.AppSettings["PortableOpenOfficeConversionFunction"]==null)
					throw new Exception("Settings variable not configured: PortableOpenOfficeConversionFunction");

				return ConfigurationSettings.AppSettings["PortableOpenOfficeConversionFunction"];
			}
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Mexico Mexico
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions