[Libreoffice] SDK LibreOffice How to get document as object?

MarcinGutman mgutman at op.pl
Wed Nov 9 05:03:34 PST 2011


> I want to develop an addon...
> ...
> I already have some  converter-methods...
> ...
> The only thing I need is to get access
> to the opened document in some Java object(s).
>
For me it sounds like you have some advanced things but you lack the
L/OOo basics. Also I don't now how skilled you are so forgive me if
these things are obvious to you:

Use search on:
http://api.openoffice.org/
(which really is a google wrapper via "site:" tag,
i.e. google: "get writer document java site:openoffice.org"

In java you start with the office context to get the office desktop then
you can go deeper. In generally you take an object then query an
interface to obtain a "deeper" object:

Destkop:
=====
   XMultiComponentFactory xmcf = m_xContext.getServiceManager();
   Object desktopFrame = xmcf.createInstanceWithContext(
   "com.sun.star.frame.Desktop", m_xContext);
   desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
   desktopFrame);

I don't have an example of a text document so instead you have a
spreadsheet example:

Active sheet:
=====
   XDesktop desk = ...
   XComponent currCompon = desk.getCurrentComponent();
   XModel model =
   (XModel) UnoRuntime.queryInterface(XModel.class, currCompon);
   XController spreadshCtrl = model.getCurrentController();
   XSpreadsheetView spreadshView =
   (XSpreadsheetView) UnoRuntime.queryInterface(
   XSpreadsheetView.class, spreadshCtrl);
   XSpreadsheet sheet = spreadshView.getActiveSheet();

I don't know how skilled you are but I suggest to start with macro in
basic using MRI:
http://extensions.services.openoffice.org/project/MRI

With MRI you can figure out which interface you can obtain from which
object and vice versa. (Notice that MRI generates a code, use bottom
splitter to show a window with a code).

Also you can use:
http://api.openoffice.org/
e.g.
http://api.openoffice.org/docs/common/ref/com/sun/star/sheet/XSpreadsheet.html

Previous java code reflects this line in basic:
   ThisComponent.CurrentController.ActiveSheet

So, we can say that every "dot" is replaced by, more or less:
   XNextObject nextObject =
      (XNextObject) UnoRuntime.queryInterface(
      XNextObject.class, previousObject);

Finally, a tricky part is to know which interface can be queried from
which object and on what object you can cast an obtained interface.

> I'd strongly recommend doing it as native C++ in the core - it'll be
> very substantially faster, and of course we can then ship it with the
> core product.
>
As I mentioned before it depends on programming skills. I suggest to
start with a macro in basic to find all needed objects an interfaces.
Then translate it into java. Finally if then extension hits at least
10.000 downloads then rewrite it in C++ to merge it with the core
product.
Everybody are talking about speed but the truth is that in 90% of cases
the slowest part of the system is a user which uses only one finger to
write and must look at the keyboard to do this ;)

> Hope this will help you to better understand my requirements. I even 
> donot know if it IS possible or not? Perhaps there is no interface for
> this kind of data-processing.
>
Think about what data do you want to export e.g. do you want to get only
pure text or a text with paragraphs or even with tables. Does an export
format supports paragraphs and tables? If yes then you need to write a
function that maps paragraphs between formats. I.e.:
- get Writer document
- get all paragraphs
- create paragraphs in an export format
- loop over Writer document paragraphs to get text
- fill paragraphs in an export format using obtained text

At the beginning don't try to map all information from Writer document.
First try to get pure text from the first page and put it into exported
document. Then try to loop over all Writer pages. Then add paragraphs
and so on..

Of course at the very beginning you should write a macro in basic that
lists all pages from Writer document.

Best Regards,
Marcin

PS
Here
http://extensions.services.openoffice.org
find some extensions with available source code that export L/OOo
documents e.g.
http://extensions.services.openoffice.org/en/project/ooo2gd




More information about the LibreOffice mailing list