• Post Reply Bookmark Topic Watch Topic
  • New Topic

about printing  RSS feed

 
marko stone
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
i created 3 documents in my API and now i need printed them
one of them have large strings of data so can i rotate printing ???
and second when printer working it shows for example "Pages 3 of 75"
but i have my doc more less then 75 ? what's wrong ? where it takes these 75 ???
thanks
my code is below
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable
{
private Component componentToBePrinted;
int iddoc;

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)
{
Graphics2D g2=(Graphics2D)g;
int fontHeight=g2.getFontMetrics().getHeight();
int fontDescent=g2.getFontMetrics().getDescent();
double pageHeight=pageFormat.getImageableHeight()-fontHeight;
double pageWidth=pageFormat.getImageableWidth();
double compWidth=(double)componentToBePrinted.getSize().width;
double compHeight=(double)componentToBePrinted.getSize().height;
double scale=1;
if (iddoc==2)
{
if (compWidth>=pageWidth)
{
scale=pageWidth/compWidth;
}
}
double compWidthOnPage=compWidth*scale;
double oneStringHeight=fontHeight*scale;
int numRowsOnAPage=(int)((pageHeight-fontHeight)/oneStringHeight);
double pageHeightForC=oneStringHeight*numRowsOnAPage;
int totalNumPages=(int)Math.ceil((double)compHeight/numRowsOnAPage);
if (pageIndex>=totalNumPages)
{
return (NO_SUCH_PAGE);
}
else
{
g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
g2.drawString("Page "+(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);
}
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!