Results 1 to 3 of 3
- 06-25-2010, 08:39 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
Spacing/Alignment problem with java 2D printing
Hi,
AWT framework i am using for printing.
I am getting problem while printing with java. I have a string which looks like (Replace * is with a space character)
"Retailer 001************************************MY INVOICE: MI1006151T10000"
"PH: 1234567, 2134568***********************NAME: MAYUR MAHESHWARI"
This is the string which i created dynamically through string operation. Now when i am going to print it with font 'Courier' or 'DialogInput' then it gets printied properly, but if use fonts other then the mentioned above then alignment is not proper something like below:
"Retailer 001*****************************MY INVOICE: MI1006151T10000"
"PH: 1234567, 2134568************************NAME: MAYUR MAHESHWARI"
Please suggest me a solution. The code i am using is mentioned below:
{code}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException{
if (pageIndex != 0) {
return NO_SUCH_PAGE;
}
Graphics2D graphics2D = (Graphics2D)graphics;
graphics2D.setFont(typeFont);
int x = (int)pageFormat.getImageableX();
int y = (int)pageFormat.getImageableY();
graphics2D.translate(x, y);
List<String> data = this.getDataList();
FontMetrics fm = graphics2D.getFontMetrics();
int stringHeight = 0;
int maxWidth = 0;
FontMetrics fm1 = graphics2D.getFontMetrics();
for(String line : data){
int width = fm1.stringWidth(line);
if(maxWidth < width)
maxWidth = width;
}
System.out.println("maxWidth:- " + maxWidth);
for(String line : data){
stringHeight += fm.getHeight();
int ascent = fm.getAscent();
int yCoordinate = stringHeight + ascent;
String tempLine = line.trim();
if(tempLine.startsWith("-") && tempLine.endsWith("-")){
System.out.println("line:- " + tempLine);
graphics2D.drawLine(x + 10, yCoordinate, maxWidth, yCoordinate);//(line, x, y);
}else{
AttributedString attString = new AttributedString(line);
attString.addAttribute(TextAttribute.FONT, typeFont);
//graphics2D.drawString(attString.getIterator(), x, yCoordinate);
//drawString((Graphics2D)g, line, x, yCoordinate, PrintPreview.width);
try{
graphics2D.drawBytes(line.getBytes(),0,line.getByt es().length, x, yCoordinate);
}catch(Exception e){
e.printStackTrace();
}
}
}// End of for loop
return PAGE_EXISTS;
}
{code}
- 06-25-2010, 02:59 PM #2
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
Problem Area
The pixel size for every character is differ while rendering a String. For ex. Serif will took 7 pixel to render a character 'A' or took 3 pixel to render a space or took 4 pixel to render character 'i'. You have to set the same pixel size for every character. I think after that your problem will be solved.
- 06-25-2010, 03:37 PM #3
Similar Threads
-
BoxLayout Alignment problem
By whiteMath in forum AWT / SwingReplies: 12Last Post: 07-19-2010, 10:04 PM -
GridLayout alignment problem
By q8inq8 in forum New To JavaReplies: 9Last Post: 03-16-2010, 07:54 PM -
Problem with margins/spacing
By joseche in forum AWT / SwingReplies: 2Last Post: 12-26-2008, 08:22 PM -
[SOLVED] alignment problem
By nanimtech in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-10-2008, 01:23 PM -
Help with spacing in java
By barney in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks