Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print strings with line breaks in java
    primarykey
    data
    text
    <p>I need to print a string using java so I fond the following solution After googled a lot. I have done some changes to print the string without showing the print dialog. My problem is although this method prints the string properly it doesn't breaks the lines as I have defined. Please tell me how to print strings with line breaks. </p> <pre><code>public class PrintBill implements Printable { private static final String mText = "SHOP MA\n" + "----------------------------\n" + "Pannampitiya\n" + "09-10-2012 harsha no: 001\n" + "No Item Qty Price Amount\n" + "1 Bread 1 50.00 50.00\n" + "____________________________\n"; private static final AttributedString mStyledText = new AttributedString(mText); static public void main(String args[]) throws PrinterException { PrinterService ps = new PrinterService(); PrintService pss = ps.getCheckPrintService("Samsung-ML-2850D-2");//get the printer service by printer name PrinterJob printerJob = PrinterJob.getPrinterJob(); printerJob.setPrintService(pss); Book book = new Book(); book.append(new PrintBill(), new PageFormat()); printerJob.setPageable(book); try { printerJob.print(); System.out.println(printerJob.getPrintService().getName()); System.out.println("Print compleated.."); } catch (PrinterException exception) { System.err.println("Printing error: " + exception); exception.printStackTrace(); } @Override 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() &lt; 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; } } </code></pre> <p>printer service providing class</p> <pre><code>public class PrinterService { public PrintService getCheckPrintService(String printerName) { PrintService ps = null; DocFlavor doc_flavor = DocFlavor.STRING.TEXT_PLAIN; PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); attr_set.add(new Copies(1)); attr_set.add(Sides.ONE_SIDED); PrintService[] service = PrintServiceLookup.lookupPrintServices(doc_flavor, attr_set); for (int i = 0; i &lt; service.length; i++) { System.out.println(service[i].getName()); if (service[i].getName().equals(printerName)) { ps = service[i]; } } return ps; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload