print the full content of JScrollpane
Hi,
I want to print JPanel (print, export to PDF, etc.) which is inside jScrollpane. JPanel is kinda large.
I need to scale it to fit A4 width and then print it (probably many pages will be needed).
I tried to use iText to export to Pdf, but it exports only the first page.
I tried to overwrite print method for JFrame(which contains JScrollpane), smth like
public int print(Graphics g, PageFormat pageFormat,
int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight=g2.getFontMetrics().getHeight();
int fontDesent=g2.getFontMetrics().getDescent();
...
double pageWidth =
pageFormat.getImageableWidth();
double rectWidth = (double) drawRect.getWidth();
...
double scale = 1;
if (rectWidth >= pageWidth) {
scale = pageWidth / rectWidth;
logger.debug("scale " + scale);
}
double rectHeight = drawRect.getHeight()*scale;
...
double rectHeightPage = pageFormat.getImageableHeight() - 25;
...
int totalNumPages=
(int)Math.ceil((
(double)rectHeight/rectHeightPage));
logger.debug("totalNumPages " + totalNumPages);
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
if (pageIndex + 1 == totalNumPages) {
...
}
//else clip to the entire area available.
else{
g2.setClip(0,
(int)(rectHeightPage*pageIndex),
(int) Math.ceil(rectWidth),
(int) Math.ceil(
rectHeightPage
));
...
}
g2.scale(scale,scale);
drawRect.paint(g2);
g2.scale(1/scale,1/scale);
return Printable.PAGE_EXISTS;
}
Scaling works, but again - it prints only the first page, and the rest of pages are empty.
What shall I do? Please help.
Re: print the full content of JScrollpane
Can you advance the contents of the JScrollPane as each new page is requested to be printed? What is the value of pageIndex?
I haven't worked with printing in a while.
Re: print the full content of JScrollpane
pageIndex values are from 0 till totalNumPages.
setClip selects the right area (according to logger - Set clip x: 0; y: 2450; width:1165; height: 817, etc. - it changes the y coordinate as it should do).
But anyway other pages are empty ;(
And I am a total newbie, working with Swing for the first time actually....
Re: print the full content of JScrollpane
OK, I found a mistake.
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
I changed to g2.translate(pageFormat.getImageableX(), -rectHeightPage*pageIndex); and it works.
But it separates pages by pixels (meaning if I have some text on the picture, the letters are split across pages).
Any ideas how to solve it?
Re: print the full content of JScrollpane
Quote:
the letters are split across pages). Any ideas how to solve it?
Scan the image and set the page length to be before the letters that are split.
Re: print the full content of JScrollpane
And how to do the scanning?
P.S. Images can be of different size; multiple textareas on them can be of different size and on different positions.
Re: print the full content of JScrollpane
Where do you want the page break to be?
Do you know how many pixels fit on a page vertically?
Start at that y location and move up until you find a horizontal row of pixels that do not have and thing that you don't want to split across pages. Like a row of white pixels.
Re: print the full content of JScrollpane
There can be vertical lines and the background color is set.
But I do not want to make breaks on text, horizontal lines or any other graphics.
And since I am a total newbie, I do not know how to scan JPanel Graphics to get pixel array or smth and how to get to know if there is text/smth.
Re: print the full content of JScrollpane
When you build the page layout, can you determine where the components are being placed?
If you can, then make sure they are not crossing a page boundary.
Re: print the full content of JScrollpane
It is generated dynamically...
Meaning I check the y coordinate of the last drawn object;
and then smth like
lastY = lastY + newObject.getHeight + someExtraSpace.
So, I can't know the coordinates of objects on the Graphics (objects - polygons, lines, texts - everything of different size).
Re: print the full content of JScrollpane
And btw pageLayout is null (otherwise I couldn't position textAreas properly).
Re: print the full content of JScrollpane
If you are positioning the components (layout = null) then you should have 100% control over where the component is positioned.
Re: print the full content of JScrollpane
Well, the positioning is kinda relative - according to other objects positions....
Re: print the full content of JScrollpane
Yes, of course. Your problem is to keep components from spanning a page break.
Since you have 100% control of where you put each component and you know where the page break is, you should be able to detect if a component will span the break.