Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Printing Documents from C# using OpenOffice Writer

, 7 May 2008 BSD
Simple printing solution based on OpenOffice suite
odf_printing.zip
odf_printing
bin
Debug
ICSharpCode.SharpZipLib.dll
OdfPrinting.exe
sample.ods
sample.odt
ICSharpCode.SharpZipLib.dll
OdfPrinting.csproj.user
odf_printing_v2.zip
ICSharpCode.SharpZipLib.dll
OdfPrinting.exe
sample.ods
sample.odt
ICSharpCode.SharpZipLib.dll
OdfPrinting.csproj.user
odt_printing.zip
odt_printing
bin
Debug
ICSharpCode.SharpZipLib.dll
OdtPrinting.exe
sample.odt
ICSharpCode.SharpZipLib.dll
OdtPrinting.csproj.user
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form
{
	private System.ComponentModel.Container components = null;

	public Form1()
	{
		InitializeComponent();
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	protected override void Dispose( bool disposing )
	{
		if( disposing )
		{
			if (components != null) 
			{
				components.Dispose();
			}
		}
		base.Dispose( disposing );
	}

	#region Windows Form Designer generated code
	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.bLoad = new System.Windows.Forms.Button();
		this.bPrint = new System.Windows.Forms.Button();
		this.bOpen = new System.Windows.Forms.Button();
		this.bSave = new System.Windows.Forms.Button();
		this.SuspendLayout();
		// 
		// bLoad
		// 
		this.bLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
		this.bLoad.Location = new System.Drawing.Point(632, 200);
		this.bLoad.Name = "bLoad";
		this.bLoad.TabIndex = 0;
		this.bLoad.Text = "Load";
		this.bLoad.Click += new System.EventHandler(this.bLoad_Click);
		// 
		// bPrint
		// 
		this.bPrint.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
		this.bPrint.Enabled = false;
		this.bPrint.Location = new System.Drawing.Point(632, 232);
		this.bPrint.Name = "bPrint";
		this.bPrint.TabIndex = 1;
		this.bPrint.Text = "Print";
		this.bPrint.Click += new System.EventHandler(this.bPrint_Click);
		// 
		// bOpen
		// 
		this.bOpen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
		this.bOpen.Enabled = false;
		this.bOpen.Location = new System.Drawing.Point(632, 264);
		this.bOpen.Name = "bOpen";
		this.bOpen.TabIndex = 2;
		this.bOpen.Text = "Open";
		this.bOpen.Click += new System.EventHandler(this.bOpen_Click);
		// 
		// bSave
		// 
		this.bSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
		this.bSave.Enabled = false;
		this.bSave.Location = new System.Drawing.Point(632, 296);
		this.bSave.Name = "bSave";
		this.bSave.Size = new System.Drawing.Size(75, 40);
		this.bSave.TabIndex = 3;
		this.bSave.Text = "Save to\r\nc:\\test.odt";
		this.bSave.Click += new System.EventHandler(this.bSave_Click);
		// 
		// Form1
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.ClientSize = new System.Drawing.Size(720, 358);
		this.Controls.Add(this.bSave);
		this.Controls.Add(this.bOpen);
		this.Controls.Add(this.bPrint);
		this.Controls.Add(this.bLoad);
		this.Name = "Form1";
		this.Text = "Form1";
		this.ResumeLayout(false);

	}
	#endregion

	private System.Windows.Forms.Button bLoad;
	private System.Windows.Forms.Button bOpen;
	private System.Windows.Forms.Button bSave;
	private System.Windows.Forms.Button bPrint;

	private Odt odt;
	private Label[] keys;
	private TextBox[] values;

	[STAThread]
	static void Main() 
	{
		Application.Run(new Form1());
	}

	// Display textboxes for all input fields in document
	private void CreateInputControls()
	{
		Odt.OdtDocFields inputs = odt.Inputs;

		keys = new Label[inputs.Count];
		values = new TextBox[inputs.Count];

		int i = 0;
		foreach(string key in inputs.FieldNames)
		{
			Label l = keys[i] = new Label();
			l.TextAlign = ContentAlignment.MiddleRight;
			l.Top = i * 20 + 8;
			l.Left = 32;
			l.Width = 128;			
			l.Text = key;				// key is name of input field in OO document

			TextBox tb = values[i] = new TextBox();
			tb.Top = l.Top;
			tb.Left = l.Left + l.Width + 16;
			tb.Width = 256;
			tb.Text = inputs[key];		// default value - user will be able to alter it

			if(key == "print_date")		// default value for print_date field
			{
				tb.Text = String.Format("{0:yyyy-MM-dd}", DateTime.Now);
			}

			this.Controls.Add(l);
			this.Controls.Add(tb);

			i++;
		}
	}

	// Write new values to input fields
	private void WriteInputs()
	{
		Odt.OdtDocFields inputs = odt.Inputs;
		for(int i = 0; i < keys.Length; i++)
		{
			inputs[keys[i].Text] = values[i].Text;
		}
	}

	private void bLoad_Click(object sender, System.EventArgs e)
	{
		// Show dialog to let user select the template document
		OpenFileDialog dlg = new OpenFileDialog();
		dlg.Filter = "OpenOffice document (*.odt)|*.odt";
		dlg.FileName = Path.Combine(Application.StartupPath, "sample.odt");
		if(dlg.ShowDialog() != DialogResult.OK)
		{
			return;
		}

		// Constructor
		odt = new Odt(dlg.FileName);

		// Create controls for displaying document fields
		CreateInputControls();
		bSave.Enabled = bOpen.Enabled = bPrint.Enabled = true;
	}

	private void bPrint_Click(object sender, System.EventArgs e)
	{
		WriteInputs();
		odt.Print();
	}

	private void bSave_Click(object sender, System.EventArgs e)
	{
		WriteInputs();
		odt.Save("c:\\test.odt");
	}

	private void bOpen_Click(object sender, System.EventArgs e)
	{
		WriteInputs();
		odt.OpenInOo();
	}
}

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

Share

About the Author

psonek
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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 7 May 2008
Article Copyright 2008 by psonek
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid