Printing multiple JTables as one job -- Book object only prints 1st table - java

For a number of reasons, I'm trying to combine the output of multiple JTables into a single print job. After tearing my hair out trying to build PDFs, and combing the Java API, I settled on the Book class. My printing code currently looks like this.
try {
PrinterJob printer = PrinterJob.getPrinterJob();
//Set 1/2 " margins and orientation
PageFormat pf = printer.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper();
double margin = 36; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
pf.setPaper(paper);
Book printJob = new Book();
// Note for next line: getAllTables() returns an ArrayList of JTables
for (JTable t : getAllTables() )
printJob.append(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf,2);
printer.setPageable(printJob);
System.out.println(printJob.getNumberOfPages());
if (printer.printDialog())
printer.print();
} catch (PrinterException e) {
e.printStackTrace();
}
Primary Problem: Only the output from the "first" table is printing. I've made sure the for loop iterates correctly, and debugging statements have shown that every table is being added to the Book. Changing order of tables has no effect. Changing the print mode to PrintMode.NORMAL, does appear result in pieces of the other tables being printed. However, I run into a slew horizontal pagination problems, as the table width frequently exceeds page width (and it still doesn't explain why PrintMode.FIT_WIDTH isn't working)
A secondary question: How can I detect the correct number of pages in each printable? Most of my tables are two pages long, so for the moment, I'm just adding 2 pages each time I append. I read "somewhere" that using Book.UNKNOWN_NUMBER_OF_PAGES as the page number will fix this problem, but that only leads to an IndexOutOfBounds exception in the API's code. I've considered calling print myself until I get NO_PAGE_EXISTS, but I'd need a Graphics object with the proper page dimensions (and I have no idea how to get that).
Lastly: If the Book approach is hopeless, how else can I combine the output of multiple JTables (ie. multiple printables) into a single job? I looked into exporting the table as a PDF, but JTable's built-in pagination is so nice, I'd rather not have to do it myself. My last resort is to just give up and use iText's built in table function to construct a copy of the table.
Edit 3: Per my comment below, I got this working by generating a printable, determining the # of pages, and then generating a new one. I also modified Durendal's wrapper to spare us the hassle of iterating over each page. The code from the wrapper is
class PrintableWrapper implements Printable
{
private Printable delegate;
private int offset;
public PrintableWrapper(Printable delegate, int offset) {
this.offset = offset;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, pageIndex-offset);
}
}
I put Durendal's code for determining number of pages in its own function
public int getNumberOfPages(Printable delegate, PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS) {
++numPages;
} else {
break;
}
}
return numPages;
}
After I create the book object, my printing code looks like this
int totalPages = 0;
for (DragNDropTable t : getAllTables() )
{
int pages = getNumberOfPages(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf);
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
printJob.append(new PrintableWrapper(p,totalPages), pf, pages);
totalPages += pages;
}
printer.setPageable(printJob);
if (printer.printDialog())
printer.print();
And it works like a charm!
Edit 2: (you can skip this) I tried Durendal's answer. While I'm printing enough pages, multipage printables are printing the last page multiple times (once for every page in the printable). This is the same problem I discussed in my 1st edit (below), and I have no idea why this is happening, and my debugging statements are saying that it's printing all of the pages correctly, but the last page of a multipage printable is printed in place of each page. Code is attached. Insight is appreciated (and repayed with virtual cookies)
try {
PrinterJob printer = PrinterJob.getPrinterJob();
//Set 1/2 " margins and orientation
PageFormat pf = printer.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper();
double margin = 36; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
pf.setPaper(paper);
Book printJob = new Book();
// Note for next line: getAllTables() returns an ArrayList of JTables
for (JTable t : getAllTables() )
{
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
int pages = getNumberOfPages(p, pf);
for (int i=0; i < pages; i++)
printJob.append(new PageWrapper(p,i), pf);
}
printer.setPageable(printJob);
System.out.println(printJob.getNumberOfPages());
if (printer.printDialog())
printer.print();
} catch (PrinterException e) {
e.printStackTrace();
}
public int getNumberOfPages(PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS)
++numPages;
else
break;
}
return numPages;
}
I'm using the unmodified PageWrapper that Durendal gave below.
Edit 1: (You can skip this) Dovetailing off of Durendal's answer, I tried to make a wrapper that spares us the chore of iterating over the pages ourselves. Unfortunately, it doesn't seem to work correctly on multipage printables, printing the same page multiple times in the document. I post it, simply because someone may get it to work, and it's slightly more convenient to use.
class PrintableWrapper implements Printable
{
private Printable delegate;
private int offset;
public PrintableWrapper(Printable delegate, int offset) {
this.offset = offset;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, pageIndex-offset);
}
public int getNumberOfPages(PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS)
++numPages;
else
break;
}
return numPages;
}
}
My printing code now looks like this (after I set the page format)
Book printJob = new Book();
int totalPages = 0;
for (DragNDropTable t : getAllTables() )
{
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
PrintableWrapper pw = new PrintableWrapper(p, totalPages);
totalPages += pw.getNumberOfPages(pf);
printJob.append(pw, pf,pw.getNumberOfPages(pf));
}
printer.setPageable(printJob);
if (printer.printDialog())
{
printer.print();
}

