Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Professional Delphi Library for Automating the LibreOffice

Intro

LibreOffice is one of the most used, popular, powerful, and free office suite, used by millions of people around the world. Moreover, deployed so many business solutions to automate or process documents.

Since LibreOffice is open-source and free, there are already available libraries to automate and work with LibreOffice from several programming languages. To connect your Delphi projects to LibreOffice we can use Libre library by WINSOFT that offers you a complete API. 

Libre by WINSOFT is a commercial library and gives complete support for LibreOffice APIs and upgrade options to new APIs. Here you can register a license to distribute this library in your Delphi projects.

Specifications

Installation

Please, follow this short video on how to configure the Libre library to your RAD Studio

And be sure to install the LibreOffice suite to your PC, because without it you can not see your results. In this demo, we are using the latest LibreOffice version 7.0.0

Moreover, you should add the Libre unit to your uses list.

Development

Our demo is huge and we try to emphasize the essential parts to you. But, you can watch this action video on this demo. You can find more features in this library.

So, let’s start from a simple one, creating a text document. For this, you should declare and create instances of Libre interfaces.

procedure TFormMain.ButtonWriterHelloClick(Sender: TObject);
var
  Libre: ILibre;
  TextDocument: XTextDocument;
  Text: XText;
  Storable: XStorable;
begin
  Screen.Cursor := crHourGlass;
  try
    Libre := CoLibre.Create;
    TextDocument := Libre.CreateTextDocument;

    Text := TextDocument.getText;
    Text.setString('Hello, world!');

    // save document
    Storable := CoXStorable.Create(TextDocument);
    Storable.storeAsURL(ToFileUrl(GetCurrentDir + '\hello.odt'), EmptyVarArray);
  finally
    Screen.Cursor := crDefault;
  end;
end;

LibreOffice has some functions that minimize document processing. For instance, the replace function. Here is how we can handle this function in our text document.

procedure TFormMain.ButtonWriterReplaceClick(Sender: TObject);
var
  Libre: ILibre;
  TextDocument: XTextDocument;
  Text: XText;
  Replaceable: XReplaceable;
  ReplaceDescriptor: XReplaceDescriptor;
  ReplaceCount: Integer;
begin
  Screen.Cursor := crHourGlass;
  try
    Libre := CoLibre.Create;
    TextDocument := Libre.CreateTextDocument;

    Text := TextDocument.getText;
    Text.setString('Hello, world!');

    Replaceable := CoXReplaceable.Create(TextDocument);
    ReplaceDescriptor := Replaceable.createReplaceDescriptor;
    ReplaceDescriptor.setSearchString('l');
    ReplaceDescriptor.setReplaceString('L');
    ReplaceCount := Replaceable.replaceAll(ReplaceDescriptor);
    ShowMessage('Replace count: ' + IntToStr(ReplaceCount));
  finally
    Screen.Cursor := crDefault;
  end;
end;

Moreover, you can export your “odt” documents into PDF, DOC, DOCX, TXT, RTF. Here is the solution to convert the LibreOffice document into a PDF.

procedure TFormMain.ButtonWriterExportPdfClick(Sender: TObject);
begin
  WriterExport(GetCurrentDir + '\document2.odt', GetCurrentDir + '\document2.pdf', 'writer_pdf_Export');
end;

Additionally, with the Libre library you can work with LibreOffice Impress, LibreOffice Base, LibreOffice Draw, LibreOffice Calc. You have an option to create even math formulas and apply them to process data. 

Here is how we can create a spreadsheet and apply a formula.

procedure TFormMain.ButtonCalcFormulaClick(Sender: TObject);
var
  Libre: ILibre;
  SpreadsheetDocument: XSpreadsheetDocument;
  Spreadsheets: XSpreadsheets;
  Spreadsheet: XSpreadsheet;
  Cell: XCell;
  I: Integer;
begin
  Screen.Cursor := crHourGlass;
  try
    Libre := CoLibre.Create;
    SpreadsheetDocument := Libre.CreateSpreadsheetDocument;
    Spreadsheets := SpreadsheetDocument.getSheets;
    Spreadsheet := CoXSpreadsheet.Create(Spreadsheets.getByName(Spreadsheets.getElementNames[0]));

    for I := 1 to 10 do
    begin
      Cell := Spreadsheet.getCellByPosition(2, I);
      Cell.setFormula(IntToStr(I));
    end;

    Cell := Spreadsheet.getCellByPosition(2, 12);
    Cell.setFormula('=SUM(C2:C11)');
  finally
    Screen.Cursor := crDefault;
  end;
end;

Head over and check out the full WINSOFT Libre library.

Like what you see? You can get the Libre Component Library and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4.1 today!

Exit mobile version