如何解决如何在打印区域内添加两个图像?
我正在使用热敏打印机打印。我可以打印文本并且效果很好,但我想将 2 张图像附加到文本中,一个在文本的顶部,一个在文本的底部。 我正在尝试使用此代码,它可以完美地打印文本,但我尝试打印的唯一一个图像没有显示。 也许我应该将它添加到 g2d 变量中? 如果这是问题所在,我怎么能不删除我的文本呢?
class ReceiptPrint implements Printable {
SimpleDateFormat df = new SimpleDateFormat();
String receiptDetailLine;
public static final String pspace = " ";//15-spaces
public int print(Graphics graphics,PageFormat pageFormat,int pageIndex)
throws PrinterException {
df.applyPattern("dd/MM/yyyy HH:mm:ss");
String strText = null;
final String LF = "\n";// text string to output
int linestart; // start index of line in textarea
int lineEnd; // end index of line in textarea
int lineNumber;
int lineCount;
final String SPACE = " ";//10 spaces
final String SPACES = " ";//9
final String uline = "________________________________________";
final String dline = "----------------------------------------";
String greetings = "GRAZIE PER L'ACQUISTO";
receiptDetailLine = "asdasdasda";
Graphics2D g2d = (Graphics2D) graphics;
Font font = new Font("MONOSPACED",Font.BOLD,9);
if (pageIndex < 0 || pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
JTextArea textarea = new JTextArea(10,40);
textarea.append(SPACES + "sadasdsad" + "\n");
textarea.append(" " + SPACES + "sadasdsad" + "\n");
textarea.append(SPACES + "sadasdsad" + "\n");
textarea.append("" + SPACES + "sadasdsad" + "\n");
textarea.append(SPACES + "sadasdsad" + "\n");
textarea.append(uline + "\n");
textarea.append("Order Ref:" + " " + receiptDetailLine + "\n");
textarea.append(dline + "\n");
textarea.append(" Qty Description" + SPACES + " Price" + LF);
textarea.append(dline + "\n");
System.out.println(2);
String printedLine = " Service Charge Complimentary";
textarea.append(printedLine + LF);
textarea.append(LF + SPACES + " Your Reciept\n" + SPACE + greetings + LF);
textarea.append(df.format(new Date()) + LF);
textarea.setEditable(false);
g2d.translate(pageFormat.getimageableX(),pageFormat.getimageableY());
g2d.setFont(font);
lineNumber = 0;
lineCount = textarea.getLineCount();
strText = textarea.getText();
//Provo a stampare un'immagine
MediaTracker mt = new MediaTracker(textarea);
String imagePath = "/mydir/TempQr1.bmp";
//--- Load the image and wait for it to load
Image image = Toolkit.getDefaultToolkit().getimage(imagePath);
mt.addImage(image,0);
while (lineCount != 0) {
try {
linestart = textarea.getLinestartOffset(lineNumber);
lineEnd = textarea.getLineEndOffset(lineNumber);
strText = textarea.getText(linestart,lineEnd - linestart);
} catch (Exception exception) {
System.out.println("Printing error:" + exception); // have to catch BadLocationException
}
g2d.drawString(strText,1,(lineNumber + 1) * 18);
//g2d.drawIma
//spacing between lines
lineNumber = lineNumber + 1;
lineCount--;
}
return Printable.PAGE_EXISTS;
}
}
关于我应该如何做的任何建议?
感谢您的帮助!
解决方法
感谢@camickr,我创建了一个 JPanel 并将所有组件添加到其中,然后我使用此代码打印了 JPanel,其中 comp 是我的 JPanel:
public void stampaDirettamente(Component comp,boolean fill) throws PrinterException {
Paper paper = new Paper();
double paperWidth = 3;//3.25
double paperHeight = 6.69;//11.69
double leftMargin = 0.12;
double rightMargin = 0.10;
double topMargin = 0;
double bottomMargin = 0.01;
paper.setSize(paperWidth * 200,paperHeight * 200);
paper.setImageableArea(leftMargin * 200,topMargin * 200,(paperWidth - leftMargin - rightMargin) * 200,(paperHeight - topMargin - bottomMargin) * 200);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
PageFormat pf = new PageFormat();
pf.setPaper(paper);
pf.setOrientation(PageFormat.PORTRAIT);
BufferedImage img = new BufferedImage(
(int) Math.round(pf.getWidth()),(int) Math.round(pf.getHeight()),BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fill(new Rectangle(0,img.getWidth(),img.getHeight()));
ComponentPrinter cp = new ComponentPrinter(comp,fill);
try {
cp.print(g2d,pf,0);
} finally {
g2d.dispose();
}
PrinterJob printerJob = PrinterJob.getPrinterJob();
Printable printable = cp;
pf = printerJob.validatePage(pf);
//boolean don = printerJob.printDialog(); //Uncomment this to have a print dialog
printerJob.setPrintable(printable,pf);
try {
printerJob.print(aset);
} catch (Exception e) {
e.printStackTrace();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。