简体   繁体   中英

Why printed me one line in SWT Eclipse (Text). I wanted to print multi line in my Text

Why printed me one line in SWT Eclipse (Text). I wanted to print multi line in my Text. That is my code. From my printer i got one line of my message instead of many rows in my print

Button btnPrint = new Button(shell, SWT.NONE);
    btnPrint.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {

             PrintDialog printDialog = new PrintDialog(shell, SWT.MULTI| SWT.BORDER );
                printDialog.setText("Print");
                PrinterData printerData = printDialog.open();
                if (!(printerData == null)) {
                  Printer p = new Printer(printerData);
                  p.startJob("PrintJob");
                  p.startPage();

                Rectangle trim = p.computeTrim(0,0,0,0);
                  Point dpi = p.getDPI();
                  int leftMargin = dpi.x + trim.x;
                  int topMargin = dpi.y / 2 + trim.y;
                  GC gc = new GC(p);
                  Font font = gc.getFont();
                  String printText = text.getText();
                  Point extent = gc.stringExtent(printText);

                  gc.drawString(printText, leftMargin, topMargin+ font.getFontData()[0].getHeight());
                  p.endPage();
                  gc.dispose();
                  p.endJob();
                  p.dispose();
                }
        }
    });

gc.drawString does not look for line delimiters.

Use gc.drawText :

public void drawText(String string, int x, int y)

which does look for line delimiters (and tabs). Or use

public void drawText(String string, int x, int y, int flags)

and specify SWT.DRAW_DELIMITER for the flags value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM