[Solved] Export Writer document page to JPEG in JAVA

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
pashamacel
Posts: 1
Joined: Thu Jun 18, 2015 1:23 pm

[Solved] Export Writer document page to JPEG in JAVA

Post by pashamacel »

Hi!

I use java uno api 3.2.1 and try to convert my Writer document page into JPEG file. I use the next code for this:

Code: Select all

 XComponentLoader cLoader = getLoader();
    final Map<String, Object> defaultLoadProperties = new HashMap<String, Object>();
    defaultLoadProperties.put("Hidden", Boolean.TRUE);
    defaultLoadProperties.put("ReadOnly", Boolean.TRUE);

    XComponent sourceDoc = cLoader.loadComponentFromURL(loadUrl, "_blank", 0, toPropertyValues(defaultLoadProperties));
    try {
      XTextDocument xTextDocument = UnoRuntime.queryInterface(XTextDocument.class, sourceDoc);
      XDrawPageSupplier xDrawPagesSupplier = UnoRuntime.queryInterface(XDrawPageSupplier.class, xTextDocument);
      XDrawPage page = xDrawPagesSupplier.getDrawPage();
      XComponent xComp = UnoRuntime.queryInterface(XComponent.class, page);
      Object exportFilter = getServiceManager().createInstanceWithContext("com.sun.star.drawing.GraphicExportFilter", ooConnection);
      XExporter xExporter = UnoRuntime.queryInterface(XExporter.class, exportFilter);
      xExporter.setSourceDocument(xComp);
      XFilter xFilter = UnoRuntime.queryInterface(XFilter.class, xExporter);

      PropertyValue aFilterData_thumb[] = new PropertyValue[2];
      aFilterData_thumb[0] = new PropertyValue();
      aFilterData_thumb[0].Name = "PixelWidth";
      aFilterData_thumb[0].Value = 1241;

      aFilterData_thumb[1] = new PropertyValue();
      aFilterData_thumb[1].Name = "PixelHeight";
      aFilterData_thumb[1].Value = 1753;

      PropertyValue aProps_thumb[] = new PropertyValue[3];
      aProps_thumb[0] = new PropertyValue();
      aProps_thumb[0].Name = "MediaType";
      aProps_thumb[0].Value = "image/jpeg";

      aProps_thumb[1] = new PropertyValue();
      aProps_thumb[1].Name = "URL";
      aProps_thumb[1].Value = storeUrl;

      aProps_thumb[2] = new PropertyValue();
      aProps_thumb[2].Name = "FilterData";
      aProps_thumb[2].Value = aFilterData_thumb;

      xFilter.filter(aProps_thumb);
    } finally {
      closeDocument(sourceDoc);
    }
As a result I get an empty (white background) JPEG file ((((. Please, tell me what am I doing wrong.
Thanks.
P.S. Manually (by OO menu) I can export to JPEG correctly
Last edited by pashamacel on Fri Jun 19, 2015 11:48 am, edited 2 times in total.
LibreOffice 4.3.4.1m on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Export Writer document page to JPEG in JAVA

Post by Villeroy »

Print it out.
Put it on your desk.
Make a photo.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Export Writer document page to JPEG in JAVA

Post by Zizi64 »

P.S. Manually (by OO menu) I can export to JPEG correctly
The OO (Apache OpenOffice) have not .jpg export function. (The LibreOffice have .jpg (and .png) export function.)
viewtopic.php?f=20&t=63302

You can export the first page or the selected text (recorded Basic code):

Code: Select all

sub W2jpg
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///C:/Documents... ... writer2picture.jpg"
args1(1).Name = "FilterName"
args1(1).Value = "writer_jpg_Export"
args1(2).Name = "FilterData"
args1(2).Value = Array(Array("PixelWidth",0,793,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PixelHeight",0,1122,com.sun.star.beans.PropertyState.DIRECT_VALUE))
args1(3).Name = "SelectionOnly"
args1(3).Value = true

dispatcher.executeDispatch(document, ".uno:ExportTo", "", 0, args1())
end sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Post Reply