By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,797 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,797 IT Pros & Developers. It's quick & easy.

Landscape Orientation margin problems

P: n/a

The code which I have attached below properly has the margins working
in Portrait mode. The margin is contained in an object called
settings. The behavior is that the top and bottom margins work
correctly in both Landscape and Portrait. The left margin ends up
being large, around 2.5 inches, in Landscape mode, while in Portrait
it correctly is at .25 inches.

I was careful to set the orientation first, then the rest of the
paperformat and paper settings. So I am confused as to why it is not
working. Any help would be great! Thanks for all the help in

public class Printer
public void toPrinter(String buffer, boolean confirm)
Book book;
PrinterPage printerPage;
String formattedBuffer;
PrinterJob printJob;
StringTokenizer pageTokenizer;
String pageStr;
PageFormat pageFormat;

formattedBuffer = formatBuffer(buffer);

printJob = PrinterJob.getPrinterJob();

if (confirm == true)

book = new Book();

pageTokenizer = new StringTokenizer(formattedBuffer, "\f");
while (pageTokenizer.hasMoreTokens())
pageStr = pageTokenizer.nextToken();
pageFormat = getPageFormat(printJob);
printerPage = new PrinterPage(settings, pageStr);
book.append(printerPage, pageFormat);


catch (Exception PrintException)
public class Printerpage extends Printable

public int print(Graphics g, PageFormat pageFormat, int pageIndex)
double height;
String newLineChars;
int newLineCharCount;
String line;
StringTokenizer lineTokenizer;

height = pageFormat.getImageableHeight();

//--- Create a graphic2D object a set the default parameters
g2d = (Graphics2D) g;


font = settings.getFont();

//--- Translate the origen to be (0,0)

//--- Print the output buffer
newLineChars = System.getProperty("line.separator");
newLineCharCount = 0;

horizontal = vertical = (int) settings.getMargin();
largestFontOnLine = settings.getFontPoints();

lineTokenizer = new StringTokenizer(pageBuffer, newLineChars,

while (vertical < height && lineTokenizer.hasMoreTokens())
line = lineTokenizer.nextToken();

// check for an empty line
if (newLineChars.indexOf(line.charAt(0)) != -1)
// all characters comprising a new line must appear to
define a new line
// Unix systems are only \r while Windows are \n\r.
This is written to be
// OS independant.
if (newLineCharCount == newLineChars.length())
newLineCharCount = 0;
horizontal = (int) settings.getMargin();
vertical += largestFontOnLine;

// safety - checking if rendering occuring out of bounds
if (vertical > pageFormat.getImageableHeight())

g2d.drawString(line, horizontal, vertical);
horizontal += (settings.getFontPoints() * token.length());

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
Sorry, I had forgotten to include this method from class Printer

public PageFormat getPageFormat(PrinterJob printJob)
PageFormat pageFormat;
Paper paper;
int orientation;

pageFormat = printJob.defaultPage();

orientation =
PrinterCommands.orientationToJavaOrientation(DynaP roperties.getProperty(DynaProperties.PROP_PRINT_OR IENTATION));
paper = pageFormat.getPaper();

settings.getMargin(), paper.getWidth()-(2*settings.getMargin()),


return pageFormat;

happy trails
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.