Solved

Printing a JTextArea with text spreading over multiple pages.

Posted on 2002-04-05
7
312 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
Independent Software Vendors: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 107
Tomcat: Unable to run tomcat service. 2 33
throw exception 21 76
Java Eclipse Loop 3 45
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

737 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