Libre

Libre

Delphi library for automating LibreOffice.
  • complete LibreOffice API supported
  • available for Delphi 7 - 12
  • 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 multi-license $360 USD (license for all developers in company)
Order Libre year upgrades $60 USD (registered users only)
Order Libre year upgrades multi-license $80 USD (registered multi-license users only)

FAQ

How can I close spreadsheet or text document?
const Closeable: XCloseable = CoXCloseable.Create(SpreadsheetDocument);
Closeable.close(False);

const Closeable: XCloseable = CoXCloseable.Create(TextDocument);
Closeable.close(False);
							
How can I create, modify or delete spreadsheets?
const Libre: ILibre = CoLibre.Create;
const Spreadsheets = Libre.CreateSpreadsheetDocument.getSheets;

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

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

// delete spreadsheet
Spreadsheets.removeByName('MySpreadsheet1');
							
How can I set cell properties?
const Cell = Spreadsheet.getCellByPosition(2, 2);
Cell.setFormula('Hello, world!');

const PropertySet: XPropertySet = 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 select cells?
const Model: XModel = CoXModel.Create(SpreadsheetDocument);
const SelectionSupplier: XSelectionSupplier = CoXSelectionSupplier.Create(Model.getCurrentController);

const CellRange = Spreadsheet.getCellRangeByName('A1:C3');
SelectionSupplier.select(CellRange.ComObject);
							
How can I set column width?
const CellRange = Spreadsheet.getCellRangeByName('C1');
const ColumnRowRange: XColumnRowRange = CoXColumnRowRange.Create(CellRange);
const Column = ColumnRowRange.getColumns.getByIndex(0);

const PropertySet: XPropertySet = CoXPropertySet.Create(Column);
PropertySet.setPropertyValue('Width', 5000);
							
How can I retrieve service names?
const Libre: ILibre = CoLibre.Create;
for var ServiceName in Libre.MultiServiceFactory.getAvailableServiceNames do
  ShowMessage(ServiceName);
							
How can I select text?
const Libre: ILibre = CoLibre.Create;
const TextDocument = Libre.OpenTextDocument('C:\document.odt');
const Model: XModel = CoXModel.Create(TextDocument);

const TextViewCursorSupplier: XTextViewCursorSupplier = CoXTextViewCursorSupplier.Create(Model.getCurrentController);
const TextViewCursor = TextViewCursorSupplier.getViewCursor;

TextViewCursor.gotoStart(False);
TextViewCursor.gotoEnd(True);
							
How can I retrieve bookmarks?
const Libre: ILibre = CoLibre.Create;
const TextDocument = Libre.OpenTextDocument('C:\document.odt');
const BookmarksSupplier: XBookmarksSupplier_2 = CoXBookmarksSupplier_2.Create(TextDocument);

for var ElementName in BookmarksSupplier.getBookmarks.getElementNames do
  ShowMessage(ElementName);
							
How can I move cursor to bookmark?
const Libre: ILibre = CoLibre.Create;
const TextDocument = Libre.OpenTextDocument('C:\document.odt');

const BookmarksSupplier: XBookmarksSupplier_2 = CoXBookmarksSupplier_2.Create(TextDocument);
const Bookmarks = BookmarksSupplier.getBookmarks;
const Element = Bookmarks.getByName(Bookmarks.getElementNames[0]); // the first bookmark
const TextContent: XTextContent = CoXTextContent.Create(Element);

const Model: XModel = CoXModel.Create(TextDocument);
const TextViewCursorSupplier: XTextViewCursorSupplier = CoXTextViewCursorSupplier.Create(Model.getCurrentController);
TextViewCursorSupplier.getViewCursor.gotoRange(TextContent.getAnchor, False);
							
How can I hide or disable Writer window?
const Libre: ILibre = CoLibre.Create;
const TextDocument = Libre.CreateTextDocument;
const Window = TextDocument.getCurrentController.getFrame.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?
const Libre: ILibre = CoLibre.Create;
const TextDocument = Libre.CreateTextDocument;
const TopWindow: XTopWindow2 = CoXTopWindow2.Create(TextDocument.getCurrentController.getFrame.getContainerWindow);
TopWindow.IsMinimized := True; // minimize
TopWindow.IsMaximized := True; // maximize
							
How can I create and show message box?
const Libre: ILibre = CoLibre.Create;
const TextDocument = Libre.CreateTextDocument;
const WindowPeer: XWindowPeer = CoXWindowPeer.Create(TextDocument.getCurrentController.getFrame.getContainerWindow);
const Toolkit: XToolkit2 = CoXToolkit2.Create(WindowPeer.getToolkit);
const MessageBox = Toolkit.XMessageBoxFactory.createMessageBox(WindowPeer, MessageBoxType_INFOBOX,
  MessageBoxButtons_BUTTONS_OK, 'Title', 'Message');
MessageBox.execute;
							
How can I specify page range for PDF export?
const Libre: ILibre = CoLibre.Create;
const TextDocument = Libre.OpenTextDocument('C:\document.odt', True, False, True);

const FilterName: PropertyValue = CoPropertyValue.Create(Libre.IdlReflection);
FilterName.Name := 'FilterName';
FilterName.Value := 'writer_pdf_Export';

const FilterOptions: PropertyValue = CoPropertyValue.Create(Libre.IdlReflection);
FilterOptions.Name := 'FilterOptions';
FilterOptions.Value := '{"PageRange": {"type": "string", "value": "1-2"}}';

const Storable: XStorable = CoXStorable.Create(TextDocument);
Storable.storeToURL(ToFileUrl('C:\document.pdf'), VarArrayOf([FilterName.ComObject, FilterOptions.ComObject]));
							

Related links