I had the very same Problem recently. The Book class by itself is useless, because if you add Printables to it, when the Book is printed it will pass the pageIndex from the Book to the Printable at pageIndex.
That is in most cases not what you want.
Create a simple Printable Wrapper that can remember a pageIndex to be used for the Printable delegate and add those to the Book:
class PageWrapper implements Printable {
private Printable delegate;
private int localPageIndex;
public PageWrapper(Printable delegate, int pageIndex) {
this.localPageIndex = pageIndex;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, localPageIndex);
}
}
Now you need to iterate trough each page of each Printable/Pageable and add a wrapper instance (that knows the pageIndex into its delegate) to the Book. That solves the problem that Book passes the wrong pageIndex to the printables added to it (its easier for Pageables than Printables).
You can detect the number of Pages in a Printable by printing it ;) Yes, I'm serious:
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = printable.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS) {
++numPages;
} else {
break;
}
}
Its important you obtain your PageFormat instance from the actual PrinterJob you want to print to, because the number of pages depends on the page format (paper size).

Related

how to print the selected pane inside FXML

i want to print the fxml contain pane but don't know how to do it.
i try two code inside button action .
first
void doPrint(Node printPane) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPageSetupDialog(printPane.getScene().getWindow())) {
job.printPage(printPane);
job.endJob();
}
}
}
second
boolean doPrint(Node printPane) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job == null) {
return false;
}
if (!job.printPage(printPane)) {
return false;
}
return job.endJob();
}
This will print any node you pass into it.
private void printImage(Node node) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null) {
boolean success = job.printPage(node);
if (success) {
System.out.println("PRINTING FINISHED");
job.endJob();
}
}
}
Try this on action event
#FXML
private void PrintAction(ActionEvent event) {
Printer printer = Printer.getDefaultPrinter(); //get the default printer
javafx.print.PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT); //create a pagelayout. I used Paper.NA_LETTER for a standard 8.5 x 11 in page.
PrinterJob job = PrinterJob.createPrinterJob();//create a printer job
if(job.showPrintDialog(tab_doctor_list.getScene().getWindow()))// this is very useful it allows you to save the file as a pdf instead using all of your printer's paper. A dialog box pops up, allowing you to change the "name" option from your default printer to Adobe pdf.
{
double pagePrintableWidth = pageLayout.getPrintableWidth(); //this should be 8.5 inches for this page layout.
double pagePrintableHeight = pageLayout.getPrintableHeight();// this should be 11 inches for this page layout.
tab_doctor_list.prefHeightProperty().bind(Bindings.size(tab_doctor_list.getItems()).multiply(35));// If your cells' rows are variable size you add the .multiply and play with the input value until your output is close to what you want. If your cells' rows are the same height, I think you can use .multiply(1). This changes the height of your tableView to show all rows in the table.
tab_doctor_list.minHeightProperty().bind(tab_doctor_list.prefHeightProperty());//You can probably play with this to see if it's really needed. Comment it out to find out.
tab_doctor_list.maxHeightProperty().bind(tab_doctor_list.prefHeightProperty());//You can probably play with this to see if it' really needed. Comment it out to find out.
double scaleX = pagePrintableWidth / tab_doctor_list.getBoundsInParent().getWidth();//scaling down so that the printing width fits within the paper's width bound.
double scaleY = scaleX; //scaling the height using the same scale as the width. This allows the writing and the images to maintain their scale, or not look skewed.
double localScale = scaleX; //not really needed since everything is scaled down at the same ratio. scaleX is used thoughout the program to scale the print out.
double numberOfPages = Math.ceil((tab_doctor_list.getPrefHeight() * localScale) / pagePrintableHeight);//used to figure out the number of pages that will be printed.
}
}
Instead of tab_doctor_list use your fx:id of pane.

