Getting started

Setting up a project

  • Start your development environment
  • Create a new project or load an existing one
  • Add a reference to the OpenOfficeLib assembly

The first program

This program demonstrates how to
  • set up the OpenOffice environment
  • create a new text document
  • set the content of the document
  • save the document
  • close and cleanup the environment

using System.IO;
using OOLib;
...

// instanciate the ServiceManager
// the ServiceManager is a singleton and can be created only once
// use ServiceManager.Instance to access it
var serviceManager = new ServiceManager();

// create the Desktop
var desktop = serviceManager.CreateDesktop();

// use the utility function to create a new text document
var textDoc = desktop.CreateTextDocument();

// set the content of the document
textDoc.Text.String = "Hello World";

// save the document to a file
var path = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "HelloWorld.odt");
textDoc.StoreAsFileName(path);

// read the document string back and write it to console
Console.WriteLine("Document string: {0}", textDoc.Text.String);

// print the Document Url to the console
Console.WriteLine("Document Url: {0}", textDoc.Url);

// wait for input
Console.WriteLine("Press key to continue ...");
Console.ReadKey();

// close the Desktop
desktop.Terminate();

// cleanup
textDoc = null;
desktop = null;
serviceManager = null;

More specialized samples

The following samples will all use the same start and end code section:

using System.IO;
using OOLib;
...

// instanciate the ServiceManager
// the ServiceManager is a singleton and can be created only once
// use ServiceManager.Instance to access it
var serviceManager = new ServiceManager();

// create the Desktop
var desktop = serviceManager.CreateDesktop();

// the sample code will be placed here

// wait for input
Console.WriteLine("Press key to continue ...");
Console.ReadKey();

// close the Desktop
desktop.Terminate();

// cleanup
textDoc = null;
desktop = null;
serviceManager = null;

Copy a bookmark with formating

This sample demonstartes how to:
  • Load a text document template
  • Bookmark and TextRange basics
  • Copying the contents of a bookmark with formating
// load the template
string path = Path.GetFullPath(Path.Combine(
   Path.GetDirectoryName(typeof(Program).Assembly.Location), 
   @"..\..\..\Templates\CopyBookmarkTemplate.ott"));
TextDocument textDoc = desktop.LoadComponentFromFile<TextDocument>(path);

// get the target range from bookmark
var targetBookmark = textDoc.Bookmarks["Target"];
// the traget range is the anchor of the bookmark
TextRange targetRange = targetBookmark.GetAnchor();

// wait for input
Console.WriteLine("Press '1' to paste before target");
Console.WriteLine("Press '2' to paste after target");
Console.WriteLine("Press any other key to replace target");
Console.WriteLine("Make your selection ...");
var ki = Console.ReadKey();

if (ki.KeyChar == '1')
{
   // get the start of the targetRange to insert before it
   targetRange = targetRange.GetStart();
}
else if (ki.KeyChar == '2')
{
   // get the end of the targetRange to insert after it
   targetRange = targetRange.GetEnd();
}
// on any other key the complete text range will be raplaced

Console.WriteLine();

// copy source to target bookmark
textDoc.CopyBookmarkContent("Source", targetRange);

Last edited Aug 31, 2011 at 12:29 PM by osre, version 2

Comments

No comments yet.