OpenOffice.org Macros Explained

The primary printing functionality is common to all OOo document types. The interface com.sun.star.view.XPrintable defines three methods (see Table 14 ).

Table 14: Object methods defined by com.sun.star.view.XPrintable.

Object Method

Description

getPrinter()

Default printer as an array of properties (com.sun.star.view.PrinterDescriptor).

setPrinter(properties)

Assign a new printer to the object (com.sun.star.view.PrinterDescriptor).

print(properties)

Print the document (com.sun.star.view.PrintOptions).

The object method getPrinter() returns an array of properties that describe the printer (see Figure 12 ). The macro in Listing 27 demonstrates how to access and interpret each of the properties (see Table 15 for supported properties).

Table 15: Properties defined by the service com.sun.star.view.PrinterDescriptor.

Property

Description

Name

Name of the printer queue.

PaperOrientation

Paper orientation (com.sun.star.view.PaperOrientation).

PaperFormat

Predefined paper sizes (com.sun.star.view.PaperFormat).

PaperSize

Paper size in twips (com.sun.star.awt. Size ).

IsBusy

Is the printer busy?

CanSet PaperOrientation

Can the paper orientation be set?

CanSet PaperFormat

Are other paper formats supported?

CanSetPaperSize

Can the paper size be set?

Figure 12: Properties of the default printer.

Listing 27: DisplayPrinterProperties is found in the Generic module in this chapter's source code files as SC12.sxw.

Sub DisplayPrinterProperties Dim Props 'Array of com.sun.star.beans.PropertyValue Dim i% 'Index variable of type Integer Dim s$ 'Display string Dim v ' Dim sName$ ' On Error Resume Next Props = ThisComponent.getPrinter() For i = 0 To UBound(Props) sName = props(i).Name v = props(i).Value s = s & sName & " = " If sName = "PaperOrientation" Then REM com.sun.star.view.PaperOrientation.LANDSCAPE also supported s = s & IIf(v=com.sun.star.view.PaperOrientation.PORTRAIT,_ "Portrait", "Landscape") & " = " & CStr(v) ElseIf sName = "PaperFormat" Then Select Case v Case com.sun.star.view.PaperFormat.A3 s = s & "A3" Case com.sun.star.view.PaperFormat.A4 s = s & "A4" Case com.sun.star.view.PaperFormat.A5 s = s & "A5" Case com.sun.star.view.PaperFormat.B4 s = s & "B4" Case com.sun.star.view.PaperFormat.B5 s = s & "B5" Case com.sun.star.view.PaperFormat.LETTER s = s & "LETTER" Case com.sun.star.view.PaperFormat.LEGAL s = s & "LEGAL" Case com.sun.star.view.PaperFormat.TABLOID s = s & "TABLOID" Case com.sun.star.view.PaperFormat.USER s = s & "USER" Case Else s = s & "Unknown value" End Select s = s & " = " & CStr(v) ElseIf sName = "PaperSize" Then REM type is com.sun.star.awt.Size REM The size is in TWIPS and there are 1440 twips per inch s=s & CDbl(v.Width)/1440.0 & "x" & CDbl(v.Height)/1440.0 & " (inches)" Else s = s & CStr(v) End If s = s & CHR$(10) Next MsgBox s, 0, "Printer Properties" End Sub

 

Tip  

Although you can set the printer as of OOo 1.1.1, no method is provided in OOo Basic to obtain a list of printers. If you really need a list of printers, try calling an external DLL or writing code to search configuration files.

Call the print() method with no properties to print a single copy of the document to the current printer. All of the document types support the properties in Table 16 . The Pages property supports the standard format used in the Print dialog. The format string "1, 3, 4-7, 9-" prints pages 1, 3, 4 through 7, and 9 through the last page.

Table 16: Properties defined by the com.sun.star.view.PrintOptions service.

Property

Description

CopyCount

Number of copies to print.

FileName

Send the output to a file rather than to the printer.

Collate

Collate the printed pages (set to True or False).

Pages

Specifies pages and page ranges to print.

Listing 28: Print pages 30 and 31 of the current document.

Dim Props(1) As New com.sun.star.beans.PropertyValue Props(0).Name = "Pages" : Props(0).Value = "30-31" ThisComponent.print(Props())

 

When a document is printed, control returns to the caller before the printing is complete. If you close the document before printing is done, OpenOffice.org is likely to crash because the OOo internals are still using the document. It's possible to set up an event listener that watches for the print job to finish, but there's an easier way that isn't currently documented. The print() method accepts an array of properties that direct the printing. The "wait" argument with a Boolean value of True instructs the print() method to not return until after printing is complete.

Warning  