How to change printed JTable font size?

Basically, I use JTable's method print(JTable.PrintMode.FIT_WIDTH, null, null, true, null, true, null); to print this table. However, on some computers this printed table on A4 sheet is quite small, so I thought I need to set its fonts larger, but I don't know how to do that.
In order to make table font larger, I created a custom CustomPrintable class: class CustomPrintable implements Printable { and then in its method print:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
...
Graphics gCopy = graphics.create();
gCopy.setFont(gCopy.getFont().deriveFont(20f)); //make font larger, but no success
int retVal = delegate.print(gCopy, format, pageIndex);
...
}
This approach did not help and printed table is still to small. Can you help me with this? How can I change font of the printed table?

How do I test a pixel of a bufferedimage for a certain color in Java

I'm trying to take a screenshot and then look through it for a pixel that has a certain color. Firstly, I tried to just print the color of an image at a certain xy coordinate but I could not even do that. What am I doing wrong?
static int ScreenWidth;
static int ScreenHeight;
static Robot robot;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic
callibrateScreenSize();
findSquares();
//takeScreenShot();
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void callibrateScreenSize() {
try {
Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
ScreenWidth = captureSize.width;
ScreenHeight = captureSize.height;
System.out.println("Width is " + ScreenWidth);
System.out.println("Height is " + ScreenHeight);
} catch (Exception e) {
e.printStackTrace();
}
//return null;
}
public static BufferedImage takeScreenShot() {
Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage image = robot.createScreenCapture(captureSize);
return image;
}
public static void findSquares() {
System.out.println(takeScreenShot().getRGB(5,5));
}
Thanks!
You can use BufferedImage#getRGB or byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData() to get the pixel data. getRBG is more convenient, but is typically slower than getting the pixel array
getRGB packs the pixel data into an int, getData will return the RGB(A) in each entry of the array (R = n; G = n+1; B=n+2(, A=n+3)), so will need to process this yourself
You can use java.awt.Color, which allows you to access the RGB values of the color, pack it as a int value or convert an int to a Color
Color color = new Color(bufferedImage.getRGB(0, 0), true);
int redColor = Color.RED.getRGB();
The answer to this question provides an example of dealing with the byte[] pixel data
Basically, you will need to loop over the data, comparing the values in the image to the value you are after, either directly (comparing the red, green and blue values) or indirectly, comparing the packed int or Color values.
Personally, I'd grab the pixel data, convert each element to an int and compare it with a previously packed int from a Color object, this creates the less number of short lived objects and should be reasonably efficient
You can take a look at this answer which use getRGB to get the red, green, blue values from a given pixel
Here's something I wrote a while ago using the Robot class. It returns an array of the screen wherever the screen is white, it was not very computationally expensive for my application, but I found probing the values individually using robot was. At first I didn't even read your question, but looking back, I think this will help you A LOT. Good luck. And then I saw the original post date...
public boolean[][] raster() throws AWTException, IOException{
boolean[][] filled= new boolean[720][480];
BufferedImage image = new Robot().createScreenCapture(new Rectangle(0,0,720,480));
//accepts (xCoord,yCoord, width, height) of screen
for (int n =0; n<720; n++){
for (int m=0; m<480; m++){
if(new Color(image.getRGB(n, m)).getRed()<254){
//can check any rgb value, I just chose red in this case to check for white pixels
filled[n][m]=true;
}
}
}
return filled;
}

