JavaScript and FilterData property when calling storeToURL()

Keyboard macros or custom scripts

JavaScript and FilterData property when calling storeToURL()

Postby gshklover » Sun Jan 01, 2012 4:06 pm

Hi.
I'm trying to use JavaScript to automate export .eps files from Draw (combining JavaScript learning with smth I actually need).
I've seen some examples from the web that do that in Basic or Java.
The issue I'm facing is when trying to pass filter-specific properties to the doc.storeToURL() method.
Here are the relevant lines:

Code: Select all   Expand viewCollapse view
var filter_props = new Array(1);
filter_props[0] = new PropertyValue();
... // filling filter-specific properties here

var store_props = new Array(1);
store_props[0] = new PropertyValue();
... // filling URL and media type here

store_props[3] = new PropertyValue();
store_props[3].Name = "FilterData";
store_props[3].Value = filter_props;

doc.storeToURL(url, store_props);


Trying to pass filter_props as a value for "FilterData" throws an exception from "[map_to_uno():any] UNO type not found: org.mozilla.javascript.NativeArray ..."

There are examples of such code in Java on the web. For Python, there appears to be explicit use of uno.Any() when assigning FilterData value.
Anybody knows how exactly this should work with JavaScript?

Thanks in advance.
OpenOffice 3.3
gshklover
 
Posts: 2
Joined: Sun Jan 01, 2012 3:52 pm

Re: JavaScript and FilterData property when calling storeToU

Postby rudolfo » Sun Jan 01, 2012 4:41 pm

I am not really working with JavaScript as macro language for OpenOffice, but I remember that there is a sample macro in Basis/share/Scripts/javascript called exportsheetstohtml.js. The following code is from there:
Code: Select all   Expand viewCollapse view
storeProps = new Array;//PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "HTML (StarCalc)";
storeUrl = xModel.getURL();
storeUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));
:
:
xStorable.storeToURL(storeUrl+"_sheet"+(i+1)+".html", storeProps);

Check the file in your OOo installation directory for details.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: JavaScript and FilterData property when calling storeToU

Postby gshklover » Sun Jan 01, 2012 5:19 pm

Thanks for you reply.
I should say that the issue is probably specific to the way "sequence<smth>" is handled in JavaScript.
There are no issues with other PropertyValues passed to doc.storeToURL() method. The issue is specific to "FilterData" property in that array - the property value should be a sequence of PropertyValue (in Java examples it is an array of PropertyValue), but trying to use Array for "filter_props" appears to trigger an exception when it is being translated to native interface.
OpenOffice 3.3
gshklover
 
Posts: 2
Joined: Sun Jan 01, 2012 3:52 pm

Re: JavaScript and FilterData property when calling storeToU

Postby hol.sten » Mon Jan 02, 2012 10:51 pm

gshklover wrote:Anybody knows how exactly this should work with JavaScript?

Intriguing question! I gave it a try but sadly to no avail.

Using the JavaScript examples delivered with OOo/LO I wrote this JavaScript macro:
Code: Select all   Expand viewCollapse view
// Import standard OpenOffice.org API classes. For more information on
// these classes and the OpenOffice.org API, see the OpenOffice.org
// Developers Guide at:
// http://api.openoffice.org/

importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextDocument);
importClass(Packages.com.sun.star.text.XText);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.com.sun.star.frame.XModel);
importClass(Packages.com.sun.star.frame.XStorable);
importClass(Packages.com.sun.star.beans.PropertyValue);

//get the document object from the scripting context
oDoc = XSCRIPTCONTEXT.getDocument();
//get the XTextDocument interface
xTextDoc = UnoRuntime.queryInterface(XTextDocument,oDoc);
//get the XModel interface from the document
xModel = UnoRuntime.queryInterface(XModel,oDoc);
//get the XStorable interface used to save the document
xStorable = UnoRuntime.queryInterface(XStorable,xTextDoc);

