Click here to Skip to main content
15,881,248 members
Articles / Productivity Apps and Services / Microsoft Office

Printing Documents from C# using OpenOffice Writer

Rate me:
Please Sign up or sign in to vote.
4.00/5 (19 votes)
7 May 2008BSD2 min read 116.5K   6.8K   53  
Simple printing solution based on OpenOffice suite
using System;
using System.Xml;
using System.Collections;

// Class for printing of odt files in OpenOffice
public class Odt : Odf
{
	OdtDocFields inputs;

	public Odt(string odtFile) : base(odtFile)
	{
	}

	#region Collections

	// Collection used to access document input fields
	public class OdtDocFields
	{
		private Odt odt;
		private Hashtable ht;
		private string[] fieldNames;

		public OdtDocFields(Odt odt)
		{
			this.odt = odt;
			this.ht = new Hashtable();
		}

		public int Count
		{
			get
			{
				return ht.Count;
			}
		}

		public string this[string fieldName]
		{
			get
			{
				return ((XmlNode)(ht[fieldName])).InnerText;
			}
			set
			{
				((XmlNode)(ht[fieldName])).InnerText = (string) value;
			}
		}

		public void AddNode(XmlNode node)
		{
			string desc = node.Attributes["text:description"].Value;
			ht[desc] = node;
		}

		public string[] FieldNames
		{
			get
			{
				if(fieldNames == null)
				{
					fieldNames = new string[ht.Count];
					ht.Keys.CopyTo(fieldNames, 0);
				}
				return fieldNames;
			}
		}
	}

	#endregion

	public OdtDocFields Inputs
	{
		get
		{
			return inputs;
		}
	}

	protected override string GetFileExt()
	{
		return "odt";
	}

	protected override string GetOoAppType()
	{
		return "writer";
	}

	private void PopulateInputs(XmlNode node)
	{
		if(node.Name == "text:text-input")
		{
			inputs.AddNode(node);
		}
		foreach(XmlNode child in node.ChildNodes)
		{
			PopulateInputs(child);
		}
	}

	public override void Load(string odtTemplateFile)
	{
		base.Load(odtTemplateFile);

		// Add all input fields in collection
		inputs = new OdtDocFields(this);
		PopulateInputs(doc.DocumentElement);
	}
}

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 BSD License


Written By
Software Developer ODP-software spol. s r. o.
Czech Republic Czech Republic
Started programming on Z80. Later on PC i started playing with Linux and joined the DotGNU project. Here i implemented debugger and fixed a few easy bugs. I have also started project PortableStudio which will be DotGNU's IDE. At work i am developing ticket system for Czech Railways and systems for on board sales for Czech Airways.

Comments and Discussions