Phil Race wrote:
Bingo, that's exactly it.This is possible, JDK already does this internally. We are working with the same information you have. So you want a convenience method like:
4500750: RFE: convenience API to calculate PageFormat from PrintRequestAttributeSet
That's wrong. You are missing the API that was designed to do this for you.
Its in a class called javax.print.attribute.standard.MediaSize. The MediaSize class is basically nothing more than a big enumeration of paper sizes and mappings to/from MediaSizeName
YOu can get the size you need from there.
Look at MediaSize MediaSize.getMediaSizeForName(MediaSizeName media);
Once you have your MediaSize object you just pull out the w & h and create your PageFormat.
I got lucky and found a sun document with this method, but oh man, was that hard to come by! :-)
Here the code I came up with to do this (maybe I should post this where the RFE is ...);
public PageFormat getPageFormat(PrintRequestAttributeSet attributeSet)
{
PageFormat retval = getPrinterJob().defaultPage();Attribute[] atts = attributeSet.toArray();
for (int i=0; i < atts.length; i++)
{
Attribute att = atts[i]; if (att instanceof OrientationRequested)
{
OrientationRequested orientation =
(OrientationRequested)atts[i]; if (orientation.equals(OrientationRequested.LANDSCAPE))
{
retval.setOrientation(PageFormat.LANDSCAPE);
}
else if (orientation.equals(OrientationRequested.PORTRAIT))
{
retval.setOrientation(PageFormat.PORTRAIT);
}
else if (orientation.equals(OrientationRequested.REVERSE_LANDSCAPE))
{
retval.setOrientation(PageFormat.REVERSE_LANDSCAPE);
}
else
{
retval.setOrientation(PageFormat.LANDSCAPE);
}
}
else if (att instanceof MediaPrintableArea)
{
MediaPrintableArea printableArea =
(MediaPrintableArea)atts[i];Paper paper = retval.getPaper();
float[] area =
printableArea.getPrintableArea(MediaPrintableArea.INCH); paper.setImageableArea(area[0] * 72.0, area[1] * 72.0,
area[2] * 72.0, area[3] * 72.0); retval.setPaper(paper);
}
else if (att instanceof MediaSize || att instanceof MediaSizeName)
{
MediaSize mediaSize = null; if (att instanceof MediaSizeName)
{
mediaSize =
MediaSize.getMediaSizeForName((MediaSizeName)att);
}
else
{
mediaSize = (MediaSize)atts[i];
}Paper paper = retval.getPaper();
paper.setSize(mediaSize.getX(MediaSize.INCH) * 72.0,
mediaSize.getY(MediaSize.INCH) * 72.0); retval.setPaper(paper);
}
}
return retval;
}Note that I have do an ugly array loop for this, instead of just extracting the AttributeSets with the get method. For example, if the user selects a media size like (in windows) "US Index Card 4x6", the statement;
attributeSet.get(MediaSizeName.class);
returns null. If you print all the values in the AttributeSet, you will note that the class in there is called sun.print.Win32MediaSize.
For some reason, the get() call doesn't get the subclass, it expects an exact match of the class. Is this how the API is supposed to work?
Another problem, but it seems minor to me, is that for some reason the calculated values are off by a few units from what is returned by say the PageFormat from pageDialog().
For example;
For paper size "letter" from page dialog : width=>469 from page dialog : height>649 from calc pformat: width=>468 from calc pformat: height>648
For paper size "legal" from page dialog : width=>469 from page dialog : height>866 from calc pformat: width=>468 from calc pformat: height>864
For other paper sizes the sizes are a bit more off, but for what I want to do right now, these values are ok for now. Anyways, I don't know why I'm getting different results. Either the PageFormat from pageDialog is wrong, or the AttributeSet values are off, or my method to convert it is off.
I don't think it is mismatched, at least it matches as well as we could make it. Integration of java.awt.print and javax.print was a design goal so the former could leverage the latter as much as possible.
I think I understand. Maybe if the relationship between PageFormat and PrintRequestAttributeSet would be documnted a bit better. Perhaps the convinence method would help. Having to code the solution above just seems kind of ugly. Aside from the little bugs I pointed out, I feel kind of nervous about something like the page orientation. There are 4 attribute values, but only 3 possible values in PageFormat (!). Maybe the print() method in printable could have a new PageFormat which has a (getAttributeSet()) object with more "details" for the application to print information.
Thanks
Augusto
=========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".
