How to print strings with line breaks in java

前端 未结 9 646
刺人心
刺人心 2020-12-01 17:10

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 p

相关标签:
9条回答
  • 2020-12-01 17:34

    OK, finally I found a good solution for my bill printing task and it is working properly for me.

    This class provides the print service

    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 < service.length; i++) {
                System.out.println(service[i].getName());
                if (service[i].getName().equals(printerName)) {
                    ps = service[i];
                }
            }
            return ps;
        }
    }
    

    This class demonstrates the bill printing task,

    public class HelloWorldPrinter implements Printable {
    
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex > 0) { /* We have only one page, and 'page' is zero-based */
                return NO_SUCH_PAGE;
            }
    
            Graphics2D g2d = (Graphics2D) graphics;
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    
            //the String to print in multiple lines
            //writing a semicolon (;) at the end of each sentence
            String mText = "SHOP MA;"
                    + "Pannampitiya;"
                    + "----------------------------;"
                    + "09-10-2012 harsha  no: 001 ;"
                    + "No  Item  Qty  Price  Amount ;"
                    + "----------------------------;"
                    + "1 Bread 1 50.00  50.00 ;"
                    + "----------------------------;";
    
            //Prepare the rendering
            //split the String by the semicolon character
            String[] bill = mText.split(";");
            int y = 15;
            Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 8);
            graphics.setFont(f);
            //draw each String in a separate line
            for (int i = 0; i < bill.length; i++) {
                graphics.drawString(bill[i], 5, y);
                y = y + 15;
            }
    
            /* tell the caller that this page is part of the printed document */
            return PAGE_EXISTS;
        }
    
        public void pp() throws PrinterException {
            PrinterService ps = new PrinterService();
            //get the printer service by printer name
            PrintService pss = ps.getCheckPrintService("Deskjet-1000-J110-series-2");
    
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintService(pss);
            job.setPrintable(this);
    
            try {
                job.print();
            } catch (PrinterException ex) {
                ex.printStackTrace();
            }
    
        }
    
        public static void main(String[] args) {
            HelloWorldPrinter hwp = new HelloWorldPrinter();
            try {
                hwp.pp();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 17:34
    package test2;
    
    public class main {
    
        public static void main(String[] args) {
            vehical vehical1 = new vehical("civic", "black","2012");
            System.out.println(vehical1.name+"\n"+vehical1.colour+"\n"+vehical1.model);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 17:35

    I think you are making it too complex. AttributedString is used when you want to store attributes - in Printing Context. But You are storing data inside that. AttributedString

    Simply, store your data into Document object and pass properties like Font, Bold, Italic everything in AttributedString.

    Hope this will be helpful A quick tutorial And In depth tutorial

    0 讨论(0)
  • 2020-12-01 17:42
    private static final String mText = "SHOP MA" + "\n" +
            + "----------------------------" + "\n" +
            + "Pannampitiya" + newline +
            + "09-10-2012 harsha  no: 001" + "\n" +
            + "No  Item  Qty  Price  Amount" + "\n" +
            + "1 Bread 1 50.00  50.00" + "\n" +
            + "____________________________" + "\n";
    

    This should work.

    0 讨论(0)
  • 2020-12-01 17:45

    Adding /r/n between the Strings solved the problem for me. give it a try. On pasting dont include the '//' for excluding.

    Eg: Option01\r\nOption02\r\nOption03

    this will give output as
    Option01
    Option02
    Option03

    0 讨论(0)
  • 2020-12-01 17:48

    Use String buffer.

      final StringBuffer mText = new StringBuffer("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");
    
    0 讨论(0)
提交回复
热议问题