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
Hello,

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
advance.

---------------snip---------------
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)
{
printJob.printDialog();
}

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);
}

printJob.setPageable(book);

try
{
printJob.print();
}
catch (Exception PrintException)
{
PrintException.printStackTrace();
}
}
}
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;

g2d.setColor(settings.getFontColor());

font = settings.getFont();
g2d.setFont(font);

//--- Translate the origen to be (0,0)
g2d.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());

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

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

lineTokenizer = new StringTokenizer(pageBuffer, newLineChars,
true);

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.
++newLineCharCount;
if (newLineCharCount == newLineChars.length())
{
newLineCharCount = 0;
horizontal = (int) settings.getMargin();
vertical += largestFontOnLine;
}
continue;
}

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

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

return PAGE_EXISTS;
}
}
----------------snip--------------------
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

----------snip----------
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));
pageFormat.setOrientation(orientation);
paper = pageFormat.getPaper();

paper.setImageableArea(settings.getMargin(),
settings.getMargin(), paper.getWidth()-(2*settings.getMargin()),
paper.getImageableHeight()-(2*settings.getMargin()));

pageFormat.setPaper(paper);

return pageFormat;
}
------------snip------------

happy trails
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.