aspose file tools*
The moose likes Java in General and the fly likes Printing hardcopy - multiple copies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing hardcopy - multiple copies" Watch "Printing hardcopy - multiple copies" New topic
Author

Printing hardcopy - multiple copies

Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
I'm trying to implement print capability in my Java application. I'm implementing printable interface in my program. It works but it prints only one page. If the screen has more than one page to print, it prints only the first page but leaves the other pages.
My print implementation looks like this:
public int
print(Graphics g, PageFormat pf, int pi) throws PrinterException
{
if (pi > 0)
return NO_SUCH_PAGE;
else
{
contentViewer.paint(g);
return Printable.PAGE_EXISTS;
}
}
contentViewer is a JComponent. If i comment
if (pi>0) return NO_SUCH_PAGE part, it prints mulitple copies of the first page. But it doesn't print the second page, third page etc.,
Can anyone suggest me how to print the entire document ?
Thanks,
- Raja.


SCJP
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
if (pi > 0)
return NO_SUCH_PAGE;
will have to be replaced with something more elaborate. I'm not really sure what I'm doing, but maybe you could do something like:
--
int top = pf.getHeight() * (pi - 1);
int bottom = pf.getHeight() * pi;
// is this page number beyond the end?
if (top > documentVerticalSize) return NO_SUCH_PAGE;
// it's not. print this page then
g.translate(0, top);
contentViewer.paint(g);
return Printable.PAGE_EXISTS;
--
I'm not a graphics programmer and don't know the specifics of the Graphics and PageFormat classes, but this might work with some tweaking. You'll probably need to look at the javadocs.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
My problem here is finding the document vertical size.
I've a JComponent which is on the right hand side of split pane. I'm trying to print that. It's contents may vary based on what is present in the left hand side of the split pane. It is dynamic. Is there a way to find the size of my JComponent ?
note: getSize().height, getHeight(), getBounds().height won't work because they return only the size of the component which is shown in the screen. If the component is larger than one page, the right pane has a scroll bar in it. In this case these methods return only the size of the component which is visible in the right pane.
Thanks,
- Raja.
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Hi Raja,
I found the below mentioned article in DDJ'97 issue for multipage printing. I was not able to find the code listing on the website link, but maybe you can get hold of DDJ'97 and get the code listing.
http://www.ddj.com/print/documentID=13011


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Hi gaurava, Thanks for your response. But unfortunately i was unable to get to that article. The link which you mentioned redirects itself to http://www.ddj.com. Can you tell me how to get to that article?
Thanks,
- Raja.
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Do you ask getHeight() to your ScrollPane or to your component, which is inside the scrollpane?
I did the same with printing and for me it worked.
I asked the component, which is in the scrollpane.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Hi Rene,
In my case, i've a split pane and to the right of the split pane i've a JComponent.
Both JComponent.getHeight() and splitpane.getHeight() returns me only the visible area in the screen.
If the JComponent at the right of the split pane occupies more than one screen size, it gets a scroll bar. Here it just returns the screen size which is visible. It is not returning the whole size of the JComponent.
Is there any way to find the whole size of a JComponent? (not just what is visible in the screen)
Thanks,
- Raja.
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Please try this little example:
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Hi Rene,
Yes. This example works when i've JTextArea inside scroll pane in a split pane.
But if i've JContentViewer inside scroll pane in a split pane, it doesn't work. Mine is a help application and JContentViewer is a JavaHelp API. JContentViewer extends JComponent. So, getSize().getHeight() should work. But it is giving me only the visible area's height. It is not giving me the whole area of the JContentViewer. Do you have any idea on this ?
Thanks,
- Raja.
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Raja,
what happens, if you wrap your JContentViewer with a JPanel? At least in my little example it worked too.
So you would have the structure:
JSplitPane - JScrollPane - JPanel - JContentViewer
Sorry, I don't know anything about JContentViewer but could it be, that this viewer has it's own JScrollPane?
I will keep thinking. Sometimes we are searching the bug at a wrong place (like me yesterday evening).
Bye
Rene
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Rene,
JSplitPane - JScrollPane - JPanel - JContentViewer doesn't work. It prints the JPanel's height as 0 and screen is blank. I tried this: JSplitPane - JPanel - JContentViewer. It is now giving the height which is visible in the screen. It is not giving the actual height of the JContentViewer.
I think JContentViewer has it's own scroll pane because when i do directly like JSplitPane - JContentViewer, it gets a scroll bar automatically. Here also it gives the height which is visible in the screen and not the actual height.
Any ideas?
Thanks,
- Raja.
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Hi
I looked to the JavaHelp API and I was not able to see a JContentViewer, only a JHelpContentViewer. Is this the same or do I have the wrong version of JavaHelp (1.1)?
Anyway, you have to try to ask for the component, which is inside the scrollpane.
Rene
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Yes. It is JHelpContentViewer only. That was a typo.
>Anyway, you have to try to ask for the >component, which is inside the scrollpane.
I couldn't understand what you are trying to say here. Can you explain me the nesting and to ask for what inside what?
Thanks,
- Raja.
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Mmhh, ok JHelpContentViewer is a JComponent, so it inherits all methods from it. I think that the JHelpContentViewer contains some other components, like a JScrollPane.
If you look to the Sourcecode of JavaHelp API (BasicContentViewerUI) then you will see, what I mean. Maybe you have to create a recursive function to ask for the JScrollPane. The class, which is inside of the JScrollPane has the name JHEditorPane. This is an inner class of BasicContentViewerUI and extends from JEditorPane.
This mehtod for searching recursively for the inner component of JScrollPane is ugly, but this is the only way I see.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
I'll try that.
Thanks Rene for your help,
- Raja.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Printing hardcopy - multiple copies