Closing a document while OpenOffice.org is printing the document can cause OpenOffice.org to crash. Use the "wait" property to avoid this problem.

On Unix-type computers, printers are configured to work with OpenOffice.org using the "spadmin" utility. After a printer has been configured for OOo, it is available for use by name. You can still use printers that have not been configured for OOo, but you must enclose the printer name between the characters "<" and ">". The task of selecting a printer would be much simpler if OOo had the ability of providing the names of the visible printers. To print with a printer other than the default, use code similar to the following:

Public oProps(0) as New com.sun.star.beans.PropertyValue Public oOpts(1) as New com.sun.star.beans.PropertyValue Dim oDoc 'Document to print. Dim oPrinter 'Array of properties that define the printer. Dim sUrl$ 'URL of the document to load and print. Dim sPrinter$ 'Name of the printer. REM Set the name of the printer as known by the system. sPrinter = "HP2200D" REM Load the document in hidden mode so it is not visible REM on the screen. oProps(0).Name = "Hidden" oProps(0).Value = True REM Now load the document. sUrl = "file:///c:/test_doc.sxw" oDoc = oDesk.LoadComponentFromUrl(sUrl, "_blank", 63, oProps()) REM Obtain the current printer object from the document. REM This is really an array of property values. REM Change the name of the object to reference the printer that REM you want to use. Notice that the printer name is the system name. oPrinter = oDoc.getPrinter() For i = LBound(oPrinter) to UBound(oPrinter) If oPrinter(i).Name = "Name" Then oPrinter(i).Value = sPrinter End If Next i REM Set the printer back into the document. The only thing REM that has changed is the printer name. oDoc.setPrinter(oPrinter) REM Now, set up the print options for actual printing. REM Notice that the printer name is surrounded by the characters < and >. REM Also notice that the print() method is set to not return until REM after printing is completed. oOpts(0).Name = "Name" oOpts(0).Value = "<" & sPrinter & ">" oOpts(1).Name = "Wait" oOpts(1).Value = True oDoc.Print(oOpts())

Tip  

Although it is not documented, it has been experimentally determined that you must set the destination printer in the document before trying to print to a printer other than the default.

Printing Writer Documents

Different document types support extra options for printing. Text documents support the interface com.sun.star.text.XPagePrintable (see Table 17 ). The XPagePrintable interface implements an alternate method of printing the document that provides more control of the output. The primary advantage is that you can print multiple pages from the document on a single output page.

Table 17: Methods defined by the com.sun.star.text.XPagePrintable interface.

Object Methods

Description

getPagePrintSettings()

Returns an array of properties (see Table 18).

setPagePrintSettings(properties)

Change the settings (see Table 18).

printPages(properties)

Print using properties in Table 16.

Table 18: Properties used by the com.sun.star.text.XPagePrintable interface.

Property

Description

PageRows

Number of rows of pages on each printed page.

PageColumns

Number of columns of pages on each printed page.

LeftMargin

Left margin.

RightMargin

Right margin.

TopMargin

Top margin.

BottomMargin

Bottom margin.

HoriMargin

Margin between rows of pages.

VertMargin

Margin between columns of pages.

IsLandscape

True or False; print in landscape format.

The object method printPages() accepts the same properties as the print() method (see Table 16). The methods to get and set the page print properties are outlined in Table 18 . The macro in Listing 29 obtains and prints the current page print properties, shown in Figure 13 .

Figure 13: Page print properties of a Writer document.

Listing 29: DisplayPagePrintProperties is found in the Generic module in this chapter's source code files as SC12.sxw.

Sub DisplayPagePrintProperties Dim Props 'Array of com.sun.star.beans.PropertyValue Dim i% 'Index variable of type Integer Dim s$ 'Display string If HasUnoInterfaces(ThisComponent, "com.sun.star.text.XPagePrintable") Then Props = ThisComponent.getPagePrintSettings() For i = 0 To UBound(Props) s = s & props(i).Name & " = " & CStr(props(i).Value) & CHR$(10) Next MsgBox s, 0, "Page Print Properties" Else Print "Sorry, this document does not support the XPagePrintable interface" End If End Sub

 

The macro in Listing 30 prints a document with two pages on each printed page. Unfortunately, the PageColumns property and the printPages() method are problematic in OpenOffice.org. The Windows version of OOo 1.1.1 generates an error while attempting to set the PageColumns property, causing the macro in Listing 30 to fail; Linux, however, allows the property to be set. The printPages() method causes the document to close on Windows computers and causes OOo to crash on Linux computers. Finally, although both the PrintColumns property and the IsLandscape property can be made to work, the landscape property is currently ignored when used with PageColumns set to 2.

Listing 30: PrintTwoPerPage is found in the Generic module in this chapter's source code files as SC12.sxw.

