Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printable  RSS feed

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Am trying to print a tet file, am using the below overrided print () and its printing fine. It simply reading line by line and then drawing the string. I have a requirement that if a string "END" is on the file then after that I need to print the remaining lines in next paper. I tried

if(line.equals("END"))
return PAGE_EXISTS;

But nothing is worked, Please help me out. How can I start printing in a new page after my condition.
Code copied from http://www.java2s.com/Code/Java/2D-Graphics-GUI/PageableText.htm

 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of line when you expect it to be "END"?

Also, keep in mind that technically speaking, "END" could be the very last line. Should you print another (blank) page then too?
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob thanks for the reply.

For example

Start - 1 st line
Meetgaurav
END
middle
rob
END
JavaRanch - last line

I need

1st Page
Start - 1 st line
Meetgaurav
END
2nd Page
middle
rob
END
3rd Page
JavaRanch

Bur currently

when I try

if(line.equals("END"))
return PAGE_EXISTS;
the second and third page is printing blank
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This isn't correct if the number of lines per page changes. In the example you gave, with 10 lines per page, startLine will be 10 on the second page, and nothing will be printed.

Both the number of pages and the start line are dynamic; perhaps you should store them in instance variables. You then get something like this:
As you can see, numPages disappears completely. Also, notice the break statement inside the loop, which will cause either PAGE_EXISTS or NO_SUCH_PAGE to be returned after the END has been printed. Also, the final return also includes a check to see if any more lines should be printed, to prevent an empty page at the end.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Thanks for your Great work. But still some issues I tried to print a file and its contains 3 END so, it was suppose to print in 3 pages, But its printing the second page alone. 1 and 3rd page were missing. I will be trying to fine tune the code, If possible please help me out.



pagenum is getting incremented as 0, 0, 1, 1, 2, 2 (Default Nature) Its a bug in Java

This pagenum is not getting incremented each time when we return PAGE.EXITS
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the Full Code

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob thanks for your help.



The above code fixed my issue. Since the Pagenum getting incremented by 0,0,1,1,2,2,3,3. Is it bug from sun ?

Here is the full code
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob any suggestions. is it a bug ?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.experts-exchange.com/Programming/Languages/Java/Q_20161543.html

Scroll down to the bottom; this is also noted in the API of Printable. I didn't know this either.


Now this does have a real impact; you can't just keep increasing startLine; it will be increased for the same page. Instead, you will need a mapping from page numbers to the first line numbers on that page. This will make it a bit harder though, but I'm sure you can work it out.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob

I will try to work it out. Mean time please have a look at here

webpage

This is useful only for graphics ovject printing but am sure I will be printing only text files.

From the URL
As I mentioned earlier, the current implementation of PrinterJob calls the print() method of a Printable object at least twice. The first call uses a dummy Graphics object whose sole purpose is to determine what kind of graphics the page contains. If the page contains only text, as is the case in Example 5-2, the PrinterJob can print the page efficiently in text mode.

However, if the page contains any other type of graphics, the PrinterJob uses a large, high-resolution image to capture the graphics on the page and then transmits this image to the printer for printing in graphics mode. Because such a high-resolution image is memory intensive, the PrinterJob typically breaks the page up into several smaller bands and calls the print() method several times (using a different clipping region each time). In this way, the PrinterJob is able to spool a large image to the printer without using a large amount of memory (a classic time versus space trade-off).


 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob

I will try to work it out. Mean time please have a look at

http://docstore.mik.ua/orelly/java-ent/jfc/ch05_02.htm

This above URL says first call is to identify the object type whether text or graphics. But am sure I will be using only text in this case. Could you please confirm whether the first call is still mandatory or I can skip as the prvious code I submitted

From the URL
As I mentioned earlier, the current implementation of PrinterJob calls the print() method of a Printable object at least twice. The first call uses a dummy Graphics object whose sole purpose is to determine what kind of graphics the page contains. If the page contains only text, as is the case in Example 5-2, the PrinterJob can print the page efficiently in text mode.

However, if the page contains any other type of graphics, the PrinterJob uses a large, high-resolution image to capture the graphics on the page and then transmits this image to the printer for printing in graphics mode. Because such a high-resolution image is memory intensive, the PrinterJob typically breaks the page up into several smaller bands and calls the print() method several times (using a different clipping region each time). In this way, the PrinterJob is able to spool a large image to the printer without using a large amount of memory (a classic time versus space trade-off).
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I can neither confirm nor deny that claim - I simply don't know.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!