How to span wide tables across multiple pages horizontally?

I am looking for a method to split wide tables so that they span across multiple pages. The goal is to make tables with large number of columns readable. I found one discussion thread where this topic is covered; however, the example referenced in there is not available. Manning's "iText in Action" (2006) doesn't cover this topic.
Can this be done in version 1.4.8, if not, to which version of iText should I upgrade to?
Please take a look at the examples of chapter 4 of my book, more specifically at the Zhang example. In this example, I have a table with four columns: (1) year, (2) movie title in English, (3) movie title in Chinese, and (4) run length. If you look at the resulting PDF, you will see that this table is split vertically.
Achieving this requires more work then simply adding a table and allowing iText to decide how to split it in between rows. When you want to split in between columns, you need to organize the layout in your code. This is done using the writeSelectedRows()) method.
In my simple book example, I use these lines:
// draw the first two columns on one page
table.writeSelectedRows(0, 2, 0, -1, 236, 806, canvas);
document.newPage();
// draw the remaining two columns on the next page
table.writeSelectedRows(2, -1, 0, -1, 36, 806, canvas);
First I draw the columns from index 0 to index 2. The column with index 0 is the first column, the column with index 2 is the first column that isn't included, namely the third column. I draw the rows from index 0 (first row) until -1. Minus one means: draw all the remaining rows.
You also see minus one on the next page, where I draw the column with index 2 (the third column) until the column with index -1 (meaning: the rest of the columns).
The values (236, 806) and (36, 806) are coordinates: that's where you want the table to start. You can't define "end coordinates". If the table doesn't fit on the page, iText will just continue drawing the table, even if that means that some content exceeds the visible area of the page. This means that you'll have to be very careful when using this method: you'll need to calculate widths and heights of rows and columns before adding the table, otherwise you may end up with parts of the table that aren't visible.
This is the source code of a class that splits your table over multiple pages when your columns don't fit in a single page
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* Class that writes a <code>PdfPTable</code>, and spans it across multiple
* pages if the columns won't fit on one page
*/
public class PdfPTableWriter {
// Instance variables
private PdfPTable table;
private PdfWriter writer;
private Document document;
// List of how many columns per horizontal page
private List numberOfColumnsPerPage;
// List of how many rows per vertical page
private List numberOfRowsPerPage;
// Offsets if given
private float widthOffset = 20;
private float heightOffset = 70;
/**
* Class Constructor
*/
public PdfPTableWriter(Document document, PdfWriter writer, PdfPTable table) {
this.document = document;
this.writer = writer;
this.table = table;
calculateColumns();
calculateRows();
}
/**
* Writes the table to the document
*/
public void writeTable() throws DocumentException {
// Begin at row 1 (row after the header)
int rowBegin = 1;
int rowEnd = 0;
// Note the size of numberOfRowsPerPage is how many vertical
// pages there are.
Iterator rowsIter = numberOfRowsPerPage.iterator();
while (rowsIter.hasNext()) {
rowEnd = ((Integer) rowsIter.next()).intValue();
writeSelectedRows(rowBegin, rowEnd);
rowBegin = rowEnd;
}
}
/**
* Prints the Report's columns (splitting horizontally if necessary) and
* subsequent rows
*
* #param rowBegin
* #param rowEnd
* #throws DocumentException
*/
private void writeSelectedRows(int rowBegin, int rowEnd) throws DocumentException {
int colBegin = 0;
int colEnd = 0;
float pageHeight = document.getPageSize().getHeight() - heightOffset;
PdfContentByte contentByte = writer.getDirectContent();
Iterator columnsIter = numberOfColumnsPerPage.iterator();
while (columnsIter.hasNext()) {
colEnd = colBegin + ((Integer) columnsIter.next()).intValue();
// Writer table header
writeSelectedRows(colBegin, colEnd, 0, 1, widthOffset, pageHeight);
// Writes selected rows to the document
writeSelectedRows(colBegin, colEnd, rowBegin, rowEnd, widthOffset, pageHeight - table.getRowHeight(0) /*table.getHeaderHeight()*/);
// Add a new page
document.newPage();
colBegin = colEnd;
}
}
public int getTotalPages() {
return numberOfColumnsPerPage.size() * numberOfRowsPerPage.size();
}
public void setHeightOffset(float heightOffset) {
this.heightOffset = heightOffset;
}
public void setWidthOffset(float widthOffset) {
this.widthOffset = widthOffset;
}
private void writeSelectedRows(int colBegin, int colEnd, int rowBegin, int rowEnd, float x, float y) {
PdfContentByte cb = writer.getDirectContent();
table.writeSelectedRows(colBegin, colEnd, rowBegin, rowEnd, x, y, cb);
}
private void calculateColumns() {
numberOfColumnsPerPage = new ArrayList();
float pageWidth = document.getPageSize().getWidth() - widthOffset;
float[] widths = table.getAbsoluteWidths();
if (table.getTotalWidth() > pageWidth) {
// tmp variable for amount of total width thus far
float tmp = 0f;
// How many columns for this page
int columnCount = 0;
// Current page we're on
int currentPage = 0;
// Iterate through the column widths
for (int i = 0; i < widths.length; i++) {
// Add to the temporary total
tmp += widths[i];
// If this column will not fit on the page
if (tmp > pageWidth) {
// Add the current column count to this page
numberOfColumnsPerPage.add(new Integer(columnCount));
// Since this column won't fit, the tmp variable should
// start off the next iteration
// as this column's width
tmp = widths[i];
// Set column count to 1, since we have moved this column to
// the next page
columnCount = 1;
}
// If this is will fit on the page
else {
// Increase the column count
columnCount++;
}
}
// Save the remaining columns
numberOfColumnsPerPage.add(new Integer(columnCount));
}
// All the columns will fit on one horizontal page
// Note: -1 means all the columns
else {
numberOfColumnsPerPage.add(new Integer(-1));
}
}
private void calculateRows() {
numberOfRowsPerPage = new ArrayList();
float pageHeight = document.getPageSize().getHeight() - heightOffset - table.getRowHeight(0) /*table.getHeaderHeight()*/;
// If the table won't fit on the first page
if (table.getTotalHeight() > pageHeight /*table.getHeaderHeight()*/) {
// Temp variables
float tmp = 0f;
// Determine the start and end rows for each page
for (int i = 1; i < table.size(); i++) {
// Add this row's height to the tmp total
tmp += table.getRowHeight(i);
if (tmp > pageHeight - (heightOffset/2)) {
// This row won't fit so end at previous row
numberOfRowsPerPage.add(new Integer(i - 1));
// Since this row won't fit, the tmp variable should start
// off the next iteration
// as this row's height
tmp = table.getRowHeight(i);
}
}
// Last page always ends on totalRows
numberOfRowsPerPage.add(new Integer(table.size()));
}
// All the rows will fit on one vertical page
// Note: -1 means all the rows
else {
numberOfRowsPerPage.add(new Integer(-1));
}
}
}
Well, I'll try to give you some kind of response. First at all, as #mkl says, 1.4.8 is ancient version. Look at http://sourceforge.net/projects/itext/files/iText/ to get something better, the last version is 5.4.5. And as I know, there is no way to split wide table in two pages. If the document is "a bit" wider than the page - rotate the page, but if you have many columns that don't fit - you have to do it your way and this could be painful. There is no automatic function that can check if your columns have too much text and don't fit the page.
Hope this helps you.

