Printing Text Documents

From Apache OpenOffice Wiki
Jump to: navigation, search



Printer and Print Job Settings

Printing is a common office functionality. The chapter Office Development provides in-depth information about it. The writer document implements the com.sun.star.view.XPrintable interface for printing. It consists of three methods:

  sequence< com::sun::star::beans::PropertyValue > getPrinter ()
  void setPrinter ( [in] sequence< com::sun::star::beans::PropertyValue > aPrinter)
  void print ( [in] sequence< com::sun::star::beans::PropertyValue > xOptions)

The following code is used with a given document xDoc to print to the standard printer without any settings:

  // query the XPrintable interface from your document
  XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, xDoc);
  
  // create an empty printOptions array
  PropertyValue[] printOpts = new PropertyValue[0];
  
  // kick off printing
  xPrintable.print(printOpts);

There are two groups of properties involved in general printing. The first one is used with setPrinter() and getPrinter() that controls the printer, and the second one is passed to print() and controls the print job.

com.sun.star.view.PrinterDescriptor comprises the properties for the printer:

Properties of com.sun.star.view.PrinterDescriptor
Name string - Specifies the name of the printer queue to be used.
PaperOrientation com.sun.star.view.PaperOrientation. Specifies the orientation of the paper.
PaperFormat com.sun.star.view.PaperFormat. Specifies a predefined paper size or if the paper size is a user-defined size.
PaperSize com.sun.star.awt.Size. Specifies the size of the paper in 1/100 mm.
IsBusy boolean - Indicates if the printer is busy.
CanSetPaperOrientation boolean - Indicates if the printer allows changes to PaperOrientation.
CanSetPaperFormat boolean - Indicates if the printer allows changes to PaperFormat.
CanSetPaperSize boolean - Indicates if the printer allows changes to PaperSize.


com.sun.star.view.PrintOptions contains the following possibilities for a print job:

Properties of com.sun.star.view.PrintOptions
CopyCount short - Specifies the number of copies to print.
FileName string - Specifies the name of a file to print to, if set.
Collate boolean - Advises the printer to collate the pages of the copies. If true, a whole document is printed prior to the next copy, otherwise the page copies are completed together.
Pages string - Specifies the pages to print in the same format as in the print dialog of the GUI (e.g. "1, 3, 4-7, 9-")
Wait boolean - Advises that the print job should be performed synchronously, i.e. wait until printing is complete before returning from printing. Otherwise return is immediate and following actions (e.g. closing the corresponding model) may fail until printing is complete. Default is false.

The following method uses PrinterDescriptor and PrintOptions to print to a special printer, and preselect the pages to print.

  protected void printDocComponent(XComponent xDoc) throws java.lang.Exception {
      XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, xDoc);
      PropertyValue[] printerDesc = new PropertyValue[1];
      printerDesc[0] = new PropertyValue();
      printerDesc[0].Name = "Name";
      printerDesc[0].Value = "5D PDF Creator"; 
  
      xPrintable.setPrinter(printerDesc); 
  
      PropertyValue[] printOpts = new PropertyValue[1];
      printOpts[0] = new PropertyValue();
      printOpts[0].Name = "Pages";
      printOpts[0].Value = "3-5,7"; 
  
      xPrintable.print(printOpts);
  }

Printing Multiple Pages on one Page

The interface com.sun.star.text.XPagePrintable is used to print more than one document page to a single printed page.

  sequence< com::sun::star::beans::PropertyValue > getPagePrintSettings()
  void setPagePrintSettings( [in] sequence< com::sun::star::beans::PropertyValue > aSettings)
  void printPages( [in] sequence< com::sun::star::beans::PropertyValue > xOptions)

The first two methods getPagePrintSettings() and setPagePrintSettings() control the page printing. They use a sequence of com.sun.star.beans.PropertyValues whose possible values are defined in com.sun.star.text.PagePrintSettings:

Properties of com.sun.star.text.PagePrintSettings
PageRows short - Number of rows in which document pages should appear on the output page.
PageColumns short - Number of columns in which document pages should appear on the output page.
LeftMargin long - Left margin on the output page.
RightMargin long - Right margin on the output page.
TopMargin long - Top margin on the output page.
BottomMargin long - Bottom margin on the output page.
HoriMargin long - Margin between the columns on the output page.
VertMargin long - Margin between the rows on the output page.
IsLandscape boolean - Determines if the output page is in landscape format.

The method printPages() prints the document according to the previous settings. The argument for the printPages() method may contain the PrintOptions as described in the section above (containing the properties CopyCount, FileName, Collate and Pages).

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages