下面列出了java.awt.print.PageFormat#getImageableHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
if (pi == 0)
token = new StringTokenizer(ta.getText(), "\r\n");
if (!token.hasMoreTokens())
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.black);
g2d.setFont(newFont);
double xbeg = pf.getImageableX();
double ywidth = pf.getImageableHeight() + pf.getImageableY();
double y = pf.getImageableY() + incrY;
while (token.hasMoreTokens() && (y < ywidth)) {
String toke = token.nextToken();
g2d.drawString(toke, (int) xbeg, (int) y);
y += incrY;
}
return Printable.PAGE_EXISTS;
}
/**
* Prints the chart on a single page.
*
* @param g
* the graphics context.
* @param pf
* the page format to use.
* @param pageIndex
* the index of the page. If not <code>0</code>, nothing gets print.
*
* @return The result of printing.
*/
@Override
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex != 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
double x = pf.getImageableX();
double y = pf.getImageableY();
double w = pf.getImageableWidth();
double h = pf.getImageableHeight();
this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor, null);
return PAGE_EXISTS;
}
/**
* Print the plot to a printer, represented by the specified graphics
* object.
*
* @param graphics The context into which the page is drawn.
* @param format The size and orientation of the page being drawn.
* @param index The zero based index of the page to be drawn.
* @return PAGE_EXISTS if the page is rendered successfully, or
* NO_SUCH_PAGE if pageIndex specifies a non-existent page.
* @exception PrinterException If the print job is terminated.
*/
public synchronized int print(Graphics graphics, PageFormat format,
int index) throws PrinterException {
if (graphics == null) return Printable.NO_SUCH_PAGE;
// We only print on one page.
if (index >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D graphics2D = (Graphics2D) graphics;
// Scale the printout to fit the pages.
// Contributed by Laurent ETUR, Schlumberger Riboud Product Center
double scalex = format.getImageableWidth() / (double) getWidth();
double scaley = format.getImageableHeight() / (double) getHeight();
double scale = Math.min(scalex, scaley);
graphics2D.translate((int)format.getImageableX(),
(int)format.getImageableY());
graphics2D.scale(scale, scale);
_drawPlot(graphics, true);
return Printable.PAGE_EXISTS;
}
public int print(Graphics g, PageFormat format, int page)
throws PrinterException
{
if (page >= 1)
{
return Printable.NO_SUCH_PAGE;
}
double width = getSize().width;
double height = getSize().height;
double pageWidth = format.getImageableWidth();
double pageHeight = format.getImageableHeight();
double scale = 1;
if (width >= pageWidth)
scale = pageWidth / width;
double xSpace = (pageWidth - width * scale) / 2;
double ySpace = (pageHeight - height * scale) / 2;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(format.getImageableX() + xSpace,
format.getImageableY() + ySpace);
g2d.scale(scale, scale);
print(g2d);
return Printable.PAGE_EXISTS;
}
/**
* Set the print size to fit a page in the verticle direction.
* The horizontal is scaled equally but no garuntees are made on the page fit.
*/
private void scaleToFitY() {
PageFormat format = getPageFormat();
Rectangle componentBounds = scene.getBounds();
if (componentBounds.height == 0) {
return;
}
double scaleY = format.getImageableHeight() / componentBounds.height;
double scaleX = scaleY;
if (scaleY < 1) {
setSize((float) (componentBounds.width * scaleX), (float) format.getImageableHeight());
setScaledSize(scaleX, scaleY);
}
}
/**
* This describes a <code>PageFormat</code> as a String. This is provided as
* a debugging tool, because <code>PageFormat.toString()</code> doesn't
* support this itself.
*/
public static String toString(PageFormat f) {
if (f == null)
return "null";
String orientation;
if (f.getOrientation() == PageFormat.LANDSCAPE) {
orientation = "LANDSCAPE";
} else if (f.getOrientation() == PageFormat.PORTRAIT) {
orientation = "PORTRAIT";
} else if (f.getOrientation() == PageFormat.REVERSE_LANDSCAPE) {
orientation = "REVERSE_LANDSCAPE";
} else {
orientation = "UNKNOWN";
}
return ("PageFormat[ " + f.getWidth() + "x" + f.getHeight()
+ " imageable=(" + f.getImageableX() + ", " + f.getImageableY()
+ ", " + f.getImageableWidth() + ", " + f.getImageableHeight()
+ ") orientation=" + orientation + "]");
}
public int print( Graphics graphics, PageFormat format, int index ) {
Graphics2D g2d = (Graphics2D)graphics;
double scalex = g2d.getTransform().getScaleX();
double scaley = g2d.getTransform().getScaleY();
double centerx = ( format.getImageableX() +
( format.getImageableWidth() / 2 ) ) * scalex;
double centery = ( format.getImageableY() +
( format.getImageableHeight() / 2 ) ) * scaley;
// The following 2 lines cause an error when printing in landscape.
g2d.scale( 1 / scalex, 1 / scaley );
g2d.translate( centerx, centery );
Path2D.Double path = new Path2D.Double();
path.moveTo( -scalex * 72, -scaley * 72 );
path.lineTo( -scalex * 72, scaley * 72 );
path.lineTo( scalex * 72, scaley * 72 );
path.lineTo( scalex * 72, -scaley * 72 );
path.closePath();
g2d.draw( path );
return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
}
/**
* Method: print
* <p>
*
* @param g
* a value of type Graphics
* @param pageFormat
* a value of type PageFormat
* @param page
* a value of type int
* @return a value of type int
*/
public int print(Graphics g, PageFormat pageFormat, int page) {
//--- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate(pageFormat.getImageableX(), pageFormat
.getImageableY());
//--- Set the drawing color to black
g2d.setPaint(Color.black);
//--- Draw a border arround the page using a 12 point border
g2d.setStroke(new BasicStroke(12));
Rectangle2D.Double border = new Rectangle2D.Double(0, 0, pageFormat
.getImageableWidth(), pageFormat.getImageableHeight());
g2d.draw(border);
//--- Print page 1
if (page == 1) {
//--- Print the text one inch from the top and laft margins
g2d.drawString("This the content page of page: " + page,
POINTS_PER_INCH, POINTS_PER_INCH);
return (PAGE_EXISTS);
}
//--- Print page 2
else if (page == 2) {
//--- Print the text one inch from the top and laft margins
g2d.drawString("This the content of the second page: " + page,
POINTS_PER_INCH, POINTS_PER_INCH);
return (PAGE_EXISTS);
}
//--- Validate the page
return (NO_SUCH_PAGE);
}
/**
* Print the frame associated with this viewer.
*
* @param gc0 the graphics context.
* @param format page format
* @param pagenumber page index
*/
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
if (pagenumber == 0) {
Graphics2D gc = ((Graphics2D) gc0);
gc.setFont(getFont());
Dimension dim = getContentPane().getSize();
int image_w = dim.width;
int image_h = dim.height;
double paper_x = format.getImageableX() + 1;
double paper_y = format.getImageableY() + 1;
double paper_w = format.getImageableWidth() - 2;
double paper_h = format.getImageableHeight() - 2;
double scale_x = paper_w / image_w;
double scale_y = paper_h / image_h;
double scale = Math.min(scale_x, scale_y);
double shift_x = paper_x + (paper_w - scale * image_w) / 2.0;
double shift_y = paper_y + (paper_h - scale * image_h) / 2.0;
gc.translate(shift_x, shift_y);
gc.scale(scale, scale);
gc.setStroke(new BasicStroke(1.0f));
gc.setColor(Color.BLACK);
getContentPane().paint(gc);
return Printable.PAGE_EXISTS;
} else
return Printable.NO_SUCH_PAGE;
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
double x0 = pageFormat.getImageableX();
double y0 = pageFormat.getImageableY();
double w0 = pageFormat.getImageableWidth();
double h0 = pageFormat.getImageableHeight();
double w1 = getWidth();
double h1 = getHeight();
double scale;
if (w0 / w1 < h0 / h1) {
scale = w0 / w1;
} else {
scale = h0 /h1;
}
g2d.translate(x0, y0);
g2d.scale(scale, scale);
// Turn off double buffering
paint(g2d);
// Turn double buffering back on
return(PAGE_EXISTS);
}
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
Double width = pageFormat.getImageableWidth();
Double height = pageFormat.getImageableHeight();
g2d.drawImage(image, 0, 0, width.intValue(), height.intValue(), null, null);
return Printable.PAGE_EXISTS;
}
/**
* Print the graph associated with this viewer.
*
* @param gc0 the graphics context.
* @param format page format
* @param pagenumber page index
*/
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
if (pagenumber == 0) {
Graphics2D gc = ((Graphics2D) gc0);
Dimension dim = frame.getContentPane().getSize();
int image_w = dim.width;
int image_h = dim.height;
double paper_x = format.getImageableX() + 1;
double paper_y = format.getImageableY() + 1;
double paper_w = format.getImageableWidth() - 2;
double paper_h = format.getImageableHeight() - 2;
double scale_x = paper_w / image_w;
double scale_y = paper_h / image_h;
double scale = Math.min(scale_x, scale_y);
double shift_x = paper_x + (paper_w - scale * image_w) / 2.0;
double shift_y = paper_y + (paper_h - scale * image_h) / 2.0;
gc.translate(shift_x, shift_y);
gc.scale(scale, scale);
gc.setStroke(new BasicStroke(1.0f));
gc.setColor(Color.BLACK);
frame.getContentPane().paint(gc);
return Printable.PAGE_EXISTS;
} else
return Printable.NO_SUCH_PAGE;
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
/**
* Method: print
* <p>
*
* @param g
* a value of type Graphics
* @param pageFormat
* a value of type PageFormat
* @param page
* a value of type int
* @return a value of type int
*/
public int print(Graphics g, PageFormat pageFormat, int page) {
//--- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate(pageFormat.getImageableX(), pageFormat
.getImageableY());
//--- Set the drawing color to black
g2d.setPaint(Color.black);
//--- Draw a border arround the page using a 12 point border
g2d.setStroke(new BasicStroke(12));
Rectangle2D.Double border = new Rectangle2D.Double(0, 0, pageFormat
.getImageableWidth(), pageFormat.getImageableHeight());
g2d.draw(border);
//--- Print page 1
if (page == 1) {
//--- Print the text one inch from the top and laft margins
g2d.drawString("This the content page of page: " + page,
POINTS_PER_INCH, POINTS_PER_INCH);
return (PAGE_EXISTS);
}
//--- Print page 2
else if (page == 2) {
//--- Print the text one inch from the top and laft margins
g2d.drawString("This the content of the second page: " + page,
POINTS_PER_INCH, POINTS_PER_INCH);
return (PAGE_EXISTS);
}
//--- Validate the page
return (NO_SUCH_PAGE);
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
/**
* Print the graph associated with this viewer.
*
* @param gc0 the graphics context.
* @param format page format
* @param pagenumber page index
*/
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
JPanel panel = getPanel();
if (panel != null && pagenumber == 0) {
if (panel instanceof GraphView) {
return ((GraphView) panel).print(gc0, format, pagenumber);
} else {
Graphics2D gc = ((Graphics2D) gc0);
Dimension dim = panel.getSize();
int image_w = dim.width;
int image_h = dim.height;
double paper_x = format.getImageableX() + 1;
double paper_y = format.getImageableY() + 1;
double paper_w = format.getImageableWidth() - 2;
double paper_h = format.getImageableHeight() - 2;
double scale_x = paper_w / image_w;
double scale_y = paper_h / image_h;
double scale = Math.min(scale_x, scale_y);
double shift_x = paper_x + (paper_w - scale * image_w) / 2.0;
double shift_y = paper_y + (paper_h - scale * image_h) / 2.0;
gc.translate(shift_x, shift_y);
gc.scale(scale, scale);
panel.print(gc0);
return Printable.PAGE_EXISTS;
}
}
return Printable.NO_SUCH_PAGE;
}
public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException {
if (pi != 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
g2.setFont(new Font("Serif", Font.PLAIN, 36));
g2.setPaint(Color.black);
g2.drawString("Java Source and Support", 100, 100);
Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf
.getImageableY(), pf.getImageableWidth(), pf
.getImageableHeight());
g2.draw(outline);
return PAGE_EXISTS;
}
public static void updateRuleForPage( final CSSPageRule rule,
final PageFormat format ) {
if ( format == null ) {
rule.removeProperty( BoxStyleKeys.MARGIN_TOP );
rule.removeProperty( BoxStyleKeys.MARGIN_LEFT );
rule.removeProperty( BoxStyleKeys.MARGIN_BOTTOM );
rule.removeProperty( BoxStyleKeys.MARGIN_RIGHT );
rule.removeProperty( PageStyleKeys.SIZE );
// rule.removeProperty(PageStyleKeys.HORIZONTAL_PAGE_SPAN);
// rule.removeProperty(PageStyleKeys.VERTICAL_PAGE_SPAN);
return;
}
final double width = format.getWidth();
final double height = format.getHeight();
rule.setPropertyValueAsString( PageStyleKeys.SIZE,
width + "pt " + height + "pt" );
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_TOP, format.getImageableY() + "pt" );
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_LEFT, format.getImageableX() + "pt" );
final double marginRight = width - format.getImageableX() - format.getImageableWidth();
final double marginBottom = height - format.getImageableY() - format.getImageableHeight();
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_BOTTOM, marginBottom + "pt" );
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_RIGHT, marginRight + "pt" );
// rule.setPropertyValueAsString(PageStyleKeys.HORIZONTAL_PAGE_SPAN, "1");
// rule.setPropertyValueAsString(PageStyleKeys.VERTICAL_PAGE_SPAN, "1");
}
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
int ix = (int)pf.getImageableX();
int iy = (int)pf.getImageableY();
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih);
if ((ix < 0) || (iy < 0)) {
throw new RuntimeException("Imageable x or y is a negative value.");
}
Paper paper = pf.getPaper();
int wid = (int)paper.getWidth();
int hgt = (int)paper.getHeight();
System.out.println("wid="+wid+" hgt="+hgt);
/*
* If imageable width/height is -ve, then print was done with 1" margin
* e.g. ix=72 iy=72 iw=451 ih=697 and paper wid=595
* but with fix, we get print with hardware margin e.g.
* ix=12, iy=12, iw=571, ih=817
*/
if ((wid - iw > 72) || (hgt - ih > 72)) {
throw new RuntimeException("Imageable width or height is negative value");
}
if ((ix+iw > wid) || (iy+ih > hgt)) {
throw new RuntimeException("Printable width or height "
+ "exceeds paper width or height.");
}
// runtime checking to see if the margins/printable area
// correspond to the entire size of the paper, for now, make it pass
// as for linux, the hwmargin is not taken into account - bug6574279
if (ix == 0 && iy == 0 && (ix+iw == wid) && (iy+ih == hgt)) {
return PAGE_EXISTS;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(ix, iy);
g2d.setColor(Color.black);
g2d.drawRect(1, 1, iw-2, ih-2);
return PAGE_EXISTS;
}