Java LibreOffice Programming Java LibreOffice Programming


Java LibreOffice Programming (JLOP) is intended for programmers who want to learn how to use the Java version of the LibreOffice API. This allows Java 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. I won't explain where to find a particular menu item to change text colour or run the spell checker. But I will explain how to do these kinds of things via API calls from Java programs. For instance, I 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, I 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 my aims was to develop utility code to help flatten the steep learning curve for the API. For example, my 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 my code work on LibreOffice and OpenOffice, which isn't difficult since their APIs are nearly identical. However, I must admit that at this stage I've restricted myself to testing the programs only on LibreOffice, and only on MS Windows.


Part 1: Basics

Chapter 1. LibreOffice API Concepts [Office program]

Topics: Some History; Help and Examples for the LibreOffice SDK (loDoc, loGuide); Office as a Process; Common Structures (Interface, Property, Service, and Component); Service and Interface Inheritance Hierarchies; the Frame-Controller-Model (FCM) Relationship; Extensions; Comparison with Basic.


Chapter 2. Starting and Stopping [Crash Report]

Topics: Starting Office; Closing Down/Killing Office; Opening a Document; Creating a Document; Saving; Closing; Document Conversion; Bug Detection and Reporting.


Chapter 3. Examining [MRI]

Topics: Examining Office; Getting and Setting Document Properties; Examining a Document for API Details; Examining a Document Using MRI.


Chapter 4. Listening, and Other Techniques

Topics: Window Listeners; Office Manipulation with JNA; Dispatching; Robot Keys.



Part 2: Writer Modules

Chapter 5. Text API Overview [Text Doc Services]

Topics: API Overview; Text Cursors; Extracting Text; Cursor Iteration; Creating Cursors; Creating a Document; Using and Comparing Text Cursors; Inserting/Changing Text in a Document; Text Enumeration; Appending Documents.


Chapter 6. Text Styles [Style Families]

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 [XSearchable]

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


Chapter 10. The Linguistics API [Lingu Services]

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 [Draw Services]

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 [Filling Rectangle]

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 [Bike Animation]

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


Chapter 15. Complex Shapes [Bezier curve]

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


Chapter 16. Making Slides [Dispatched shapes]

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 [Generated slide]

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 [Sheet Services]

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 [Sheet view]

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 [Styled sheet]

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


Chapter 23. Garlic Secrets [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 [Sheet 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 [Ram search]

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


Chapter 27. Functions and Data Analysis [Nonlinear]

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 [TableChart service]

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 [Col/Line chart]

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 [Donut chart]

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


Chapter 31. XY (Scatter) Charts [Scatter chart]

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


Chapter 32. Bubble, Net, Stock Charts [Net chart]

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 [Database modules]

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


Chapter 35. Examining Base Documents [Relation diagram]

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 [Inside ODB]

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 [Form structs]

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


Chapter 40. Building a Form Programmatically [Build a form]

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 [Form letter]

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 [SwingViewer]

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 [Generated poem]

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 [Add-on]

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 [Calc Add-in]

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


Chapter 48. Event Macros [LibreLogo]

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 [Macro dialog]

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 [XSLT filter]

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


Chapter 51. Simple ODF [ODF Text]

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).