//set up an array of PropertyValue objects used to set the filter settings
filterProps = new Array();
filterProps[0] = new PropertyValue();
filterProps[0].Name = "PermissionPassword";
filterProps[0].Value = "nopermission";
filterProps[1] = new PropertyValue();
filterProps[1].Name = "UseLosslessCompression";
filterProps[1].Value = true;
filterProps[2] = new PropertyValue();
filterProps[2].Name = "SelectPdfVersion";
filterProps[2].Value = 1;
filterProps[3] = new PropertyValue();
filterProps[3].Name = "Magnification";
filterProps[3].Value = 4;
filterProps[4] = new PropertyValue();
filterProps[4].Name = "Zoom";
filterProps[4].Value = 250;

//set up an array of PropertyValue objects used to save the document
storeProps = new Array();
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "writer_pdf_Export";
storeProps[1] = new PropertyValue();
storeProps[1].Name = "Overwrite ";
storeProps[1].Value = true;
storeProps[2] = new PropertyValue();
storeProps[2].Name = "FilterData";
storeProps[2].Value = filterProps;
storeUrl = xModel.getURL();
storeUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));

xStorable.storeToURL(storeUrl+".pdf", storeProps);
Intriguingly I don't get any errors, but the last line just does nothing. No error, no exception, no result, nothing.

What does work is this example using no FilterData at all:
Code: Select all   Expand viewCollapse view
// Import standard OpenOffice.org API classes. For more information on
// these classes and the OpenOffice.org API, see the OpenOffice.org
// Developers Guide at:
// http://api.openoffice.org/

importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextDocument);
importClass(Packages.com.sun.star.text.XText);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.com.sun.star.frame.XModel);
importClass(Packages.com.sun.star.frame.XStorable);
importClass(Packages.com.sun.star.beans.PropertyValue);

//get the document object from the scripting context
oDoc = XSCRIPTCONTEXT.getDocument();
//get the XTextDocument interface
xTextDoc = UnoRuntime.queryInterface(XTextDocument,oDoc);
//get the XModel interface from the document
xModel = UnoRuntime.queryInterface(XModel,oDoc);
//get the XStorable interface used to save the document
xStorable = UnoRuntime.queryInterface(XStorable,xTextDoc);

//set up an array of PropertyValue objects used to save the document
storeProps = new Array();
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "writer_pdf_Export";
storeProps[1] = new PropertyValue();
storeProps[1].Name = "Overwrite ";
storeProps[1].Value = true;
storeUrl = xModel.getURL();
storeUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));

xStorable.storeToURL(storeUrl+".pdf", storeProps);
Although this works from a Writer document which has previously been saved, I know, this was not the question of this thread.

I tried both JavaScript macros with OOo 3.2.0 and LO 3.3.1 on Ubuntu 10.04. The first macro did not work at all, the second worked with OOo 3.2.0 and LO 3.3.1.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
hol.sten
Volunteer
 
Posts: 494
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: JavaScript and FilterData property when calling storeToU

Postby Charlie Young » Wed Jun 27, 2012 8:23 pm

hol.sten wrote:
gshklover wrote:Anybody knows how exactly this should work with JavaScript?

Intriguing question! I gave it a try but sadly to no avail.

Using the JavaScript examples delivered with OOo/LO I wrote this JavaScript macro:
Code: Select all   Expand viewCollapse view
// Import standard OpenOffice.org API classes. For more information on
// these classes and the OpenOffice.org API, see the OpenOffice.org
// Developers Guide at:
// http://api.openoffice.org/

importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextDocument);
importClass(Packages.com.sun.star.text.XText);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.com.sun.star.frame.XModel);
importClass(Packages.com.sun.star.frame.XStorable);
importClass(Packages.com.sun.star.beans.PropertyValue);

//get the document object from the scripting context
oDoc = XSCRIPTCONTEXT.getDocument();
//get the XTextDocument interface
xTextDoc = UnoRuntime.queryInterface(XTextDocument,oDoc);
//get the XModel interface from the document
xModel = UnoRuntime.queryInterface(XModel,oDoc);
//get the XStorable interface used to save the document
xStorable = UnoRuntime.queryInterface(XStorable,xTextDoc);

