• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

In JavaFX how do I approach dividing a text blob for pagination and then printing?  RSS feed

 
Ranch Hand
Posts: 49
2
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Struggle:
I'm stuck on how to divide a lengthy text blob of string data into pages that will fit within Printer setup parameters. The text blob comes from a user's database which may be 100, 1000, may be 3000 words or more, which could be 10 or more pages depending on the printer page setup, font style, size, etc.

What I have done:
I have done some work the last couple days researching and trying some suggestions. The examples out there pertain more to tables than text areas or flows. Those that do are scrolling and taking rectangular shapes of the data, which doesn't seem  as the right approach. The Oracle illustration of the Pagination Control example (see link below) handles a multi-page text blob by simply dividing it into a String[] array for each page in the Pagination which is the least evasive approach, but nothing explaining how that should be done. There are multiple factors to consider as I stated (font, margin, size, etc). I did find another Oracle document titled Printing a Multiple Page Document (see link below) illustrating a calculation method but it is not using the JavaFX Printing suite (for lack of a better term). I'm curious if anyone believes this may be the way to go though the calculations are not specific to JavaFX but using awt and swing.

27 Pagination Control: https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/pagination.htm
Printing a Multiple Page Document: https://docs.oracle.com/javase/tutorial/2d/printing/set.html

What is working:
I think I have a good setup on the Pagination simply based upon the Oracle Pagination. I'm comfortable with the Printing as well. Both of these work for a single page except that the text blob gets cut-off because I'm uncertain how to divide it having to factor in the parameters I stated. The division of the text blob occurs prior to Pagination and is used in the actual pagination method createPages.

The core Pagination procedure for creating pages - which includes the textBlob array:


Oracle suggested example of text blob which I may pass to a method and return as a String[]:


 
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you've really done your homework on this!  I admit I've never done pagination in JavaFX, so I'm confused.  I thought you could just send the whole String to the printer and it would figure out the pagination for itself.  But you seem to want the same pagination on the screen as on the printer, is that correct?

I will look at the links you provided to educate myself on this subject.  Hopefully we can come up with something together.
 
Knute Snortum
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'm seeing the complexity of this problem now.  I've been researching how to get certain metrics but I have yet to put it all together:
Does any of this help?
 
Knute Snortum
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott can't post (may be a bug in Code Ranch software), so he PMed me a reply:

Scott wrote:Yes, right now the pagination will show the entire text blob in a single pagination page. However, if I can figure out how to separate the text out so the pagination can page it out, I think I can then loop the pagination pages out to the printer. I think that is how it should work anyway.  

 
Knute Snortum
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[This whole post is from Scott:]

Hey Knute!

I thought you deserved more information and I didn't want to leave you hanging since for some reason I cannot reply to my own post but below is the test code I've been working with to solve this issue.

As you stated, you would think I could simply pass the textFlow to the job.printPage() (line 86) and I can, but the PrinterJob (job) only prints what will fit on one page and does not automatically recognize more than one page.

I setup a TextFlow and VBox global variables so I could test both ways (Lines 81-84). In each case, job.printPage() will only print a single page. So Pagination was suggested for dividing text into pages as TextFlows and from there I myself am confused on what should happen in the gap between the text blob and printing -- meaning managing the paging if greater than one page.

Hope this helps with some clarification.  Again, this is just test code which is a practice I do to isolate functionality testing before refactoring and implementing so I understand what has to be done. Btn1 and Btn3 (Setup, Exit) do nothing. I typically print do PDF to save on time and paper.
 
Scott Vallery
Ranch Hand
Posts: 49
2
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I solved one of the two problems:

1) Parsing the text to fit the printer's current printable page area
2) JavaFX print, recognizing multiple pages to print.

It took a lot of time but I found using the JavaFX Font, FontMetrics and Toolkit classes for font data, combined with PrinterJob and PageLayout classes, I could pull all the metrics data needed in calculating the number of lines of text in a text blob and how many fit on a page before creating the next page.

PrinterJob allows pulling the page layout and using PageLayout to acquire the printable height (getPrintableHeight()) and printable width (getPrintableWidth()) of the page.
Font, FontMetrics and Toolkit allows pulling the font line height (getLineHeight()) and the measurement of each line width (computeStringWidth(line))

The following text parsing class will return a String[] of pages in a public variable docPages which can be dumped into the Oracle JavaFX Pagination as described in their documentation (https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/pagination.htm).

What I have not solved (issue 2):
When I use the pagination example in the link I provided, the printPages() will only print a single page of the pagination based upon the page selected -- so it will print the page selected. But I think the user should be able to print all at the same time, especially if they want them all in a PDF.  I cannot seem to find how that works?



This is the JavaFX GUI call with a modified createPages() based upon TextArea example in the Pagination example.


 
Scott Vallery
Ranch Hand
Posts: 49
2
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was finally able to solve my second problem -

1) Parsing the text to fit the printer's current printable page area
2) JavaFX print, recognizing multiple pages to print

I'm an amateur coder at best here which people are so eager to point out, so my code and comments are for informational purposes. At the time of this post, the information out there was very vague. I spent two weeks reading posts and most had very little information or quite frankly any resolution, or the resolution was so complicated it took 3-4 times the code you see here. The sources I used for everything I did here in this and the other posts in this thread are:

JavaFX Pagination: https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/pagination.htm
Calculating and parsing your text to pages (I used this idea but created my own Parsing class (above): https://docs.oracle.com/javase/tutorial/2d/printing/set.html
Printing Multiple pages: https://stackoverflow.com/questions/36559924/how-do-i-handle-pagination-when-printing-text

I believe there are two keys to making this successful. One falls with the placement of the endJob() method of the PrintJob class and second is to pass each of the String[] parsed pages of your text blob directly to a TextFlow and avoid the complication of Pagination except if you want to provide individual page printing or for presentation if that is what you prefer -- which I did. You don't need pagination to make this work, all you need is the text blob parsed to a String[] of page strings as I have in the above post. I'm using my own "Print Preview" GUI, so there is no call to the print dialog below (see pic attached).

There are two methods for demonstration: In my Print Preview pagination GUI, I have two buttons: Print and Print Page. Print will print all the pages created from the String[] and Print Page will print only the selected page as pagination was designed. When printing all pages in the Print method, you can use the length of your String[] to help setup the PageRange  (if you only have one print range). If you prefer to call the print dialog, you will need to setup your page range before that call.





This is the simplified standard pagination print method that will print single pages through the use of Pagination.
PrintPreview.JPG
[Thumbnail for PrintPreview.JPG]
My customized print preview
 
Knute Snortum
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is great!  Thanks for posting the solution, and I wish I could have been more of a help to you.
 
Scott Vallery
Ranch Hand
Posts: 49
2
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:This is great!  Thanks for posting the solution, and I wish I could have been more of a help to you.



Np! Thanks Sheriff! LOL!
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!