Skip to main content

How to obtain a PageFormat from a PrintRequestAttributeSet?

1 reply [Last post]
Joined: 2006-05-05

This is my first try to print something using Java and i ran into the following problem:

In the Print Dialog a user can set a page margin and orientation, which the Print Dialog stores in a PrintRequestAttributeSet.
What i had expected is a PageFormat constructor that takes a PrintRequestAttributeSet to initialize itself - no such thing.

Is there another way to obtain a PageFormat instance with information from the PrintRequestAttributeSet? How do YOU handle this?

Thanks in advance, Hans

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-09

In most cases you don't need to do this.
If you then call print(PrintRequestAttributeSet) then
the printing implementation can do this calculation

However we added a new method in JDK 6 to do this
exactly what you are requesting.
It on java.awt.print.PrinterJob so it can return
a PageFormat consistent with the printer rather than
one conjured out of thin air.

The spec for the new method is below

public PageFormat getPageFormat(PrintRequestAttributeSet attributes)[/b]
Calculates a PageFormat with values consistent with those supported by the current PrintService for this job (ie the value returned by getPrintService()) and media, printable area and orientation contained in attributes.
Calling this method does not update the job. It is useful for clients that have a set of attributes obtained from printDialog(PrintRequestAttributeSet attributes) and need a PageFormat to print a Pageable object.

attributes - a set of printing attributes, for example obtained from calling printDialog. If attributes is null a default PageFormat is returned.
a PageFormat whose settings conform with those of the current service and the specified attributes.