Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
热敏打印机上的Java打印可从任意一侧剪切内容_Java_Printing - Fatal编程技术网

热敏打印机上的Java打印可从任意一侧剪切内容

热敏打印机上的Java打印可从任意一侧剪切内容,java,printing,Java,Printing,我正试图用上面的代码将内容从bean对象打印到热敏打印机上,它在其他打印机上工作得很好,但在热敏打印机上不行 含量从两侧切掉,只有不完整的内容放在纸卷的中间。p> 我使用barbacue生成条形码并将图像绘制到图形中, 任何帮助都将不胜感激 package com.iwar.utility; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; impo

我正试图用上面的代码将内容从bean对象打印到热敏打印机上,它在其他打印机上工作得很好,但在热敏打印机上不行

含量从两侧切掉,只有不完整的内容放在纸卷的中间。p> 我使用barbacue生成条形码并将图像绘制到图形中, 任何帮助都将不胜感激

package com.iwar.utility;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO; 
import javax.swing.JOptionPane;

import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;
import net.sourceforge.barbecue.output.OutputException;

import com.iware.connection.db.GetStuff;
import com.iware.pos.beans.PrintableBean;

public class PrinterManager {

public static boolean PrintTicket(final PrintableBean printable, final String input){
    try
    { 

// printable is a bean containing all the information to be printed.

        final Book book = new Book();//java.awt.print.Book
 // here i create a new printable object where draw all the content from the bean         //object onto the graphic g
        Printable b= new Printable() {

            @Override
            public int print(Graphics g, PageFormat pf, int page)
                    throws PrinterException {
                // TODO Auto-generated method stub
                // We have only one page, and 'page'
                // is zero-based
                if (page > 0) {
                    return NO_SUCH_PAGE;
                }
                // User (0,0) is typically outside the
                // imageable area, so we must translate
                // by the X and Y values in the PageFormat
                // to avoid clipping.

 //                 Paper paper = pf.getPaper();

                Paper paper = new Paper();

                double paperWidth = 3;//3.25
                double paperHeight = 5;//11.69
                double leftMargin = 0.12;
                double rightMargin = 0.10;
                double topMargin = 0.75;
                double bottomMargin = 0.01;

                paper.setSize(paperWidth * 100, paperHeight * 100);
                paper.setImageableArea(leftMargin * 200, topMargin * 200,
                     (paperWidth - leftMargin - rightMargin) * 200,
                     (paperHeight - topMargin - bottomMargin) * 200);

                pf.setOrientation(PageFormat.PORTRAIT);
                Graphics2D g2d = (Graphics2D)g; 

                g2d.translate(pf.getImageableX(), pf.getImageableY());
                g.setFont(new Font("Copperplate Gothic Bold", Font.CENTER_BASELINE, 10));
                g.drawString("HUB ENTERTAINMENT LIMITED", 0, 25);
                g.setFont(new Font("Copperplate Gothic Bold", Font.CENTER_BASELINE, 8)); 
                g.setColor(Color.BLACK);
                g.drawString("Ticket Number : " + printable.getTicket_entry_number(), 0, 90);
                g.drawString("Ticket Type   : " + printable.getTicket_type(), 0, 110);
                g.drawString("Movie Name    : " + printable.getMovie_name(), 0, 130);
                g.drawString("Date          : " + printable.getShow_date(), 0, 150);
                g.drawString("Time          : " + printable.getSpecific_time(), 0, 170);
                g.drawString("Theater       : " + printable.getTheater(), 0, 190);
                g.drawString("Cinema Room   : " + printable.getCinema_room(), 0, 210);
                g.drawString("Cost          : " + printable.getCost(), 0, 230);
                g.drawString("Cashier       : " + printable.getCashier(), 0, 250);
                g.drawString("Printed On    : " + GetStuff.getCurrentDate(), 0, 270);
                g.drawString("Seat Number   : " + input , 0, 290);    
                g.drawLine(380, 310, 0, 310);
                g.drawString("Ticket Number : " + printable.getTicket_entry_number(), 0, 350);
                g.drawString("Ticket Type   : " + printable.getTicket_type(), 0, 370);
                g.drawString("Movie Name    : " + printable.getMovie_name(), 0, 390);
                g.drawString("Date          : " + printable.getShow_date(), 0, 410);
                g.drawString("Time          : " + printable.getSpecific_time(), 0, 430);
                g.drawString("Theater       : " + printable.getTheater(), 0, 450);
                g.drawString("Cinema Room   : " + printable.getCinema_room(), 0, 470);
                g.drawString("Cost          : " + printable.getCost(), 0, 490);
                g.drawString("Cashier       : " + printable.getCashier(), 0, 510);
                g.drawString("Printed On    : " + GetStuff.getCurrentDate(), 0, 530);
                g.drawString("Seat Number   : " + input, 0, 550);
                g.drawLine(380, 570, 0, 570);
                // String combo = "Combo";
                BufferedImage img = null;
                g.setFont(new Font("Eras Bold ITC", Font.ROMAN_BASELINE, 20)); 
                if(printable.getTicket_type().equals("VIP")){
                    g.drawString("VIP SEATING", 0, 610);
                    g.drawString("DRINK + SNACK", 0, 630);
                    try {

                        String number = Integer.toString(printable.getTicket_entry_number());
                        Barcode barCode = BarcodeFactory.createCode128(number);
                        img = BarcodeImageHandler.getImage(barCode);
                        //ImageIO.read(new File("image/logo.png"));
                    } catch (BarcodeException | OutputException e) {
                        JOptionPane.showMessageDialog(null,"Failed to Load Image");
                    }


                    int w = img.getWidth(null);
                    int h = img.getHeight(null);
                    BufferedImage bi = new
                            BufferedImage(w, h, BufferedImage.TRANSLUCENT);
                    g = bi.getGraphics();
                    g.drawImage(img, 0, 0, null);
                    /* Draw the image, applying the filter */
                    g2d.drawImage(bi, null, 0, 650);
                }else if(printable.getTicket_type().equals("Combo")){

                    g.drawString("FREE SODA", 0, 610);
                    g.drawString("FREE PORPCORN", 0, 630);
                    try {

                        String number = Integer.toString(printable.getTicket_entry_number());
                        Barcode barCode = BarcodeFactory.createCode128(number);
                        img = BarcodeImageHandler.getImage(barCode);
                        //ImageIO.read(new File("image/logo.png"));
                    } catch (BarcodeException | OutputException e) {
                        JOptionPane.showMessageDialog(null,"Failed to Load Image");
                    }


                    int w = img.getWidth(null);
                    int h = img.getHeight(null);
                    BufferedImage bi = new
                            BufferedImage(w, h, BufferedImage.TRANSLUCENT);
                    g = bi.getGraphics();
                    g.drawImage(img, 0, 0, null);
                    /* Draw the image, applying the filter */
                    g2d.drawImage(bi, null, 0, 650);

                }else{
                    try {

                        String number = Integer.toString(printable.getTicket_entry_number());
                        Barcode barCode = BarcodeFactory.createCode128(number);
                        img = BarcodeImageHandler.getImage(barCode);
                        //ImageIO.read(new File("image/logo.png"));
                    } catch (BarcodeException | OutputException e) {
                        JOptionPane.showMessageDialog(null,"Failed to Load Image");
                    }


                    int w = img.getWidth(null);
                    int h = img.getHeight(null);
                    BufferedImage bi = new
                            BufferedImage(w, h, BufferedImage.TRANSLUCENT);
                    g = bi.getGraphics();
                    g.drawImage(img, 0, 0, null);
                    /* Draw the image, applying the filter */
                    g2d.drawImage(bi, null, 0, 590);

                }

                BufferedImage img2 = null;
                try {
                    img2 = ImageIO.read(new File("images/logo.png"));
                } catch (IOException e) {
                }

                int wi = 150; //img.getWidth(null);
                int hi = 50; //img.getHeight(null);
                BufferedImage bin = new
                        BufferedImage(wi, hi, BufferedImage.TRANSLUCENT);
                g = bin.getGraphics();
                g.drawImage(img2, 0, 0, null);
                /* Draw the image, applying the filter */
                g2d.drawImage(bin, null, 0, 26);



                book.append(this, pf);
                // tell the caller that this page is part
                // of the printed document
                return PAGE_EXISTS;

            }
        };
        PrinterJob job = PrinterJob.getPrinterJob();

        job.setPrintable(b);



        if (job.printDialog())
        {


            //job.setPageable(book);

            job.print();
            return true;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return false;



}

}

打印机驱动程序可能有问题。打印机是否能够在白色区域内打印?您可以从其他应用程序执行此操作吗?你能从其他Java应用程序中做到这一点吗?与我的其他Java应用程序的情况相同,即使是hello world示例也被删除了,但我用vb.net设计的其他应用程序工作得非常好。