Java. Estimation of total number of printing pages

I have question how to check how many pages will be printed when i use my own Printable class.
I need it, because i want to have "page_number/total_pages" in footer of each page.
In my case problem with estimation of rows is that rows are wrapped. Additionaly the are empty rows beetwen some of the lines of text and there are some other cases which can prevent regular spreading of text. Generally this is not uniform printing.
As you know, rendering process is done after calling all print dialog windows.
Is any way to deal with this issue or should i somehow launch printing simulation , to receieve real number of pages? Or maybe should i implement some other class?
Regards
You can use following to get no. of pages:
int linesPerPage; // No. of lines per Page to be drawn.
static int numPages; // No. of pages too be rendered.
public int getNumberOfPages() // Override method to get Number Of Pages.
{
return numPages;
}
in main..
linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
numPages = (DATA_to_Print.length - 1)/linesPerPage + 1;
in override print method..
if ((pagenum < 0) | (pagenum >= numPages))
{
return NO_SUCH_PAGE;
}
Each time when print method will be call, value of the numPages will increment.
I figured out how to do it. Maybe this is no elegant, but it works (i tested estimation with document with 1200 pages and estimation is accurate). I will show you rather concept supported with couple line of code because my Printable classes are complex.
This is class which calls all operations connected with printing:
package print_manager;
import icd_searcher.ResultContainer;
import java.awt.Point;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import others.MeasuredBox;
public class PrintManager
{
private PrinterJob printerJob;
private SimpleResultPrinter srp;
/**
* Print printable object.
*
* #param toPrint
*/
public void initPrint(ResultContainer resultToPrint)
{
printerJob = PrinterJob.getPrinterJob();
PageFormat selectedArea = printerJob.pageDialog(printerJob.defaultPage());
MeasuredBox margin = new MeasuredBox(new Point((int) selectedArea.getImageableX(), (int) selectedArea.getImageableY()), (int) selectedArea.getImageableWidth(), (int) selectedArea.getImageableHeight() - 72);
srp = new SimpleResultPrinter(resultToPrint, margin);
SimpleResultPrinterSimulator srpSimulation = new SimpleResultPrinterSimulator(resultToPrint, margin);
// total pages simulation
int totalPages = 0;
try
{
while (srpSimulation.print(selectedArea, totalPages) != Printable.NO_SUCH_PAGE)
{
totalPages++;
}
}
catch (PrinterException e)
{
e.printStackTrace();
}
printerJob.setPrintable(srp, widenedPage);
if (printerJob.printDialog())
{
try
{
printerJob.print();
}
catch (PrinterException exc)
{
System.out.println(exc);
}
}
}
}
Method print in SimpleResultPrinter
public int print(Graphics g, PageFormat page, int pageIndex) throws
PrinterException {...}
Method print in SimpleResultPrinterSimulator
public int print(PageFormat page, int pageIndex) throws PrinterException
{...}
You need to know about this:
MeasuredBox and ResultContainer are my custom classes that have no meaning in this conception - treat them as a "some classes".
Class SimpleResultPrinter which is Printable has method print(...) which is called by printerJob.print().
SimpleResultPrinterSimulator has the same method print(...) like SimpleResultPrinter except there is no draw actions in it for better performance and of course SimpleResultPrinterSimulator has only this method. Also SimpleResultPrinterSimulator extends Component because i need Graphics object to measure font height.
My solution is just to launch simulation of printing and print(...) makes the same actions like will be done soon during printing except of real drawing to any Graphics object. Finally i recieves total number of pages.
I know this is not elegant and i make the same action twice (lost of performance), but i don't see any other way to estimate number of pages in complex printing.

Categories