我正在打印包含文本的Swing组件。 Swing组件在屏幕上呈现文本很好,但是,当我打印它(到.tif文件)时,字符都被粉碎在一起。这是为什么?
运行此代码以查看我的意思:
import javax.swing.*;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public final class PrintingDemo2 implements Printable {
private final JTextPane textPane;
private static final String WORDS = "GOOD MORNING\u00AE AMERICA";
private static final String TEXT = WORDS + '\n' + WORDS + '\n' + WORDS + '\n' + WORDS + '\n' + WORDS + '\n' + WORDS;
public static void main(String[] args) {
new PrintingDemo2();
}
public PrintingDemo2() {
textPane = new JTextPane();
textPane.setText(TEXT);
final StyledDocument document = textPane.getStyledDocument();
String[] fontFamilies = new String[]{"Tahoma", "SimSum", "MS Mincho", "Batang", "Arial", "Times New Roman"};
for (int i = 0; i < fontFamilies.length; i++) {
final MutableAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setFontFamily(attributeSet, fontFamilies[i]);
StyleConstants.setFontSize(attributeSet, 14);
document.setParagraphAttributes(i * 22, 21, attributeSet, true);
}
final AbstractButton printContextButton = new JButton("Print Context");
printContextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(PrintingDemo2.this);
try {
job.print();
} catch (PrinterException ex) {
throw new RuntimeException("Printing Failed.", ex);
}
}
});
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(printContextButton);
contentPane.add(new JScrollPane(textPane));
frame.setSize(400, 200);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;
RepaintManager mgr = RepaintManager.currentManager(textPane);
mgr.setDoubleBufferingEnabled(false);
final Graphics2D graphics2D = (Graphics2D) graphics;
graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
textPane.paint(graphics);
mgr.setDoubleBufferingEnabled(true);
return Printable.PAGE_EXISTS;
}
}
答案 0 :(得分:3)
您可以尝试栅格化(将其绘制为300dpi的BufferedImage),然后打印该图像。 Hacky,性能不好(发送到打印机而不是矢量数据的巨大光栅化文件),但至少你不会有字体问题。
要对其进行栅格化,请创建一个与屏幕面板大小相差4.17倍(300/72)的BufferedImage,并将其图形对象缩放为相同的值,然后将面板绘制到缓冲图像的Graphics2D对象上。 / p>
免责声明:这不优雅,我知道。如果有人知道如何说服每个品牌和型号的打印机接收Java打印过程中的字体,请加入!
答案 1 :(得分:0)
检查打印的字体是否与您在屏幕表格中看到的字体相同。如果打印机替换字体,它可能会与屏幕字体不同地显示和提示。
首先,尝试将其修剪为仅一种字体。那里有许多活动部件。
好的,我刚尝试过,似乎工作正常,从Mac OS / X 10.5.6打印到LaserJet 1300。
我会尝试不同的打印设备;我敢打赌,在Java完成数据之后,它正在发生。