0

这是计费应用程序(如超市计费)。我使用以下代码在默认的外部打印机(Posiflex)中打印了一些东西

我正在与spring-boot, maven, Java.

package com.spring.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;

public class Printer implements Printable {
public static void main(String[] args) {

  Printer example1 = new Printer();
  System.exit(0);
}


 private final double INCH = 25;

 public Printer() {

 PrinterJob printJob = PrinterJob.getPrinterJob();

 printJob.setPrintable(this);

  try {
    printJob.print();
  } catch (Exception PrintException) {
    PrintException.printStackTrace();
  }

 }


public int print(Graphics g, PageFormat pageFormat, int page) {

 if (page == 0) {  


Graphics2D g2d = (Graphics2D) g;
g.setFont(new Font("TimesRoman", Font.PLAIN, 10)); 
FontMetrics fontMetrics = g.getFontMetrics();
  g2d.setColor(Color.black);
  g2d.translate(pageFormat.getImageableWidth(), pageFormat
        .getImageableHeight());

  g.drawString("XYZ Textiles", 0, 10);
  g.drawString("no 123, abc street, state", 0, 30);
  g.drawString("country, zip xxxxxx", 0, 50);
  g.drawString("sales@xyztex.com", 0, 70);

  return (PAGE_EXISTS);
} else
  return (NO_SUCH_PAGE);
}

} 

它在打印机中成功打印文本。但它从打印机纸卷的中间开始打印。

我将 x 坐标设为 0,即使它是从中间开始的

实际的

------------------------------------
|                                  |
|                                  |
|              XYZ Textiles        |
|              no 123, abc street, |
|              country, zip xxxxxx |
|              sales@xyztex.com    |
|                                  |
------------------------------------

预期的

------------------------------------
|                                  |
|                                  |
|           XYZ Textiles           |
|        no 123, abc street        |
|       country, zip xxxxxx        |
|        sales@xyztex.com          |
|                                  |
------------------------------------

如何自定义它以按预期打印?

4

0 回答 0