Solved

Printing a JTextArea with text spreading over multiple pages.

Posted on 2002-04-05
7
314 Views
Last Modified: 2010-03-31
Hi,
I am trying to print the text in JTextArea. The printing is done through a class 'PrintUtilities' which can print all 'Component's. But, when tried to print with text that spreads over multiple pages (say 2 pages), I am getting 2 copies of first 41 lines. The text starting from 42 line onwards (2nd page) are not getting printed. Instead I am getting two copies of first 41 lines. i.e in the
following code jTextArea1 contains 80 lines of text. When it got printed I am getting 2 copies of first 41 lines.
Thanking in advance,
Regards,
Unnic.
---- <code>-----

// the PrintUtilities which will print a Component...

public class PrintUtilities implements Printable {
  private Component componentToBePrinted;
  public static void printComponent(Component c) {
    new PrintUtilities(c).print();
  }
  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }
   public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    try {
        printJob.print();
      } catch(PrinterException pe) {
      }
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
// calculating the no. of pages required...
     int width =     this.component.getHeight();
     double imgHeight = pf.getImageableHeight();
     int numpages=  0;
     numpages = (int)(width/imgHeight);
     if(numpages*imgHeight < width)
       numpages++;
    if (pageIndex > numpages) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(),    
      pageFormat.getImageableY());
      componentToBePrinted.paint(g2d);
      return(PAGE_EXISTS);
    }
  }
// Invoking the print
    PrintUtilities pu = new PrintUtilities(this.jTextArea1);
    pu.print();
---- <code>-----

0
Comment
Question by:unnic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 6922480
You do not appear to be taking notice of the requested pageIndex passed to print().
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7933214
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.
 
In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care
 
Venci75
EE Cleanup Volunteer
0
 
LVL 92

Expert Comment

by:objects
ID: 7936084
I believe I answered the question.
The page required to be printed is passed to the print method, but the above code ignores it. Thus it prints the 1st page on every page.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Venci75
ID: 7940874
ok - it seems that unnic does not participate in his own questions. objects - I think you are right - but let me give few days to unnic. If he doesn't respond the next 3 days - I will recomend your comment as an answer.
ok?
0
 
LVL 92

Expert Comment

by:objects
ID: 7946070
sounds fine.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7973702
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8020296
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question