//set up an array of PropertyValue objects used to set the filter settings
filterProps = new Array();
filterProps[0] = new PropertyValue();
filterProps[0].Name = "PermissionPassword";
filterProps[0].Value = "nopermission";
filterProps[1] = new PropertyValue();
filterProps[1].Name = "UseLosslessCompression";
filterProps[1].Value = true;
filterProps[2] = new PropertyValue();
filterProps[2].Name = "SelectPdfVersion";
filterProps[2].Value = 1;
filterProps[3] = new PropertyValue();
filterProps[3].Name = "Magnification";
filterProps[3].Value = 4;
filterProps[4] = new PropertyValue();
filterProps[4].Name = "Zoom";
filterProps[4].Value = 250;

//set up an array of PropertyValue objects used to save the document
storeProps = new Array();
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "writer_pdf_Export";
storeProps[1] = new PropertyValue();
storeProps[1].Name = "Overwrite ";
storeProps[1].Value = true;
storeProps[2] = new PropertyValue();
storeProps[2].Name = "FilterData";
storeProps[2].Value = filterProps;
storeUrl = xModel.getURL();
storeUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));

xStorable.storeToURL(storeUrl+".pdf", storeProps);
Intriguingly I don't get any errors, but the last line just does nothing. No error, no exception, no result, nothing.


hanya a solved this at viewtopic.php?f=45&p=239690#p239683.

Change

Code: Select all   Expand viewCollapse view
filterProps = new Array();


to

Code: Select all   Expand viewCollapse view
filterProps = java.lang.reflect.Array.newInstance(PropertyValue, 5);
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: JavaScript and FilterData property when calling storeToU

Postby bprateek28 » Fri May 06, 2016 3:15 pm

Hi,

I am trying to convert Microsoft office files to pdf.
I need to add watermark, headers and footer using open office api

Thanks to Charlie Young's reply i was able to solve the watermark, but im not able to find any clue on how to set the position and also add headers and footers.

If i could get any help on them it would be great.

Code: Select all   Expand viewCollapse view
PropertyValue[] filterProps = (PropertyValue[]) java.lang.reflect.Array.newInstance(PropertyValue.class, 5);
  filterProps[0] = new PropertyValue();
  filterProps[0].Name = "UseLosslessCompression";
  filterProps[0].Value = true;
  filterProps[1] = new PropertyValue();
  filterProps[1].Name = "SelectPdfVersion";
  filterProps[1].Value = 1;
  filterProps[2] = new PropertyValue();
  filterProps[2].Name = "Magnification";
  filterProps[2].Value = 1;
  filterProps[3] = new PropertyValue();
  filterProps[3].Name = "Zoom";
  filterProps[3].Value = 100;
  filterProps[4] = new PropertyValue();
  filterProps[4].Name = "Watermark";
  filterProps[4].Value = "Draft";

  //set up an array of PropertyValue objects used to save the document
  PropertyValue[] storeProps = new PropertyValue[3];
  storeProps[0] = new PropertyValue();
  storeProps[0].Name = "FilterName";
  storeProps[0].Value = "writer_pdf_Export";
  storeProps[1] = new PropertyValue();
  storeProps[1].Name = "Overwrite ";
  storeProps[1].Value = true;
  storeProps[2] = new PropertyValue();
  storeProps[2].Name = "FilterData";
  storeProps[2].Value = filterProps;
  xStorable.storeToURL(storeUrl, storeProps);


I get the converted pdf but the watermark is portrait in position.
I would like to control where and how the watermark shows up on the page is it possible?

Thanks,
Prateek.
OpenOffice 4.1.0 on Windows 7
bprateek28
 
Posts: 2
Joined: Fri May 06, 2016 3:05 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: Google [Bot] and 3 guests