Hi Mathias,
> In a calc document i want to place a button with that the user can print
> the actual sheet or rather the defined (and named) print area at the
> sheet. But i can't find the right print options to do that.
please see the code attached. It is free, again.
Greetings, Tobias
package de.twc.oocom.oo;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XComponent;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.style.XStyle;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.view.PrintableState;
import com.sun.star.view.XPrintJobBroadcaster;
import com.sun.star.view.XPrintable;
import de.twc.oocom.oo.calc.OOCalcDoc;
import de.twc.oocom.oo.text.OOTextDoc;
import de.twc.oocom.server.PrintJobListener;
/**
* @author tobias
*
*/
public class OOPrint {
/**
* OpenOffice.org representative of the opened file.
*/
private XComponent xComponent = null;
/**
* Turns on the debug mode: 0 = absolute quiet, 1 = normal, 2 = verbose.
*/
private static int debug = 0;
public OOPrint (XComponent myXComponent) {
this.xComponent = myXComponent;
}
/**
* This method sets the printer tray of the loaded Document.
*
* @param myTray The paper tray the printer should use
* [EMAIL PROTECTED] de.twc.oocom.OOCom#tray1}
*/
public void setPrinterTray(String printerTray)
{
if (debug > 0)
System.out.print("Setting printer tray " + printerTray + " ... ");
try {
// Get application name of the loaded document
String applicationName =
OODocument.getApplicationName(this.xComponent);
// If loaded document is a Text Document
if (applicationName.equals("com.sun.star.text.TextDocument")) {
// Get the text document
XTextDocument xTextDocument = (XTextDocument)
UnoRuntime.queryInterface(XTextDocument.class,
this.xComponent);
if(xTextDocument == null) {
if (debug > 0)
System.out.println("failed.");
return;
}
// Get the StyleFamiliesSupplier interface of the document
XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
xTextDocument);
setPrinterTrayHelper(xSupplier,
OOTextDoc.getXTextDocumentPageStyleName(xTextDocument),
printerTray);
}
// If loaded Document is Spread Sheet
else if (applicationName.equals("com.sun.star.sheet.SpreadsheetDocument")) {
// Get SpreadSheet Document
XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)
UnoRuntime.queryInterface(XSpreadsheetDocument.class,
this.xComponent);
if(xSpreadsheetDocument == null) {
if (debug > 0)
System.out.println("failed.");
return;
}
// Get the StyleFamiliesSupplier interface of the document
XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
xSpreadsheetDocument);
// Get Spreadsheets
XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
String[] sheets = xSpreadsheets.getElementNames();
for (int i = 0; i < sheets.length; i++) {
setPrinterTrayHelper(xSupplier,
OOCalcDoc.getXSpreadsheetPageStyleName(
xSpreadsheets, sheets[i]),
printerTray);
}
}
else if (applicationName.equals("com.sun.star.text.WebDocument")) {
}
else if (applicationName.equals("com.sun.star.text.GlobalDocument")) {
}
else if (applicationName.equals("com.sun.star.presentation.PresentationDocument")) {
}
else if (applicationName.equals("com.sun.star.drawing.DrawingDocument")) {
}
else if (applicationName.equals("com.sun.star.chart.ChartDocument")) {
}
else if (applicationName.equals("com.sun.star.formula.FormulaProperties")) {
}
else if (applicationName.equals("com.sun.star.sdb.OfficeDatabaseDocument")) {
}
//TODO Weitere Dokumenttypen hinzufügen.
}
catch (Exception e) {
if (debug > 0)
System.out.println("failed");
if (debug > 1)
System.out.println(e);
return;
}
if (debug > 0)
System.out.println("done.");
}
/**
* Prints out the document.
*/
public void print(String myPages) {
if (debug > 0)
System.out.print("Starting to print... ");
// Querying for the interface XPrintable on the loaded document
XPrintable xPrintable = (XPrintable)
UnoRuntime.queryInterface(XPrintable.class, this.xComponent);
XPrintJobBroadcaster selection = (XPrintJobBroadcaster) UnoRuntime
.queryInterface(XPrintJobBroadcaster.class, xPrintable);
PrintJobListener myXPrintJobListener = new PrintJobListener();
selection.addPrintJobListener(myXPrintJobListener);
// Setting the property "Pages" so that only the desired pages
// will be printed.
PropertyValue[] printOpts = new PropertyValue[1];
printOpts[0] = new PropertyValue();
printOpts[0].Name = "Pages";
printOpts[0].Value = myPages;
try {
xPrintable.print(printOpts);
} catch (IllegalArgumentException e) {
if (debug > 0)
System.out.println("failed.");
if (debug > 1)
System.err.println(e);
return;
}
while (myXPrintJobListener.getStatus() == null ||
myXPrintJobListener.getStatus() == PrintableState.JOB_STARTED)
{
// TODO: verhindern, dass nicht existierende Seiten gedruckt werden und
// Methode in Endlosschleife gefangen ist
try {
Thread.sleep(100);
} catch (InterruptedException e) {
if (debug > 1)
System.err.println(e);
return;
}
}
if (debug > 0)
System.out.println("done.");
}
/**
* Sets the printer name for the specified XComponent.
* @param printerName Name of the printer to be set.
*/
public void setPrinter(String printerName) {
if (debug > 0)
System.out.print("Setting printer " + printerName + " ... ");
// Querying for the interface XPrintable on the loaded document
XPrintable xPrintable = (XPrintable)
UnoRuntime.queryInterface(XPrintable.class, this.xComponent);
// Setting the property "Name" for the favoured printer
PropertyValue[] printerDesc = new PropertyValue[1];
printerDesc[0] = new PropertyValue();
printerDesc[0].Name = "Name";
printerDesc[0].Value = printerName;
try {
// Setting the name of the printer
xPrintable.setPrinter(printerDesc);
} catch (IllegalArgumentException e) {
if (debug > 0)
System.out.println("failed. Continue with default printer.");
if (debug > 1)
System.err.println(e);
}
if (debug > 0)
System.out.println("done.");
}
/**
* Setting a printer tray to a document.
*
* @param xSupplier Supplier that is supported by every kind of document.
* @param pageStyleName String with document page style name.
* @param printerTray String with the name of the printer tray
*/
private static void setPrinterTrayHelper(XStyleFamiliesSupplier xSupplier,
String pageStyleName, String printerTray) {
try {
// Use the StyleFamiliesSupplier interface to get the XNameAccess
// interface of the actual style families
XNameAccess xFamilies = (XNameAccess)
UnoRuntime.queryInterface(XNameAccess.class,
xSupplier.getStyleFamilies());
// Access the 'PageStyles' Family
XNameContainer xFamily = (XNameContainer)
UnoRuntime.queryInterface(XNameContainer.class,
xFamilies.getByName("PageStyles"));
XStyle xStyle = (XStyle)
UnoRuntime.queryInterface(XStyle.class,
xFamily.getByName(pageStyleName));
// Get the property set of the cell's TextCursor
XPropertySet xStyleProps = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xStyle);
try {
// my PageStyleSetting ...
xStyleProps.setPropertyValue("PrinterPaperTray", printerTray);
}
catch (Exception e) {
if (debug > 0)
System.out.println("failed. Continue printing on default" +
" tray.) ");
if (debug > 1)
System.out.println(e);
}
} catch (Exception e) {
if (debug > 0)
System.out.println("(Setting printer tray failed. Continue " +
"printing on default tray.) ");
if (debug > 1)
System.out.println(e);
}
}
/**
* Turn on or off debug mode to print exceptions to commandline.
*
* @param debug true if debug mode should be turned on.
*/
public void setDebug(int mydebug) {
debug = mydebug;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]