Page 1 of 1

[Solved] Export Writer document page to JPEG in JAVA

PostPosted: Thu Jun 18, 2015 1:36 pm
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   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

Re: Export Writer document page to JPEG in JAVA

PostPosted: Thu Jun 18, 2015 7:18 pm
by Villeroy
Print it out.
Put it on your desk.
Make a photo.

Re: Export Writer document page to JPEG in JAVA

PostPosted: Thu Jun 18, 2015 9:30 pm
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.)
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