Click here to Skip to main content
Licence BSD
First Posted 4 Mar 2008
Views 52,798
Downloads 1,798
Bookmarked 44 times

Printing Documents from C# using OpenOffice Writer

By psonek | 7 May 2008
Simple printing solution based on OpenOffice suite
1 vote, 5.6%
1
3 votes, 16.7%
2
1 vote, 5.6%
3
3 votes, 16.7%
4
10 votes, 55.6%
5
3.97/5 - 18 votes
1 removed
μ 3.68, σa 2.40 [?]

Introduction

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

Background

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 sample.zip. 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 sample.zip. If you rename sample.zip 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.

doc.Save("c:\\test_modified.odt");
doc.OpenInOo();
doc.Print();

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.

odt_printing3.png

Thanks

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

Updates

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

License

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

About the Author

psonek

Software Developer
ODP-software spol. s r. o.
Czech Republic Czech Republic

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSilverlight version Pinmemberrobdal1:45 10 Dec '11  
QuestionDo you have any example that allows you to preview a print? Pinmemberamdacc23:56 18 Nov '10  
QuestionCan input field be used for images/graphics? Pinmemberchihuahua09658:22 13 Oct '10  
GeneralGood work.... Pinmemberanshulvaid1:44 13 Oct '10  
RantWonderful work, psonek Pinmembervovovulture17:12 4 Feb '10  
GeneralGreat job Pinmemberbalaji_s8423:37 25 Nov '09  
Questionsaving as .doc Pinmemberhollyhazen10:19 21 May '09  
AnswerRe: saving as .doc Pinmemberrobajz11:32 25 May '09  
GeneralVery simple and excellent work Pinmemberpvox22:50 23 Apr '09  
GeneralHere's my version PinmemberDmitryDrosd20:32 22 Jan '09  
QuestionGood Work... But a small bug PinmemberMember 458863920:41 29 Oct '08  
QuestionGreat Work Pinmemberskjpatel9:07 13 May '08  
GeneralGood work PinmemberAlePu21:42 12 May '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120206.1 | Last Updated 7 May 2008
Article Copyright 2008 by psonek
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid