Click here to Skip to main content
13,803,717 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


52 bookmarked
Posted 4 Mar 2008
Licenced BSD

Printing Documents from C# using OpenOffice Writer

, 7 May 2008
Rate this:
Please Sign up or sign in to vote.
Simple printing solution based on OpenOffice suite


When it comes to printing from C#, you have many options. But I found this solution with Open Office very nice, elegant and easy.

OpenOffice is a free Office suite. You can download it from the internet and use it for free. In this article, I will show you how to use it to load, modify and print text documents from a program in C#.


First of all, you should know that OpenOffice document is a zipped file. You can try to rename the sample.odt file (this file is contained in bin/Debug directory of my sample) to Now you will be able to unpack the file and inspect its contents. The most interesting file between the zip contents is the file content.xml. This file is a simple XML text file. You can extract it, modify and write back to If you rename back to sample.odt, you should be able to open the modified document back in OpenOffice.

We will be doing exactly the same steps in the source code.

Using the Code

All the work for manipulating OpenOffice document text file is in the class called Odt which stands for Open Document Text.

The constructor takes filename of the document to be opened. It extracts the zip contents using SharpZipLib and reads content.xml into XmlDocument class. The XmlDocument is then scanned for all input fields and these fields are placed in a collection that allows modifying fields text.

Odt doc = Odt("c:\\test.odt");
doc.Inputs["address"] = "My address";

You can modify the fields as you need. The resulting document can then be saved, opened in OpenOffice writer or printed on your default printer.


If something goes wrong, you should check that you have OpenOffice installed. The searching for OpenOffice is hardcoded in Odt.cs and you may want to change it to suit your needs via static variable:

Odt.OoExe = "path to your soffice.exe";>   

The attached sample program shows how easy is to use the Odt class. It displays all input fields of the document and has buttons for saving, opening and printing the modified document.



I hope you enjoyed reading this article. See you next time.


I have added support for spreadsheet documents. You can get it here.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralHere's my version Pin
DmitryDrosd22-Jan-09 20:32
memberDmitryDrosd22-Jan-09 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.181215.1 | Last Updated 7 May 2008
Article Copyright 2008 by psonek
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid