This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes print preview Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "print preview" Watch "print preview" New topic

print preview

Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 275
Hello. If somebody has a minute, I've been working through a print preview app I found on the web. I am having a problem with this because the vertical image doesn't seem to stop although I am feeding in the default page format. The default page format is 1 in. margins all around. When the preview comes out, it captures the top margin as well as left and right, but the image blows through the 1 inch bottom margin and prints put all the way to the bottom.

With that behavior, it seems the BufferedImage isn't cutting off according to the default page format. All the system prints in this code snippet indicate the 1 inch margins (8.5x11 Portrait) for the PageFormat. When actually printing by translating the Graphics of the image depending on margins and page number, the pageformat seems to be "adhered to"...but here, I seem to be getting one huge image. I've been trying to mess with rendering a small rectangle representing the printable area to the "8.5x11" rectangle representing the page itself...but I can't get over why this doesn't work in the first place, since it prints out fine and three margins are ok. Well, if any of this makes sense, I'd appreciate any input. Thank you very much for reading.

PrinterJob prnJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = prnJob.defaultPage();

m_wPage = (int)(pageFormat.getWidth());
m_hPage = (int)(pageFormat.getHeight());

// THIS IS INCHES (1/72 in)
int m_wPage_imageable = (int)pageFormat.getImageableWidth();
System.out.println("imageable_width - " + m_wPage_imageable);

int m_hPage_imageable = (int)pageFormat.getImageableHeight();
System.out.println("imageable_height - " + m_hPage_imageable);

int m_x = (int)pageFormat.getImageableX();
System.out.println("m_x - " + m_x);

// upper left y coordinate
int m_y = (int)pageFormat.getImageableY();
System.out.println("m_y - " + m_y);

int scale = 10;
int w = (int)(m_wPage*scale/100);
int h = (int)(m_hPage*scale/100);

int pageIndex = 0;
try {
while (true) {
BufferedImage img = new BufferedImage(m_wPage, m_hPage, BufferedImage.TYPE_INT_RGB);
Graphics g1 = img.getGraphics();

g.fillRect(0, 0, m_wPage, m_hPage);...
[ March 31, 2006: Message edited by: Tom Griffith ]
Tom Griffith
Ranch Hand

Joined: Aug 06, 2004
Posts: 275
Hello. If anybody was able to decipher what I typed above, I brewed up a workaround via clipping rectangles of the imageable areas out of the loaded image for each page iteration...

if (pageIndex==0) {
g.setClip(new Rectangle(0,0,m_wPage,m_hPage-72));
} else {
int new_x=0;
int new_y=72;
int new_w=m_wPage;
int new_h=m_hPage_imageable*pageIndex;
g.setClip(new Rectangle(new_x,new_y,new_w,new_h));

I kinda hard-coded that '72' in there for now to represent the top 1 inch margin but I'm sure I can derive the upper default print margin from the page format instead. I guess this is just a bybrid of translating when actually painting graphics. Welp, I hope this helps somebody. Thank you for reading.
[ April 03, 2006: Message edited by: Tom Griffith ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
there were a couple of posts to this old thread yesterday

don't know if anything is relevant to your problem,
I just remembered the subject had "print preview",
and could be worth a read.
I agree. Here's the link:
subject: print preview
jQuery in Action, 3rd edition