下面列出了java.awt.print.PageFormat#getPaper ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Handles the page format.
*
* @param atts
* the attributes.
* @throws SAXException
* if a parser error occurs or the validation failed.
* @noinspection SuspiciousNameCombination
*/
private void handlePageFormat( final Attributes atts ) throws SAXException {
final MasterReport report =
(MasterReport) getRootHandler().getHelperObject( ReportParserUtil.HELPER_OBJ_REPORT_NAME );
// grab the default page definition ...
PageFormat format = report.getPageDefinition().getPageFormat( 0 );
float defTopMargin = (float) format.getImageableY();
float defBottomMargin = (float) ( format.getHeight() - format.getImageableHeight() - format.getImageableY() );
float defLeftMargin = (float) format.getImageableX();
float defRightMargin = (float) ( format.getWidth() - format.getImageableWidth() - format.getImageableX() );
format = createPageFormat( format, atts );
defTopMargin = ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.TOPMARGIN_ATT ), defTopMargin );
defBottomMargin =
ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.BOTTOMMARGIN_ATT ), defBottomMargin );
defLeftMargin = ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.LEFTMARGIN_ATT ), defLeftMargin );
defRightMargin = ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.RIGHTMARGIN_ATT ), defRightMargin );
final Paper p = format.getPaper();
switch ( format.getOrientation() ) {
case PageFormat.PORTRAIT:
PageFormatFactory.getInstance().setBorders( p, defTopMargin, defLeftMargin, defBottomMargin, defRightMargin );
break;
case PageFormat.LANDSCAPE:
// right, top, left, bottom
PageFormatFactory.getInstance().setBorders( p, defRightMargin, defTopMargin, defLeftMargin, defBottomMargin );
break;
case PageFormat.REVERSE_LANDSCAPE:
PageFormatFactory.getInstance().setBorders( p, defLeftMargin, defBottomMargin, defRightMargin, defTopMargin );
break;
default:
// will not happen..
throw new IllegalArgumentException( "Unexpected paper orientation." );
}
format.setPaper( p );
pageFormat = format;
}
protected PageFormat getPageFormatFromAttributes() {
if (attributes == null || attributes.isEmpty()) {
return null;
}
PageFormat newPf = attributeToPageFormat(
getPrintService(), attributes);
PageFormat oldPf = null;
Pageable pageable = getPageable();
if ((pageable != null) &&
(pageable instanceof OpenBook) &&
((oldPf = pageable.getPageFormat(0)) != null)) {
// If orientation, media, imageable area attributes are not in
// "attributes" set, then use respective values of the existing
// page format "oldPf".
if (attributes.get(OrientationRequested.class) == null) {
newPf.setOrientation(oldPf.getOrientation());
}
Paper newPaper = newPf.getPaper();
Paper oldPaper = oldPf.getPaper();
boolean oldPaperValWasSet = false;
if (attributes.get(MediaSizeName.class) == null) {
newPaper.setSize(oldPaper.getWidth(), oldPaper.getHeight());
oldPaperValWasSet = true;
}
if (attributes.get(MediaPrintableArea.class) == null) {
newPaper.setImageableArea(
oldPaper.getImageableX(), oldPaper.getImageableY(),
oldPaper.getImageableWidth(),
oldPaper.getImageableHeight());
oldPaperValWasSet = true;
}
if (oldPaperValWasSet) {
newPf.setPaper(newPaper);
}
}
return newPf;
}
protected PageFormat getPageFormatFromAttributes() {
Pageable pageable = null;
if (attributes == null || attributes.isEmpty() ||
!((pageable = getPageable()) instanceof OpenBook)) {
return null;
}
PageFormat newPf = attributeToPageFormat(
getPrintService(), attributes);
PageFormat oldPf = null;
if ((oldPf = pageable.getPageFormat(0)) != null) {
// If orientation, media, imageable area attributes are not in
// "attributes" set, then use respective values of the existing
// page format "oldPf".
if (attributes.get(OrientationRequested.class) == null) {
newPf.setOrientation(oldPf.getOrientation());
}
Paper newPaper = newPf.getPaper();
Paper oldPaper = oldPf.getPaper();
boolean oldPaperValWasSet = false;
if (attributes.get(MediaSizeName.class) == null) {
newPaper.setSize(oldPaper.getWidth(), oldPaper.getHeight());
oldPaperValWasSet = true;
}
if (attributes.get(MediaPrintableArea.class) == null) {
newPaper.setImageableArea(
oldPaper.getImageableX(), oldPaper.getImageableY(),
oldPaper.getImageableWidth(),
oldPaper.getImageableHeight());
oldPaperValWasSet = true;
}
if (oldPaperValWasSet) {
newPf.setPaper(newPaper);
}
}
return newPf;
}
/**
* Resolves a page format, so that the result can be serialized.
*
* @param format the page format that should be prepared for serialisation.
* @return the prepared page format data.
*/
private Object[] resolvePageFormat( final PageFormat format ) {
final Integer orientation = new Integer( format.getOrientation() );
final Paper p = format.getPaper();
final float[] fdim = new float[] { (float) p.getWidth(), (float) p.getHeight() };
final float[] rect = new float[] { (float) p.getImageableX(),
(float) p.getImageableY(),
(float) p.getImageableWidth(),
(float) p.getImageableHeight() };
return new Object[] { orientation, fdim, rect };
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
/**
*
*/
public boolean printPages(
int firstPageIndex,
int lastPageIndex,
PrintService service
) throws JRException
{
boolean isOK = true;
if (
firstPageIndex < 0 ||
firstPageIndex > lastPageIndex ||
lastPageIndex >= jasperPrint.getPages().size()
)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_INVALID_PAGE_RANGE,
new Object[]{firstPageIndex, lastPageIndex, jasperPrint.getPages().size()}
);
}
pageOffset = firstPageIndex;
PrinterJob printJob = PrinterJob.getPrinterJob();
// fix for bug ID 6255588 from Sun bug database
initPrinterJobFields(printJob);
PageFormat pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
printJob.setJobName("JasperReports - " + jasperPrint.getName());
switch (jasperPrint.getOrientationValue())
{
case LANDSCAPE :
{
pageFormat.setOrientation(PageFormat.LANDSCAPE);
paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
paper.setImageableArea(
0,
0,
jasperPrint.getPageHeight(),
jasperPrint.getPageWidth()
);
break;
}
case
PORTRAIT :
default :
{
pageFormat.setOrientation(PageFormat.PORTRAIT);
paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
paper.setImageableArea(
0,
0,
jasperPrint.getPageWidth(),
jasperPrint.getPageHeight()
);
}
}
pageFormat.setPaper(paper);
Book book = new Book();
book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
printJob.setPageable(book);
try
{
if (service == null) {
if (printJob.printDialog()) {
printJob.print();
} else {
isOK = false;
}
} else {
printJob.setPrintService(service);
printJob.print();
}
}
catch (Exception ex)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_ERROR_PRINTING_REPORT,
null,
ex);
}
return isOK;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
public static PageFormat getPageFormat(String name, PageFormat pageFormat) {
Properties properties = getProperties(name);
if (properties == null)
return pageFormat;
final String orientation = properties.getProperty(ORIENTATION);
if (LANDSCAPE.equals(orientation))
pageFormat.setOrientation(PageFormat.LANDSCAPE);
else if (PORTRAIT.equals(orientation))
pageFormat.setOrientation(PageFormat.PORTRAIT);
else if (REVERSE_LANDSCAPE.equals(orientation))
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
String s = properties.getProperty(PAPER_IMAGEABLE_HEIGHT);
if (s == null)
return pageFormat;
double iHeight = Double.parseDouble(s);
s = properties.getProperty(PAPER_IMAGEABLE_WIDTH);
if (s == null)
return pageFormat;
double iWidth = Double.parseDouble(s);
s = properties.getProperty(PAPER_HEIGHT);
if (s == null)
return pageFormat;
double height = Double.parseDouble(s);
s = properties.getProperty(PAPER_WIDTH);
if (s == null)
return pageFormat;
double width = Double.parseDouble(s);
s = properties.getProperty(PAPER_IMAGEABLE_X);
if (s == null)
return pageFormat;
final double x = Double.parseDouble(s);
s = properties.getProperty(PAPER_IMAGEABLE_Y);
if (s == null)
return pageFormat;
final double y = Double.parseDouble(s);
final Paper paper = pageFormat.getPaper();
paper.setImageableArea(x, y, iWidth, iHeight);
paper.setSize(width, height);
pageFormat.setPaper(paper);
return pageFormat;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
/**
*
*/
public boolean printPages(
int firstPageIndex,
int lastPageIndex,
boolean withPrintDialog
) throws JRException
{
boolean isOK = true;
if (
firstPageIndex < 0 ||
firstPageIndex > lastPageIndex ||
lastPageIndex >= jasperPrint.getPages().size()
)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_INVALID_PAGE_RANGE,
new Object[]{firstPageIndex, lastPageIndex, jasperPrint.getPages().size()}
);
}
pageOffset = firstPageIndex;
PrinterJob printJob = PrinterJob.getPrinterJob();
// fix for bug ID 6255588 from Sun bug database
initPrinterJobFields(printJob);
PageFormat pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
printJob.setJobName("JasperReports - " + jasperPrint.getName());
switch (jasperPrint.getOrientationValue())
{
case LANDSCAPE :
{
pageFormat.setOrientation(PageFormat.LANDSCAPE);
paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
paper.setImageableArea(
0,
0,
jasperPrint.getPageHeight(),
jasperPrint.getPageWidth()
);
break;
}
case
PORTRAIT :
default :
{
pageFormat.setOrientation(PageFormat.PORTRAIT);
paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
paper.setImageableArea(
0,
0,
jasperPrint.getPageWidth(),
jasperPrint.getPageHeight()
);
}
}
pageFormat.setPaper(paper);
Book book = new Book();
book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
printJob.setPageable(book);
try
{
if (withPrintDialog)
{
if (printJob.printDialog())
{
printJob.print();
}
else
{
isOK = false;
}
}
else
{
printJob.print();
}
}
catch (Exception ex)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_ERROR_PRINTING_REPORT,
null,
ex);
}
return isOK;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
/**
* Compiles a collection of page format properties.
*
* @param fmt
* the pageformat
* @param retval
* the attribute list
* @return The properties.
*/
private static AttributeList buildPageFormatProperties( final PageFormat fmt, final AttributeList retval ) {
if ( fmt == null ) {
throw new NullPointerException();
}
if ( retval == null ) {
throw new NullPointerException();
}
final Paper paper = fmt.getPaper();
final int w = (int) paper.getWidth();
final int h = (int) paper.getHeight();
final String pageDefinition = PageFormatFactory.getInstance().getPageFormatName( w, h );
if ( pageDefinition != null ) {
retval.setAttribute( BundleNamespaces.STYLE, "pageformat", pageDefinition );
} else {
retval.setAttribute( BundleNamespaces.STYLE, "width", String.valueOf( w ) );
retval.setAttribute( BundleNamespaces.STYLE, "height", String.valueOf( h ) );
}
final Insets borders = getBorders( paper );
if ( fmt.getOrientation() == PageFormat.REVERSE_LANDSCAPE ) {
retval.setAttribute( BundleNamespaces.STYLE, "orientation", "reverse-landscape" );
retval.setAttribute( BundleNamespaces.STYLE, "margin-top", String.valueOf( borders.right ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-left", String.valueOf( borders.top ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-bottom", String.valueOf( borders.left ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-right", String.valueOf( borders.bottom ) );
} else if ( fmt.getOrientation() == PageFormat.PORTRAIT ) {
retval.setAttribute( BundleNamespaces.STYLE, "orientation", "portrait" );
retval.setAttribute( BundleNamespaces.STYLE, "margin-top", String.valueOf( borders.top ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-left", String.valueOf( borders.left ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-bottom", String.valueOf( borders.bottom ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-right", String.valueOf( borders.right ) );
} else {
retval.setAttribute( BundleNamespaces.STYLE, "orientation", "landscape" );
retval.setAttribute( BundleNamespaces.STYLE, "margin-top", String.valueOf( borders.left ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-left", String.valueOf( borders.bottom ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-bottom", String.valueOf( borders.right ) );
retval.setAttribute( BundleNamespaces.STYLE, "margin-right", String.valueOf( borders.top ) );
}
return retval;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)){
return false;
}
copyAttributes(pServ);
DefaultSelectionType select =
this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if ( d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else { // (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName)attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media)attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
mediaSize.getY(MediaSize.INCH)*72.0);
}
if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0,
p.getWidth()-36.0,
p.getHeight()-36.0);
} else {
p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (orient!= null &&
orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
private void getLandscapeAttributes() {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat documentPageFormat = new PageFormat();
documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
documentPageFormat.setPaper(pappyLand);
documentPageFormat = printJob.pageDialog(documentPageFormat);
pappyLand = documentPageFormat.getPaper();
}
private void getPortraitAttributes() {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat documentPageFormat = new PageFormat();
documentPageFormat.setOrientation(PageFormat.PORTRAIT);
documentPageFormat.setPaper(pappyPort);
documentPageFormat = printJob.pageDialog(documentPageFormat);
pappyPort = documentPageFormat.getPaper();
}