1

我正在尝试打印与我的小程序 GUI 中相同的内容。

事情是当我打印它时它并不完全相同......每个组件的大小以不成比例的方式变化。

图形用户界面:

在此处输入图像描述

打印:

在此处输入图像描述

边距不是问题,我可以轻松修复它们...但是您可以看到条形码和垂直文本的大小不合适。

主类:

    public class Impresion extends Applet{
        PrinterJob job = PrinterJob.getPrinterJob();
        Panel test;
        Label test2;
        Label test3;
        String copyParam = null;
        String dialogParam = null;
        String codeParam = null;
        String descParam = null;
        public void init(){
            copyParam = this.getParameter("copias");
            dialogParam = this.getParameter("ventanita");
            codeParam = this.getParameter("codigo");
            descParam = this.getParameter("descripcion");
            copyParam = "1";
            dialogParam = "1";
            codeParam = "002/113";
            descParam = "asd";

            if (copyParam == null || dialogParam == null || codeParam == null || descParam == null){
                System.out.println("Faltan parametros. - "+copyParam+" - "+dialogParam+" - "+codeParam+" - "+descParam);
            } else {
                job.setPrintable(new Dibujar(codeParam, descParam));
                PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                float leftMargin = (float) 0.1;
                float topMargin = (float) 0.1;
                float rightMargin = (float) 0.1;
                float bottomMargin = (float) 0.1;
                float mediaWidth = (float) 60.0;
                float mediaHeight = (float) 50.0;
                aset.add(new MediaPrintableArea(leftMargin, topMargin, (mediaWidth - leftMargin - rightMargin), (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));
                int copias = Integer.parseInt(copyParam);
                aset.add(new Copies(copias));
                boolean imprimir = true;
                if (dialogParam.indexOf('1') != -1){
                    imprimir = job.printDialog(aset);
                }
                if (imprimir){
                    try {
                        job.print(aset);
                    } catch (PrinterException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

可打印类:

    public class Dibujar implements Printable{
        String codeParam = null;
        String descParam = null;
        public Dibujar(String code, String desc) {
            codeParam = code;
            descParam = desc;
        }

        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException{
            if (pageIndex > 0){
                return NO_SUCH_PAGE;
            }
            Barcode b = null;
            try {
                b = BarcodeFactory.createCodabar(codeParam);
                b.setDrawingText(false);
                b.setBarWidth(1);
                b.draw((Graphics2D) g, 30, 8);
            } catch (BarcodeException | OutputException e1) {
                e1.printStackTrace();
            }
            g.setFont(new Font("Arial", Font.BOLD, 9));
            String description = descParam;
            g.drawString(description, 16, 70);
            AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(90),16,8);
            Graphics2D g2 = (Graphics2D) g;
            g2.transform(at);
            g.setFont(new Font("Arial", Font.BOLD, 14));
            g2.drawString(codeParam.replace("/",  ""),16,8);
            g2.transform(new AffineTransform());
            return PAGE_EXISTS;
        }
    }
4

1 回答 1

0

在此调整参数:

aset.add(new MediaPrintableArea(leftMargin, topMargin, (mediaWidth - leftMargin - rightMargin), (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));

或者调整这两个中的参数:

b.draw((Graphics2D) g, 30, 8);
//....
g2.drawString(codeParam.replace("/", ""),16,8);
于 2013-08-07T14:58:51.370 回答