LibreOffice SDK Programming Guide

    From The Document Foundation Wiki

    Preface

    LibreOffice SDK Programming Guide (SDKGuide) is intended for programmers who want to learn how to use the the LibreOffice API. This allows developers to control and manipulate LibreOffice's text, drawing, presentation, spreadsheet, and database applications, and a lot more (e.g. its spell checker, forms designer, and charting tools).

    This book is not about how to use LibreOffice's GUI. It won't be explained where to find a particular menu item to change text color or run the spell checker. But it will be explained how to do these kinds of things via API calls from external programs. For instance, we describe a program that constructs a Word file full of randomly generated algebra questions, and show how a ASCII text file can be transformed into a slide presentation.

    Although most of the examples are Java programs that call the LibreOffice API, we briefly look at other kinds of programming, such as Java macros and new LibreOffice functionality, accessible via menu items or as spreadsheet functions, and the dispatching of commands to the GUI.

    One of the aims was to develop utility code to help flatten the steep learning curve for the API. For example, the Lo class simplifies the steps needed to initialize the API (by creating a connection to a LibreOffice process), to open/create a document, save it, and close down LibreOffice.

    Another aim was to have the code work on LibreOffice and OpenOffice, which isn't difficult since their APIs are nearly identical.

    Authors

    This guide is based on the Java LibreOffice programming (JLOP) book written by Dr. Andew Davison. It is based on the draft #2 - 20 March 2017, published in his website and also on Github with the license CC BY-SA.

    Table of Contents

    Part 1: Basics

    This chapter consists of 4 chapters. It focuses on Basics of LibreOffice SDK, providing insight on what is LibreOffice SDK, how it can be used, some examples for starting and stopping LibreOffice and examining documents. This part ends with a general introduction to Office programming by looking at several techniques that will reappear periodically in later chapters.

    Chapter 1: LibreOffice API Concepts

    This chapter describes LibreOffice API concepts without resorting to code. These concepts some History of LibreOffice, help and examples for the LibreOffice SDK (loDoc, loGuide). Office as a (possibly networked) process, common structures like interface, property, service, and componentthe two API inheritance hierarchies, and the Frame-Controller-Model (FCM) relationship. A brief comparison with BASIC is also provided.

    Chapter 2. Starting and Stopping

    This chapter focuses on the most fundamental tasks like starting Office, loading (or creating) a document, saving and closing the document, and shutting down Office. The DocConverter example at the end pulls these together to show how to convert a document into another format.

    Chapter 3. Examining

    This chapter looks at ways to examine the state of the Office application and a document. A document will be examined in three different ways: the first retrieves properties about the file, such as its author, keywords, and when it was last modified. The second and third approaches extract API details, such as what services and interfaces it uses. The MRI extension is also discussed here.

    Chapter 4. Listening, and Other Techniques

    This chapter concludes the general introduction to Office programming by looking at several techniques that will reappear periodically in later chapters: the use of window listeners, the manipulation of Office using the JNA library, dispatching messages to the Office GUI, and the Java Robot package.

    Part 2: Writer Modules

    The second part of the book, Writer Modules looks at programming with the text document part of the Office API.

    Chapter 5. Text API Overview

    This chapter begins with a quick overview of the text API, then a detailed look at text cursors for moving about in a document, extracting text, and adding/inserting new text.

    Chapter 6. Text Styles

    Topics: Five Style Families; Properties; Listing Styles; Creating a Style; Applying Styles; Paragraph/Word Styles; Hyperlink Styling; Text Numbering; Headers and Footers.

    Chapter 7. Text Content Other than Strings

    Topics: Accessing Text Content; Text Frames; Embedded Objects (Math Formulae); Text Fields; Text Tables; Bookmarks.

    Chapter 8. Graphic Content

    Topics: Graphics; Linked Images/Shapes.

    Chapter 9. Text Search and Replace

    Topics: Finding the First Matching Phrase; Replacing all the Matching Words; Finding all Matching Phrases.

    Chapter 10. The Linguistics API

    Topics: Linguistic Tools; Using the Spell Checker; Using the Thesaurus; Grammar Checking; Guessing the Language used in a String; Spell Checking and Grammar Checking a Document.

    Part 3: Draw & Impress Modules

    Chapter 11. Draw/Impress APIs Overview

    Topics: Draw Pages and Master Pages; Draw Page Details; API Hierarchy Code Examples; Shapes in a Drawing; Shapes in a Presentation ; The Slide Show APIs.

    Chapter 12. Examining a Draw/Impress Document

    Topics: Examining Slides/Pages; Page Layers; Styles.

    Chapter 13. Drawing Basic Shapes

    Topics: A Black Dashed Line; A Red Ellipse; Filled Rectangles; Text; Shape Names; A Transparent Circle and a Polar Line; A Math Formula as an OLE Shape; Polygons; Multiple Lines, Partial Elipses.

    Chapter 14. Animation

    Topics: Circle Movement; Line Rotation; Animating an Image; The Gallery Module.

    Chapter 15. Complex Shapes

    Topics: Connecting Two Rectangles; Shape Composition (grouping, binding, and combining); Combining with Dispatches; Undoing Composition; Bezier Curves (simple and complex).

    Chapter 16. Making Slides

    Topics: Creating Slides: title, subtitle, bullets, images, video, buttons; Shape Animations; Dispatch Shapes (special symbols, block arrows, 3D shapes, flowchart elements, callouts, and stars); Slide Viewing.

    Chapter 17. Slide Deck Manipulation

    Topics: Deck Building; Master Pages; Adding a Slide to a Deck; Rearranging a Deck; Appending Two Decks; Exporting a Slide as an Image; Extracting the Text from a Deck.

    Chapter 18. Slide Shows

    Topics: Starting a Slide Show; Play and End a Slide Show Automatically; Play a Slide Show Repeatedly; Play a Custom Slide Show.

    Part 4: Calc Modules

    Chapter 19. Calc API Overview

    Topics: The Spreadsheet Document; Document Spreadsheets; Spreadsheet Data; The Spreadsheet Service; Cell Range Services; Cell Services; Sheet Cell Ranges.

    Chapter 20. Spreadsheet Displaying and Creation

    Topics: Displaying a Document; Read-only and Protected Viewing; Active Sheets; Sheet Names; Zooming; Creating a Document; Cell Names and Ranges; Cell Values; Data Arrays; Rows and Columns of Data; Adding a Picture and a Chart.

    Chapter 21. Extracting Data

    Topics: Getting a Cell Value; Getting the Data from a Cell Range; Getting Rows and Columns of Data; Obtaining Cell Ranges by Using Queries; Finding the Used Area with Sheet Cursors.

    Chapter 22. Styles

    Topics: Obtaining Style Information: the TableCellStyle and TablePageStyle Services; Creating and Using a New Style; Adding Borders.

    Chapter 23. Garlic Secrets

    Topics: Freezing Rows; GeneralFunctions; Sheet Searching; Sheet Range Queries; Hidden Cells; Cell Merging; Splitting Windows; View Panes; View State Data; Active Panes; Inserting Rows and Columns; Shifting Cells.

    Chapter 24. Complex Data Manipulation

    Topics: Sorting Data; Generating Data: Automatic, LINEAR Mode, DATE Mode, GROWTH Mode; Fancy Text: borders, headlines, hyperlinks, annotations.

    Chapter 25. Monitoring Sheets

    Topics: Listening for Document Modifications (XModifyListener); Listening for Application Closing (XTopWindowListener); Listening for Cell Selection (XSelectionChangeListener).

    Chapter 26. Search and Replace

    Topics: XSearchable; XReplaceable; SearchDescriptor; ReplaceDescriptor; Searching Iteratively; Searching For All Matches; Replacing All Matches.

    Chapter 27. Functions and Data Analysis

    Topics: Calling Calc Functions from Code; Pivot Tables; Goal Seek; Linear and Nonlinear Solving (using SCO, DEPS).

    Part 5: Chart Modules

    Chapter 28. Chart2 API Overview

    Topics: Charting Elements; Chart Creation: TableChart, ChartDocument, linking template, diagram, and data source; Modifying Chart Elements: diagram, coordinate system, chart type, data series.

    Chapter 29. Column Charts

    Topics: Creating a Chart Title; Creating Axis Titles; Rotating Axis Titles; What Chart Templates are Available?; Multiple Columns; 3D Pizazz; The Column and Line Chart.

    Chapter 30. Bar, Pie, Area, Line Charts

    Topics: The Bar Chart; The Pie Chart; 3D Pie; Donuts; The Area Chart; The Line Chart: multiple lines.

    Chapter 31. XY (Scatter) Charts

    Topics: A Scatter Chart (with Regressions); Calculating Regressions; Drawing a Regression Curve; Changing Axis Scales; Adding Error Bars.

    Chapter 32. Bubble, Net, Stock Charts

    Topics: The Bubble Chart; The Net Chart; The Stock Chart: basic and modified candle sticks; Stock Chart and Line Graph.

    Chapter 33. Using Charts in Other Documents

    Topics: Copy-and-Paste Dispatches; Adding a Chart to a Text Document; Adding a Chart to a Slide Document; Saving the Chart as an Image.

    Part 6: Base Modules

    Chapter 34. From JDBC to the Base API

    Topics: Three Database Modules: sdbc, sdbcx, sdb; A Little JDBC; From JDBC to sdbc.

    Chapter 35. Examining Base Documents

    Topics: Querying an Embedded HSQLDB Database; Using Metadata when Querying; Debugging Database Code; Creating a Base Document; Processing Multiple SQL Commands; Displaying Table Relationships; Prepared Statements; Scrollable and Updatable Result Sets; Converting a Database to Text.

    Chapter 36. Using RowSets and Database Context

    Topics: Using RowSets; Using the Database Context; Copying Data From Base To Calc.

    Chapter 37. Using the Driver Manager

    Topics: Querying a CSV File; Querying an Access File; Querying a Calc Spreadsheet; Querying a Thunderbird Address Book.

    Chapter 38. Treating an ODB File as a Zipped Folder

    Topics: Extracting a Database from an ODB File; Querying an Extracted HSQLDB Database; Querying an Extracted Firebird Database.


    Part 7: Cross-application Modules

    Chapter 39. Forms API Overview

    Topics: An Overview of the Forms API; Examining a Text-based Form; Attaching Listeners to a View.

    Chapter 40. Building a Form Programmatically

    Topics: Creating Controls; Assigning a Data Source to a Form; Creating Data-aware Controls; Attaching Listeners.

    Chapter 41. Printing

    Topics: The Java Print Service (JPS); Printing in Office Writer, Impress, Calc; Viewing a Document's Print Properties; Specialized Printing: Writer, Impress, Calc; Hacking the GUI; Command Prompt Printing.

    Chapter 42. Sending E-mail

    Topics: SimpleSystemMail / SimpleCommandMail; Using the MailServiceProvider Service; Using JavaMail; The Desktop API; Thunderbird Scripting; Office Mail Merge.

    Chapter 43. Using the Clipboard

    Topics: The Office Clipboard API: adding, retrieving, text, images; Java's Clipboard API adding, retrieving, text, images, 2D arrays; Copy and Pasting in an Office Document: Writer, Calc, Impress, Base.

    Chapter 44. Office as a GUI Component

    Topics: OOoBean; Wrapping OOoBean in a JPanel; Using the OBeanPanel; Using an Undecorated Office Window; Augmenting the User Interface: a New Toolbar Item; Augmenting the User Interface: a New Menu Item.


    Part 8: Extending LibreOffice

    Chapter 45. Coding UNO Components

    Topics: Using the RandomSents Component; Writing IDL Definitions; Using idlc.bat; Merging Type Data into a Registry Database; Generating the Java Package and Interface; Creating a Skeleton Component; Compiling the Completed Implementation; Packaging the Component; Installing the Extension; Using a New Component in a Program.

    Chapter 46. Add-ons

    Topics: What is an Add-on?; Creating the EzHighlight Add-on; Creating a Partial Add-on Implementation; The FreeMarker-generated EzHighlightAddonImpl; Creating the Dialog; Configuring the Add-on; Building and Installing the OXT File.

    Chapter 47. Calc Add-ins

    Topics: Implementing a Calc Add-in; The IDL definitions of the Functions; XCU creation.

    Chapter 48. Event Macros

    Topics: Macro Locations; Naming Macro Functions; Calling Existing Macros; The LibreLogo Macro; Writing a Simple Event Macro; Automating the Assigning of Event Macros.

    Chapter 49. Extension and Document Event Macros

    Topics: Form Macros as an Extension; Loading an XML Dialog; Building a Dialog at Runtime; Storing Macros inside the (Form) Document; Attaching Macros to Other Events; Executing Macros from the Command Line.


    Part 9: The ODF Format

    Chapter 50. Importing XML

    Topics: XSLT Filters; Using Filters with Java; Alternatives to XSLT Filters: DOM Parsing, Node and Attribute Data Extraction, JAXB Conversion.

    Chapter 51. Simple ODF

    Topics: The OpenDocument Format; Doc Information; Unzipping an ODF Doc; the Simple Java API for ODF (Apache ODF Toolkit): making docs (text, sheet, and slides), slide movement, doc concatenation (text, sheet, and slides).

    Cc-by new white.svgCc-sa white.svg

    This work is licensed under the Attribution Share-Alike 4.0 Unported License