[Solved] Export Writer document page to JPEG in JAVA

Creating a macro - Writing a Script - Using the API

[Solved] Export Writer document page to JPEG in JAVA

Postby pashamacel » Thu Jun 18, 2015 1:36 pm

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   Expand viewCollapse view
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
pashamacel
 
Posts: 1
Joined: Thu Jun 18, 2015 1:23 pm

Re: Export Writer document page to JPEG in JAVA

Postby Villeroy » Thu Jun 18, 2015 7:18 pm

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27200
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Export Writer document page to JPEG in JAVA

Postby Zizi64 » Thu Jun 18, 2015 9:30 pm

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.)
https://forum.openoffice.org/en/forum/v ... 20&t=63302

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

Code: Select all   Expand viewCollapse view
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; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8318
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 1 guest