• Post Reply Bookmark Topic Watch Topic
  • New Topic

Neef Help with Printing  RSS feed

 
Sandeep DSouza
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to do some printing in my Java program....
this is what I am doing.... I pass an ArrayList to the class performing the print ... and then create a graphics object using the data in the ArrayList..... And print it.....

public int print(Graphics g, PageFormat pageFormat, int page) throws
PrinterException
{
int i;
Graphics2D g2d = (Graphics2D) g;
if (page == 0)
{
g2d.translate (pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setFont(new java.awt.Font("Microsoft Sans Serif", 0,11));
int x =0;
int y= 45;
// Printing the data from the Array list.
// The way I am formatting to print.. I can print only 16 records on one page.
So it needs to go to the next page after 16 records.
for(i=0;i<printArray.size();i++)
{
// trying to position the data into tow columns .. with all odd rows on one side and even on another.
if(checknum_odd_even(i))
{
x =0;
// trying to position the data.
Object[] prnObject = (Object[]) printArray.get(i);
g2d.drawString(prnObject[0].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[1].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[2].toString(), x, y);
y = y + 45;
}
else
{
// trying to position the data.
x=300;
y=y-75;
Object[] prnObject = (Object[]) printArray.get(i);
g2d.drawString(prnObject[0].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[1].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[2].toString(), x, y);
y = y + 45;
}
}
return (PAGE_EXISTS);
}
else return (NO_SUCH_PAGE);
}
}

This works fine.. as long as it is limited to only one page .... but i don't know how to print multiple pages ,,,
for example .. if there are 30 items in the array list that I am sending to the class ... it should print the first 16 on page one and the next 14 on page two .... .. I went through the javax.print .. but did not understand much .. will really appreciate if someone could help me .. give me an idea as per what needs to be done ..
Thanks,
Sandeep
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The print method has the following signature:

You need to consider the page variable. The printing engine will keep calling this method with ever-increasing values for page until you return NO_SUCH_PAGE. You have to figure out how many items will fit on a page (PageFormat gives you the bounds) and draw the items on the appropropriate page. Be forewarned, the print method may be called several times for the same page number.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!