B
bilaribilari
Hi all,
I've spent the last 12 hours trying to get Java 1.4.2 to print page
numbers on a report I am printing. I am using a subclass of JEditorPane
which implements Printable with HTML text inside it. I am using the
following code inside the print method:
Graphics2D g2 = (Graphics2D)g;
int fontHeight=g2.getFontMetrics().getHeight();
int fontDesent=g2.getFontMetrics().getDescent();
g2.setColor(Color.black); //set default foreground color to
black
//for faster printing, turn off double buffering
RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = this.getSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pf.getImageableHeight() ; //height of
printer page
double pageWidth = pf.getImageableWidth(); //width of
printer page
double scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight /
pageHeight);
if(pageIndex >= totalNumPages)
{
return Printable.NO_SUCH_PAGE;
}
//System.out.println ( "Total Pages: "+totalNumPages);
//shift Graphic to line up with beginning of print-imageable
region
g2.translate(pf.getImageableX(), pf.getImageableY());
//shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex*pageHeight);
//scale the page so the width fits...
g2.scale(scale, scale);
this.paint(g2); //repaint the page for printing
//PRINT THE PAGE NUMBER AS A FOOTER
g2.setFont ( new Font ( "Dialog" , Font.PLAIN , 8 ) ) ;
g2.drawString( Integer.toString (pageIndex + 1) ,
(int)pageWidth/2 ,
6 );
//DOESN'T WORK
/*g2.drawString( Integer.toString (pageIndex + 1) ,
(int)pageWidth - 10 ,
((int)pf.getImageableY() +
(int)pageHeight ) );*/
return Printable.PAGE_EXISTS;
Now I am really fed up and frustrated because the page number just
doesn't come. It comes on the first page and not on any of the other
pages. Also, in the Print Dialog, the margins are set by default to 1
inch. However, my text starts printing 2 inches from the top, which is
disturbing since I haven't put any additional space. So how do I get a
decent print out ?
Please help!
Thanks in advance.
Best regards.
I've spent the last 12 hours trying to get Java 1.4.2 to print page
numbers on a report I am printing. I am using a subclass of JEditorPane
which implements Printable with HTML text inside it. I am using the
following code inside the print method:
Graphics2D g2 = (Graphics2D)g;
int fontHeight=g2.getFontMetrics().getHeight();
int fontDesent=g2.getFontMetrics().getDescent();
g2.setColor(Color.black); //set default foreground color to
black
//for faster printing, turn off double buffering
RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = this.getSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pf.getImageableHeight() ; //height of
printer page
double pageWidth = pf.getImageableWidth(); //width of
printer page
double scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight /
pageHeight);
if(pageIndex >= totalNumPages)
{
return Printable.NO_SUCH_PAGE;
}
//System.out.println ( "Total Pages: "+totalNumPages);
//shift Graphic to line up with beginning of print-imageable
region
g2.translate(pf.getImageableX(), pf.getImageableY());
//shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex*pageHeight);
//scale the page so the width fits...
g2.scale(scale, scale);
this.paint(g2); //repaint the page for printing
//PRINT THE PAGE NUMBER AS A FOOTER
g2.setFont ( new Font ( "Dialog" , Font.PLAIN , 8 ) ) ;
g2.drawString( Integer.toString (pageIndex + 1) ,
(int)pageWidth/2 ,
6 );
//DOESN'T WORK
/*g2.drawString( Integer.toString (pageIndex + 1) ,
(int)pageWidth - 10 ,
((int)pf.getImageableY() +
(int)pageHeight ) );*/
return Printable.PAGE_EXISTS;
Now I am really fed up and frustrated because the page number just
doesn't come. It comes on the first page and not on any of the other
pages. Also, in the Print Dialog, the margins are set by default to 1
inch. However, my text starts printing 2 inches from the top, which is
disturbing since I haven't put any additional space. So how do I get a
decent print out ?
Please help!
Thanks in advance.
Best regards.