• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print multiple pages  RSS feed

 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please help me in learning how to print multiple pages? The code I have works fine except if there are more than one page, the other pages come out blank. Here is my code:
class PrintListener implements ActionListener,Printable
{
public void actionPerformed(ActionEvent ae)
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new PrintListener());
if (pj.printDialog())
{
try
{
pj.print();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
double pageOffset = pageIndex * pf.getImageableHeight();
View view = theTextArea.getUI().getRootView(theTextArea);
if (pageOffset > view.getPreferredSpan(View.Y_AXIS))
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D) g;
g2d.translate(0d,-pageOffset);
Shape shape = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(), pf.getImageableHeight() + pageOffset);
view.paint(g2d,shape);
return Printable.PAGE_EXISTS;
}
}
Thanks for your help!!!
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
there's an interface "Pageable".
for more information see: http://java.sun.com/docs/books/tutorial/2d/printing/collection.html
have fun,
chantal
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know about Pageable, but I thought that was normally used for pages that have different formats. I have pages that are all the same format. Shouldn't Printable work? Correct me if I am wrong. Thanks!
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For anyone that is interested, the printing method that I used above does print multiple pages if you use jdk1.3. For some reason, it does not work with 1.2. Problem solved...... Next problem.....
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Barry,
PrinterJob is a very recent addition to JDK. maybe that is the reason why your program does not work with 1.2.
Than you would have to use PrintJob.
You're right about Pageable. Sorry.
chantal
 
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!