Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Invoking printer using java  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
Hi all,

Please skim through this code,

import java.awt.print.*;
import java.text.*;

public class PrintText implements Printable {

private static final String mText ="Device Name: "+acmtxt_DeviceName.getText()+"\n Alarm Text :"++acmtxt_AlarmText.getText()+"\n Time stamp: "+acmtxt_TimeStamp.getText()+"\n Alarm Detail: "+jta_AlarmDetail.getText();

private static final AttributedString mStyledText = new AttributedString(mText);

static public void main(String args[]) {

PrinterJob printerJob = PrinterJob.getPrinterJob();

Book book = new Book();
book.append(new PrintText(), new PageFormat());

printerJob.setPageable(book);

boolean doPrint = printerJob.printDialog();
if (doPrint) {
try {
printerJob.print();
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
}
}
public int print(Graphics g, PageFormat format, int pageIndex) {

Graphics2D g2d = (Graphics2D) g;

g2d.translate(format.getImageableX(), format.getImageableY());

g2d.setPaint(Color.black);

Point2D.Float pen = new Point2D.Float();
AttributedCharacterIterator charIterator = mStyledText.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
float wrappingWidth = (float) format.getImageableWidth();
while (measurer.getPosition() < charIterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout(wrappingWidth);
pen.y += layout.getAscent();
float dx = layout.isLeftToRight()? 0 : (wrappingWidth - layout.getAdvance());
layout.draw(g2d, pen.x + dx, pen.y);
pen.y += layout.getDescent() + layout.getLeading();
}
return Printable.PAGE_EXISTS;
}


Please skim through the above code. It does well of printing, the only issue was it does not print what i has inputted text. Rather than it prints sequentially. Anyone can help me out in this regard to print this as this form

Device Name: N/A
Alarm Text: New Device Found.
Time Stamp: 2007-09-01 19:57:56
Alarm Detail: New Device Found. IP Address: 135.111.122.56

Now it prints as Device Name: N/AAlarm Text: New Device Found.Time Stamp: 2007-09-01 19:57:56Alarm Detail: New Device Found. IP Address: 135.111.122.56

Any one please help me out, immediate reply it would be helpful..

Thanks in advance,
Ram
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!