如何在 java 中使用 printdialog 导出多行文本?

How to export multiline text with printdialog in java?

我正在尝试使用 java print/ printdialog 导出一些结果。我想打印多行(使用“\n”),但我导出的是简单的行。我该如何解决这个问题?

这是我的代码示例:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintableDemo1 implements Printable {

  public int print(Graphics g, PageFormat pf, int pageIndex) {


    StringBuilder builder = new StringBuilder();
    builder.append("Test Result:").append("\n")
    .append("First Line").append("\n")
            .append("Second Line").append("\n")
            .append("Third Line").append("\n")
            .append("Fourth Line").append("\n")
            .append("Last Line");


    if (pageIndex != 0)
      return NO_SUCH_PAGE;

    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 12));
    g2.setPaint(Color.black);
    g2.drawString(builder.toString(), 144, 144);
    return PAGE_EXISTS;
  }

  public static void main(String[] args) {

    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setPrintable(new PrintableDemo1());

    try {
      pj.print();
    } catch (PrinterException e) {
      e.printStackTrace();
    }

  }

}

感谢@MadProgrammer,我找到了一些解决方案,并更新了我的代码。

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintableDemo1 implements Printable {

  public int print(Graphics g, PageFormat pf, int pageIndex) {

    StringBuilder builder = new StringBuilder();
    builder.append("Test Result:").append("\n")
    .append("First Line").append("\n")
            .append("Second Line").append("\n")
            .append("Third Line").append("\n")
            .append("Fourth Line").append("\n")
            .append("Last Line");

    if (pageIndex != 0)
      return NO_SUCH_PAGE;

    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 12));
    g2.setPaint(Color.black);
    myDrawString(g2, builder.toString(), 100, 100);

    return PAGE_EXISTS;
  }

  private void myDrawString(Graphics g, String text, int x, int y) {
      for (String line : text.split("\n"))
        g.drawString(line, x, y += g.getFontMetrics().getHeight());
  }

  public static void main(String[] args) {

    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setPrintable(new PrintableDemo1());

    try {
      pj.print();
    } catch (PrinterException e) {
      e.printStackTrace();
    }

  }

}