hello
i have problem with printer
for example i have only 2 pages of text but printer says much more paper
i think i need use Pageable interface but
i'm try but not effect
here is my code without Pageble
anybody help me to set real number of pages ?
thanks at advance
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable
{
private Component componentToBePrinted;
int iddoc;
PageFormat dpageFormat=new PageFormat();
Graphics2D g2;
Graphics g;
int fontHeight,fontDescent;
double pageHeight,pageWidth,compWidth,compHeight;
double scale=1;
double compWidthOnPage,oneStringHeight;
int numRowsOnAPage;
double pageHeightForC;
int totalNumPages;
public static void printComponent(Component c,int iddoc)
{
new PrintUtilities(c,iddoc).print();
}
public PrintUtilities(Component componentToBePrinted,int iddoc)
{
this.componentToBePrinted=componentToBePrinted;
this.iddoc=iddoc;
}
public void print()
{
PrinterJob printJob=PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
{
try
{
printJob.print();
}
catch(PrinterException pe)
{
System.out.println("Error"+pe);
}
}
}
public int print(Graphics g,PageFormat pageFormat,int pageIndex)
{
g2=(Graphics2D)g;
fontHeight=g2.getFontMetrics().getHeight();
fontDescent=g2.getFontMetrics().getDescent();
if (iddoc==2)
{
pageFormat.setOrientation(PageFormat.LANDSCAPE);
}
pageHeight=pageFormat.getImageableHeight()-fontHeight;
pageWidth=pageFormat.getImageableWidth();
compWidth=(double)componentToBePrinted.getSize().width;
compHeight=(double)componentToBePrinted.getSize().height;
scale=1;
if (iddoc==2)
{
if (compWidth>=pageWidth)
{
scale=pageWidth/compWidth;
}
}
compWidthOnPage=compWidth*scale;
oneStringHeight=fontHeight*scale;
numRowsOnAPage=(int)((pageHeight-fontHeight)/oneStringHeight);
pageHeightForC=oneStringHeight*numRowsOnAPage;
totalNumPages=(int)Math.ceil((double)compHeight/numRowsOnAPage);
if (pageIndex>=totalNumPages)
{
return (NO_SUCH_PAGE);
}
else
{
g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
g2.drawString("TЄЁрэшЎр "+(1+pageIndex),(int)pageWidth/2-35,(int)(pageHeight+fontHeight-fontDescent));
g2.translate(0f,-pageIndex*pageHeightForC);
g2.setClip(0,(int)(pageHeightForC*pageIndex),(int)Math.ceil(compWidthOnPage),(int)Math.ceil(pageHeightForC));
g2.scale(scale,scale);
componentToBePrinted.paint(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*pageHeightForC);
return (PAGE_EXISTS);
}
}
}