Sub PrintTwoPerPage Dim Props(0 To 1) As New com.sun.star.beans.PropertyValue Props(0).Name = "PageColumns" : Props(0).Value = 2 Props(1).Name = "IsLandscape" : Props(1).Value = True If HasUnoInterfaces(ThisComponent, "com.sun.star.text.XPagePrintable") Then ThisComponent.setPagePrintSettings(Props()) ' ThisComponent.printPages(Array()) 'Use default properties Else Print "Sorry, this document does not support the XPagePrintable interface" End If End Sub

 

Warning  

The printPages() method crashes OOo 1.1.1 on my Linux computer. This is a known issue. For more information, see: http://qa.openoffice.org/issues/show_bug.cgi?id=23411 .

Although the macro in Listing 30 stopped working with OOo 1.1.1, a small modification allows the macro to run. The new macro still causes OOo to crash under Linux and to close the document under Windows, but it does manage to print the document. As with Listing 30, the document is printed in portrait mode rather than landscape mode-this is an OOo bug.

Sub PrintTwoPerPage_2 Dim oSettings Dim oSet Dim i% oSettings = ThisComponent.getPagePrintSettings() oSet = oSettings(1) For i=LBound(oSettings) To UBound(oSettings) oSet = oSettings(i) If oSet.Name = "PageColumns" Then oSet.Value = 2 oSettings(i) = oSet End If If oSet.Name = "IsLandscape" Then oSet.Value = True oSettings(i) = oSet End If Next ThisComponent.printPages(oSettings) End Sub

Printing Calc Documents

To perform special printing functions with a Writer document, a special object method is called. To perform special printing functions with Calc documents, you must modify the document properties and page-style properties and then use the standard print() method. For example, it is common for a Calc sheet to be too large to fit on a single sheet of paper. To scale the sheet to fit on a specified number of pages, set the ScaleToPages property to contain the number of pages that should contain the sheet. To simply scale the page based on a percentage, use the PageScale property (see Listing 31 ).

Listing 31: Print a spreadsheet at 25 percent; this is very small!

Sub PrintScaledSpreadsheet Dim s$ 'Style name Dim oStyle 'The current page style REM Use the currently active sheet to obtain the page style. REM In a Calc document, the current controller knows which sheet REM is active. s = ThisComponent.CurrentController.getActiveSheet().PageStyle oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(s) REM oStyle.PageScale = 100 Default value is 100 (as in 100%) REM oStyle.ScaleToPages = 0 Default value is 0, as in don't scale oStyle.PageScale = 25 'Scale document to 25% (very very very small) ThisComponent.Print(Array()) 'Print the document End Sub

 

The second aspect to printing Calc documents involves setting the area to print along with the column and row titles (see Table 19 ).

Table 19: Properties used by the com.sun.star.sheet.XPrintAreas interface.

Object Method

Description

getPrintAreas()

Return array of type com.sun.star.table.CellRangeAddress.

setPrintAreas(ranges)

Set print areas for the sheet with array of type CellRangeAddress. Print everything if nothing is set.

getPrintTitleColumns()

Return True if title columns are repeated to the right.

setPrintTitleColumns(boolean)

Set True if title columns are repeated on all print pages to the right.

getTitleColumns()

Array of type com.sun.star.table.CellRangeAddress.

setTitleColumns(ranges)

Set columns to use as titles. Rows are ignored; only columns matter.

getPrintTitleRows()

Return True if title rows are repeated on all print pages.

setPrintTitleRows(boolean)

Set to True if row titles are repeated on all print pages to the bottom.

getTitleRows()

Return array of type com.sun.star.table.CellRangeAddress.

setTitleRows(ranges)

Set rows to use as titles. Columns are ignored; only rows matter.

The methods in Table 19 are based on spreadsheets, not on Calc documents. The macro in Listing 32 sets two print ranges and then prints the document. Each print range is printed on a new page.

Listing 32: Set and print multiple ranges in a Calc document.

Sub PrintSpreadsheetAreas Dim oRanges(1) As New com.sun.star.table.CellRangeAddress oRanges(0).Sheet = 0 oRanges(0).StartColumn = 0 : oRanges(0).StartRow = 0 'Al oRanges(0).EndColumn = 3 : oRanges(0).EndRow = 4 'D5 oRanges(1).Sheet = 0 oRanges(1).StartColumn = 0 : oRanges(1).StartRow = 8 'A9 oRanges(1).EndColumn = 3 : oRanges(1).EndRow = 10 'Dll ThisComponent.CurrentController.getActiveSheet().setPrintAreas(oRanges()) ThisComponent.Print(Array()) End Sub

 

Категории