AODL example 17

From Apache OpenOffice Wiki
Revision as of 20:39, 4 March 2007 by LarsBehr (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Create a new TextDocument and use the OpenOfficeLib to do a print out

This example NUnit test will show how to create a new OpenDocument text document by using AODL and send it to a printer by using the new OpenOfficeLib.

Remarks: The OpenOfficeLib is an add on library for AODL. It's a simple wrapper for the CLI UNO assemblies which are part of OpenOffice.org since vers. 1.9.x. In contrast to the AODL library the OpenOfficeLib will need an existing OpenOffice.org installation in order to work!


using System;
using NUnit.Framework;
using System.Xml;
using AODL.Document.Content.Tables;
using AODL.Document.TextDocuments;
using AODL.Document.Styles;
using AODL.Document.Content.Text;
using OpenOfficeLib.Connection;
using OpenOfficeLib.Document;
using OpenOfficeLib.Printer;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;

namespace AODLTest
{
	/// <summary>
	/// OpenOfficeLibTests
	/// </summary>
	[TestFixture]
	public class OpenOfficeLibTests
	{
		/// <summary>
		/// Creates the new document and print it out.
		/// </summary>
		[Test (Description="Simple print test. OpenOffice.org installation and printer must exist."), Explicit]
		public void CreateNewDocumentAndDoAPrintOut()
		{
			string fileToPrint = "fileToPrint.odt";

			//Create a new text document
			TextDocument document = new TextDocument();
			document.New();
			//Create a standard paragraph using the ParagraphBuilder
			Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(document);
			//Add some simple text
			paragraph.TextContent.Add(new SimpleText(document, "Some simple text!"));
			//Add the paragraph to the document
			document.Content.Add(paragraph);
			//Save empty
			document.SaveTo(fileToPrint);

			//Now print the new document via the OpenOfficeLib
			//Get the Component Context
			XComponentContext xComponentContext = Connector.GetComponentContext();
			//Get a MultiServiceFactory
			XMultiServiceFactory xMultiServiceFactory = Connector.GetMultiServiceFactory(xComponentContext);
			//Get a Dektop instance		
			XDesktop xDesktop = Connector.GetDesktop(xMultiServiceFactory);
			//Convert a windows path to an OpenOffice one
			fileToPrint = Component.PathConverter(fileToPrint);
			//Load the document you want to print
			XComponent xComponent = Component.LoadDocument(
				(XComponentLoader)xDesktop, fileToPrint, "_blank");
			//Print the XComponent
			Printer.Print(xComponent);
		}
	}
}

Back to the AODL examples overview.

Personal tools