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