Libre

Libre
Delphi library for automating LibreOffice.
  • complete LibreOffice API supported
  • available for Delphi 7 - 10.2
  • source code included in registered version
  • distributing library in applications is royalty free

Download and order

Order Libre license $120,- USD (license for one developer)
Order Libre multilicense $360,- USD (license for all developers in company)
Order Libre year upgrades $60,- USD (registered users only)
Order Libre year upgrades multilicense $80,- USD (registered multilicense users only)

FAQ

How can I close spreadsheet or text document?
var
  SpreadsheetDocument: XSpreadsheetDocument;
  TextDocument: XTextDocument;
  Closeable: XCloseable;

Closeable := CoXCloseable.Create(SpreadsheetDocument);
Closeable.close(False);

Closeable := CoXCloseable.Create(TextDocument);
Closeable.close(False);
							
How can I create, modify or delete spreadsheets?
var
  Libre: ILibre;
  SpreadsheetDocument: XSpreadsheetDocument;
  Spreadsheets: XSpreadsheets;
  Spreadsheet: XSpreadsheet;
  Cell: XCell;

Libre := CoLibre.Create;

SpreadsheetDocument := Libre.CreateSpreadsheetDocument;
Spreadsheets := SpreadsheetDocument.getSheets;

// create new spreadsheets
Spreadsheets.insertNewByName('MySpreadsheet1', 0);
Spreadsheets.insertNewByName('MySpreadsheet2', 0);

// modify content of spreadsheet
Spreadsheet := CoXSpreadsheet.Create(Spreadsheets.getByName('MySpreadsheet1'));
Cell := Spreadsheet.getCellByPosition(2, 2);
Cell.setFormula('Hello, world!');

// delete spreadsheet
Spreadsheets.removeByName('MySpreadsheet1');
							
How can I set cell properties?
var
  Spreadsheet: XSpreadsheet;
  Cell: XCell;
  PropertySet: XPropertySet;

Cell := Spreadsheet.getCellByPosition(2, 2);
Cell.setFormula('Hello, world!');

PropertySet := CoXPropertySet.Create(Cell);
PropertySet.setPropertyValue('CharColor', $003399);
PropertySet.setPropertyValue('CharHeight', 20);
PropertySet.setPropertyValue('ParaLeftMargin', 500);
PropertySet.setPropertyValue('IsCellBackgroundTransparent', False);
PropertySet.setPropertyValue('CellBackColor', $99CCFF);
							
How can I set column width?
var
  Spreadsheet: XSpreadsheet;
  CellRange: XCellRange;
  ColumnRowRange: XColumnRowRange;
  TableColumns: XTableColumns;
  Column: Variant;
  PropertySet: XPropertySet;

CellRange := Spreadsheet.getCellRangeByName('C1');
ColumnRowRange := CoXColumnRowRange.Create(CellRange);
TableColumns := ColumnRowRange.getColumns;
Column := TableColumns.getByIndex(0);

PropertySet := CoXPropertySet.Create(Column);
PropertySet.setPropertyValue('Width', 5000);
							
How can I retrieve service names?
var
  Libre: ILibre;
  ServiceNames: TWideStringDynArray;
  I: Integer;

Libre := CoLibre.Create;
ServiceNames := Libre.MultiServiceFactory.getAvailableServiceNames;
for I := 0 to Length(ServiceNames) - 1 do
  Memo.Lines.Add(ServiceNames[I]);
							
How can I retrieve bookmarks?
var
  Libre: ILibre;
  TextDocument: XTextDocument;
  BookmarksSupplier: XBookmarksSupplier_2;
  NameAccess: XNameAccess;
  ElementNames: TWideStringDynArray;
  I: Integer;

Libre := CoLibre.Create;
TextDocument := Libre.OpenTextDocument(GetCurrentDir + '\document.odt');
BookmarksSupplier := CoXBookmarksSupplier_2.Create(TextDocument);
NameAccess := BookmarksSupplier.getBookmarks;
ElementNames := NameAccess.getElementNames;
for I := 0 to Length(ElementNames) - 1 do
  ShowMessage('Bookmark ' + IntToStr(I + 1) + ': ' + ElementNames[I]);
							
How can I select text?
var
  Libre: ILibre;
  TextDocument: XTextDocument;
  TextViewCursor: XTextViewCursor;
  Model: XModel;
  Controller: XController;
  TextViewCursorSupplier: XTextViewCursorSupplier;

Libre := CoLibre.Create;
TextDocument := Libre.OpenTextDocument(GetCurrentDir + '\document.odt');

Model := CoXModel.Create(TextDocument);
Controller := Model.getCurrentController;

TextViewCursorSupplier := CoXTextViewCursorSupplier.Create(Controller);
TextViewCursor := TextViewCursorSupplier.getViewCursor;

TextViewCursor.gotoStart(False);
TextViewCursor.gotoEnd(True);
							
How can I move cursor to bookmark?
var
  BookmarksSupplier: XBookmarksSupplier_2;
  NameAccess: XNameAccess;
  Element: Variant;
  TextContent: XTextContent;
  TextViewCursor: XTextViewCursor;

NameAccess := BookmarksSupplier.getBookmarks;
Element := NameAccess.getByName(NameAccess.getElementNames[0]); // the first bookmark
TextContent := CoXTextContent.Create(Element);
TextViewCursor.gotoRange(TextContent.getAnchor, False);
							
How can I hide or disable Writer window?
var
  Libre: ILibre;
  TextDocument: XTextDocument;
  Controller: XController;
  Frame: XFrame;
  Window: XWindow;

Libre := CoLibre.Create;
TextDocument := Libre.CreateTextDocument;
Controller := TextDocument.getCurrentController;
Frame := Controller.getFrame;
Window := Frame.getContainerWindow;
Window.setEnable(False); // disable window
Window.setVisible(False); // hide window
Window.setEnable(True); // enable window
Window.setVisible(True); // show window
							
How can I minimize or maximize Writer window?
var
  Libre: ILibre;
  TextDocument: XTextDocument;
  Controller: XController;
  Frame: XFrame;
  Window: XWindow;
  TopWindow: XTopWindow2;

Libre := CoLibre.Create;
TextDocument := Libre.CreateTextDocument;
Controller := TextDocument.getCurrentController;
Frame := Controller.getFrame;
Window := Frame.getContainerWindow;
TopWindow := CoXTopWindow2.Create(Window);
TopWindow.IsMinimized := True; // minimize
TopWindow.IsMaximized := True; // maximize
							
How can I create and show message box?
var
  Libre: ILibre;
  TextDocument: XTextDocument;
  Controller: XController;
  Frame: XFrame;
  Window: XWindow;
  WindowPeer: XWindowPeer;
  Toolkit: XToolkit2;
  MessageBoxFactory: XMessageBoxFactory;
  MessageBox: XMessageBox;

Libre := CoLibre.Create;
TextDocument := Libre.CreateTextDocument;
Controller := TextDocument.getCurrentController;
Frame := Controller.getFrame;
Window := Frame.getContainerWindow;
WindowPeer := CoXWindowPeer.Create(Window);
Toolkit := CoXToolkit2.Create(WindowPeer.getToolkit);
MessageBoxFactory := Toolkit.XMessageBoxFactory;
MessageBox := MessageBoxFactory.createMessageBox(WindowPeer, MessageBoxType_INFOBOX,
  MessageBoxButtons_BUTTONS_OK, 'Title', 'Message');
MessageBox.execute;
							

Related links