Java awt连接打印机打印打印word文档输出自定义格式

maven坐标

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-mock</artifactId>
            <version>2.0.8</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.15</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>

打印类

package com.ydkj.common.print;

import com.ydkj.common.core.domain.entity.drinks.LiqCheck;
import com.ydkj.common.utils.StringUtils;

import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @author:myb
 * @description:
 * @date:2023/9/27 14:40
 */
public class PrintHelp  extends JFrame
    implements ActionListener, Printable
{
    private JButton printTextButton = new JButton("Print Text");
    private JButton previewButton = new JButton("Print Preview");
    private JButton printText2Button = new JButton("Print Text2");
    private JButton printFileButton = new JButton("Print File");
    private JButton printFrameButton = new JButton("Print Frame");
    private JButton exitButton = new JButton("Exit");
    private JLabel tipLabel = new JLabel("");
    private JTextArea area = new JTextArea();
    private JScrollPane scroll = new JScrollPane(area);
    private JPanel buttonPanel = new JPanel();

    private int PAGES = 0;
    private String printStr;

    private String name = null;

    private String sex = null;
    private String xuehao = null;
    private String sfz = null;
    private String ruxue = null;
    private String major = null;
    private String grade = null;
    private String date = null;
    private String zymc = null;
    private String zyhgx = null;

    private String bottleBodyCodeOne = null;
    private String bottleCapCodeOne = null;

    private String bottleBodyCodeTwo = null;
    private String bottleCapCodeTwo = null;

    private String bottleBodyCodeThree = null;
    private String bottleCapCodeThree = null;

    private String bottleBodyCodeFour = null;
    private String bottleCapCodeFour = null;

    private String bottleBodyCodeFive = null;
    private String bottleCapCodeFive = null;

    private String bottleBodyCodeSix = null;
    private String bottleCapCodeSix = null;

    private String bottleBodyCodeSeven = null;
    private String bottleCapCodeSeven = null;

    private String bottleBodyCodeEight = null;
    private String bottleCapCodeEight = null;

    private String bottleBodyCodeNine = null;
    private String bottleCapCodeNine = null;

    private String bottleBodyCodeTen = null;
    private String bottleCapCodeTen = null;


    public PrintHelp() {
        this.setTitle("Print Test");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds((int) ((SystemProperties.SCREEN_WIDTH - 800) / 2), (int) ((SystemProperties.SCREEN_HEIGHT - 600) / 2), 800, 600);
        initLayout();
    }

    /**
     * 第一张卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
    }

    /**
     * 第二张卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                        String bottleBodyCodeTwo,String bottleCapCodeTwo) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;

    }

    /**
     * 第三张卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                        String bottleBodyCodeTwo,String bottleCapCodeTwo,
                        String bottleBodyCodeThree,String bottleCapCodeThree) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
    }

    /**
     * 第四张卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     * @param bottleBodyCodeFour
     * @param bottleCapCodeFour
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                     String bottleBodyCodeTwo,String bottleCapCodeTwo,
                     String bottleBodyCodeThree,String bottleCapCodeThree,
                     String bottleBodyCodeFour,String bottleCapCodeFour) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
        this.bottleBodyCodeFour = bottleBodyCodeFour;
        this.bottleCapCodeFour = bottleCapCodeFour;
    }

    /**'
     *   第五张卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     * @param bottleBodyCodeFour
     * @param bottleCapCodeFour
     * @param bottleBodyCodeFive
     * @param bottleCapCodeFive
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                     String bottleBodyCodeTwo,String bottleCapCodeTwo,
                     String bottleBodyCodeThree,String bottleCapCodeThree,
                     String bottleBodyCodeFour,String bottleCapCodeFour,
                     String bottleBodyCodeFive ,String bottleCapCodeFive) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
        this.bottleBodyCodeFour = bottleBodyCodeFour;
        this.bottleCapCodeFour = bottleCapCodeFour;
        this.bottleBodyCodeFive = bottleBodyCodeFive;
        this.bottleCapCodeFive = bottleCapCodeFive;
    }

    /**
     *  第六章卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     * @param bottleBodyCodeFour
     * @param bottleCapCodeFour
     * @param bottleBodyCodeFive
     * @param bottleCapCodeFive
     * @param bottleBodyCodeSix
     * @param bottleCapCodeSix
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                     String bottleBodyCodeTwo,String bottleCapCodeTwo,
                     String bottleBodyCodeThree,String bottleCapCodeThree,
                     String bottleBodyCodeFour,String bottleCapCodeFour,
                     String bottleBodyCodeFive ,String bottleCapCodeFive,
                     String bottleBodyCodeSix,String bottleCapCodeSix) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
        this.bottleBodyCodeFour = bottleBodyCodeFour;
        this.bottleCapCodeFour = bottleCapCodeFour;
        this.bottleBodyCodeFive = bottleBodyCodeFive;
        this.bottleCapCodeFive = bottleCapCodeFive;
        this.bottleBodyCodeSix = bottleBodyCodeSix;
        this.bottleCapCodeSix = bottleCapCodeSix;
    }

    /**
     * 第七章卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     * @param bottleBodyCodeFour
     * @param bottleCapCodeFour
     * @param bottleBodyCodeFive
     * @param bottleCapCodeFive
     * @param bottleBodyCodeSix
     * @param bottleCapCodeSix
     * @param bottleBodyCodeSeven
     * @param bottleCapCodeSeven
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                     String bottleBodyCodeTwo,String bottleCapCodeTwo,
                     String bottleBodyCodeThree,String bottleCapCodeThree,
                     String bottleBodyCodeFour,String bottleCapCodeFour,
                     String bottleBodyCodeFive ,String bottleCapCodeFive,
                     String bottleBodyCodeSix,String bottleCapCodeSix,
                     String bottleBodyCodeSeven,String bottleCapCodeSeven) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
        this.bottleBodyCodeFour = bottleBodyCodeFour;
        this.bottleCapCodeFour = bottleCapCodeFour;
        this.bottleBodyCodeFive = bottleBodyCodeFive;
        this.bottleCapCodeFive = bottleCapCodeFive;
        this.bottleBodyCodeSix = bottleBodyCodeSix;
        this.bottleCapCodeSix = bottleCapCodeSix;
        this.bottleBodyCodeSeven = bottleBodyCodeSeven;
        this.bottleCapCodeSeven = bottleCapCodeSeven;
    }

    /**
     * 第八章卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     * @param bottleBodyCodeFour
     * @param bottleCapCodeFour
     * @param bottleBodyCodeFive
     * @param bottleCapCodeFive
     * @param bottleBodyCodeSix
     * @param bottleCapCodeSix
     * @param bottleBodyCodeSeven
     * @param bottleCapCodeSeven
     * @param bottleBodyCodeEight
     * @param bottleCapCodeEight
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                     String bottleBodyCodeTwo,String bottleCapCodeTwo,
                     String bottleBodyCodeThree,String bottleCapCodeThree,
                     String bottleBodyCodeFour,String bottleCapCodeFour,
                     String bottleBodyCodeFive ,String bottleCapCodeFive,
                     String bottleBodyCodeSix,String bottleCapCodeSix,
                     String bottleBodyCodeSeven,String bottleCapCodeSeven,
                     String bottleBodyCodeEight,String bottleCapCodeEight) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
        this.bottleBodyCodeFour = bottleBodyCodeFour;
        this.bottleCapCodeFour = bottleCapCodeFour;
        this.bottleBodyCodeFive = bottleBodyCodeFive;
        this.bottleCapCodeFive = bottleCapCodeFive;
        this.bottleBodyCodeSix = bottleBodyCodeSix;
        this.bottleCapCodeSix = bottleCapCodeSix;
        this.bottleBodyCodeSeven = bottleBodyCodeSeven;
        this.bottleCapCodeSeven = bottleCapCodeSeven;
        this.bottleBodyCodeEight = bottleBodyCodeEight;
        this.bottleCapCodeEight = bottleCapCodeEight;
    }

    /**
     * 第九张卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     * @param bottleBodyCodeFour
     * @param bottleCapCodeFour
     * @param bottleBodyCodeFive
     * @param bottleCapCodeFive
     * @param bottleBodyCodeSix
     * @param bottleCapCodeSix
     * @param bottleBodyCodeSeven
     * @param bottleCapCodeSeven
     * @param bottleBodyCodeEight
     * @param bottleCapCodeEight
     * @param bottleBodyCodeNine
     * @param bottleCapCodeNine
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                     String bottleBodyCodeTwo,String bottleCapCodeTwo,
                     String bottleBodyCodeThree,String bottleCapCodeThree,
                     String bottleBodyCodeFour,String bottleCapCodeFour,
                     String bottleBodyCodeFive ,String bottleCapCodeFive,
                     String bottleBodyCodeSix,String bottleCapCodeSix,
                     String bottleBodyCodeSeven,String bottleCapCodeSeven,
                     String bottleBodyCodeEight,String bottleCapCodeEight,
                     String bottleBodyCodeNine,String bottleCapCodeNine) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
        this.bottleBodyCodeFour = bottleBodyCodeFour;
        this.bottleCapCodeFour = bottleCapCodeFour;
        this.bottleBodyCodeFive = bottleBodyCodeFive;
        this.bottleCapCodeFive = bottleCapCodeFive;
        this.bottleBodyCodeSix = bottleBodyCodeSix;
        this.bottleCapCodeSix = bottleCapCodeSix;
        this.bottleBodyCodeSeven = bottleBodyCodeSeven;
        this.bottleCapCodeSeven = bottleCapCodeSeven;
        this.bottleBodyCodeEight = bottleBodyCodeEight;
        this.bottleCapCodeEight = bottleCapCodeEight;
        this.bottleBodyCodeNine = bottleBodyCodeNine;
        this.bottleCapCodeNine = bottleCapCodeNine;
    }

    /**
     * 第10章卡片
     * @param bottleBodyCodeOne
     * @param bottleCapCodeOne
     * @param bottleBodyCodeTwo
     * @param bottleCapCodeTwo
     * @param bottleBodyCodeThree
     * @param bottleCapCodeThree
     * @param bottleBodyCodeFour
     * @param bottleCapCodeFour
     * @param bottleBodyCodeFive
     * @param bottleCapCodeFive
     * @param bottleBodyCodeSix
     * @param bottleCapCodeSix
     * @param bottleBodyCodeSeven
     * @param bottleCapCodeSeven
     * @param bottleBodyCodeEight
     * @param bottleCapCodeEight
     * @param bottleBodyCodeNine
     * @param bottleCapCodeNine
     * @param bottleBodyCodeTen
     * @param bottleCapCodeTen
     */
    public void init(String bottleBodyCodeOne,String bottleCapCodeOne,
                     String bottleBodyCodeTwo,String bottleCapCodeTwo,
                     String bottleBodyCodeThree,String bottleCapCodeThree,
                     String bottleBodyCodeFour,String bottleCapCodeFour,
                     String bottleBodyCodeFive ,String bottleCapCodeFive,
                     String bottleBodyCodeSix,String bottleCapCodeSix,
                     String bottleBodyCodeSeven,String bottleCapCodeSeven,
                     String bottleBodyCodeEight,String bottleCapCodeEight,
                     String bottleBodyCodeNine,String bottleCapCodeNine,
                     String bottleBodyCodeTen,String bottleCapCodeTen) {
        this.bottleBodyCodeOne = bottleBodyCodeOne;
        this.bottleCapCodeOne = bottleCapCodeOne;
        this.bottleBodyCodeTwo = bottleBodyCodeTwo;
        this.bottleCapCodeTwo = bottleCapCodeTwo;
        this.bottleBodyCodeThree = bottleBodyCodeThree;
        this.bottleCapCodeThree = bottleCapCodeThree;
        this.bottleBodyCodeFour = bottleBodyCodeFour;
        this.bottleCapCodeFour = bottleCapCodeFour;
        this.bottleBodyCodeFive = bottleBodyCodeFive;
        this.bottleCapCodeFive = bottleCapCodeFive;
        this.bottleBodyCodeSix = bottleBodyCodeSix;
        this.bottleCapCodeSix = bottleCapCodeSix;
        this.bottleBodyCodeSeven = bottleBodyCodeSeven;
        this.bottleCapCodeSeven = bottleCapCodeSeven;
        this.bottleBodyCodeEight = bottleBodyCodeEight;
        this.bottleCapCodeEight = bottleCapCodeEight;
        this.bottleBodyCodeNine = bottleBodyCodeNine;
        this.bottleCapCodeNine = bottleCapCodeNine;
        this.bottleBodyCodeTen = bottleBodyCodeTen;
        this.bottleCapCodeTen = bottleCapCodeTen;
    }



    private void initLayout() {
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(scroll, BorderLayout.CENTER);
        printTextButton.setMnemonic('P');
        printTextButton.addActionListener(this);
        buttonPanel.add(printTextButton);
        previewButton.setMnemonic('v');
        previewButton.addActionListener(this);
        buttonPanel.add(previewButton);
        printText2Button.setMnemonic('e');
        printText2Button.addActionListener(this);
        buttonPanel.add(printText2Button);
        printFileButton.setMnemonic('i');
        printFileButton.addActionListener(this);
        buttonPanel.add(printFileButton);
        printFrameButton.setMnemonic('F');
        printFrameButton.addActionListener(this);
        buttonPanel.add(printFrameButton);
        exitButton.setMnemonic('x');
        exitButton.addActionListener(this);
        buttonPanel.add(exitButton);
        this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent evt) {
        Object src = evt.getSource();
        if (src == printTextButton)
            printTextAction();
        else if (src == previewButton)
            previewAction();
        else if (src == printText2Button)
            printText2Action();
        else if (src == printFileButton)
            printFileAction();
        else if (src == printFrameButton)
            printFrameAction();
        else if (src == exitButton)
            exitApp();
    }

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException
    {
        // 转换成Graphics2D
        Graphics2D g2 = (Graphics2D) g;
        // 设置打印颜色为黑
        g2.setColor(Color.black);

        // 打印起点坐标 对象的可成像区域左上方点的x 坐标 和左上的y坐标
        double x = pf.getImageableX();//返回与此 PageFormat 相关 Paper
        double y = pf.getImageableY();

        switch (page) {
            case 0:
                // 新建 个打印字体样式(1.字体名称�?2.样式(加粗).3字号�?
                Font font = new Font("微软雅黑", Font.PLAIN, 11);
                // 给g2设置字体
                g2.setFont(font);
                // 保存虚线的宽�?
                float[] dash1 = {2.0f};
                // 设置打印线的属�?��??1.线宽 2�?3、不知道�?4、空白的宽度�?5、虚线的宽度�?6、偏移量
                g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
                float fontHeigth1 = font.getSize2D();
              /**第一张卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeOne)){
                    g2.drawString(this.bottleBodyCodeOne, (int) x+2, (int) y + fontHeigth1 + 55);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeOne)){
                    g2.drawString(this.bottleCapCodeOne, (int) x+2, (int) y + fontHeigth1 + 80);
                }

                /**第二张卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeTwo)){
                    g2.drawString(this.bottleBodyCodeTwo, (int) x+2, (int) y + fontHeigth1 + 268);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeTwo)){
                    g2.drawString(this.bottleCapCodeTwo, (int) x+2, (int) y + fontHeigth1 + 293);
                }
                /**第三张卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeThree)){
                    g2.drawString(this.bottleBodyCodeThree, (int) x+2, (int) y + fontHeigth1 + 481);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeThree)){
                    g2.drawString(this.bottleCapCodeThree, (int) x+2, (int) y + fontHeigth1 + 505);
                }
                /**第四卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeFour)){
                    g2.drawString(this.bottleBodyCodeFour, (int) x+2, (int) y + fontHeigth1 + 693);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeFour)){
                    g2.drawString(this.bottleCapCodeFour, (int) x+2, (int) y + fontHeigth1 + 717);
                }
                /**第五卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeFive)){
                    g2.drawString(this.bottleBodyCodeFive, (int) x+2, (int) y + fontHeigth1 + 905);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeFive)){
                    g2.drawString(this.bottleCapCodeFive, (int) x+2, (int) y + fontHeigth1 + 929);
                }
                /**第六卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeSix)){
                    g2.drawString(this.bottleBodyCodeSix, (int) x+2, (int) y + fontHeigth1 + 1117);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeSix)){
                    g2.drawString(this.bottleCapCodeSix, (int) x+2, (int) y + fontHeigth1 + 1141);
                }/**第七张卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeSeven)){
                    g2.drawString(this.bottleBodyCodeSeven, (int) x+2, (int) y + fontHeigth1 + 1329);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeSeven)){
                    g2.drawString(this.bottleCapCodeSeven, (int) x+2, (int) y + fontHeigth1 + 1343);
                }
                /**第八卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeEight)){
                    g2.drawString(this.bottleBodyCodeEight, (int) x+2, (int) y + fontHeigth1 + 1555);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeEight)){
                    g2.drawString(this.bottleCapCodeEight, (int) x+2, (int) y + fontHeigth1 + 1569);
                }
                /**第九卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeNine)){
                    g2.drawString(this.bottleBodyCodeNine, (int) x+2, (int) y + fontHeigth1 + 1757);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeNine)){
                    g2.drawString(this.bottleCapCodeNine, (int) x+2, (int) y + fontHeigth1 + 1771);
                }
                /**第10卡片*/
                if (StringUtils.isNotNull(this.bottleBodyCodeTen)){
                    g2.drawString(this.bottleBodyCodeTen, (int) x+2, (int) y + fontHeigth1 + 1959);
                }
                if (StringUtils.isNotNull(this.bottleCapCodeTen)){
                    g2.drawString(this.bottleCapCodeTen, (int) x+2, (int) y + fontHeigth1 + 1973);
                }
                return PAGE_EXISTS;
            default:
                return NO_SUCH_PAGE;
        }
    }


    public int getPagesCount(String curStr) {
        int page = 0;
        int position, count = 0;
        String str = curStr;
        while (str.length() > 0) {
            position = str.indexOf('\n');
            count += 1;
            if (position != -1)
                str = str.substring(position + 1);
            else
                str = "";
        }

        if (count > 0)
            page = count / 54 + 1;

        return page;
    }

    private void printTextAction() {
        printStr = area.getText().trim();
        if (printStr != null && printStr.length() > 0) {
            PAGES = getPagesCount(printStr);
            PrinterJob myPrtJob = PrinterJob.getPrinterJob();
            PageFormat pageFormat = myPrtJob.defaultPage();
            myPrtJob.setPrintable(this, pageFormat);
            if (myPrtJob.printDialog()) {
                try {
                    myPrtJob.print();
                } catch (PrinterException pe) {
                    pe.printStackTrace();
                }
            }
        } else {
            JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty"
                    , JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
        }
    }

    private void previewAction() {
        printStr = area.getText().trim();
        PAGES = getPagesCount(printStr);
        (new PrintPreviewDialog(this, "Print Preview", true, this, printStr)).setVisible(true);
    }

    private void printText2Action() {
        printStr = area.getText().trim();
        if (printStr != null && printStr.length() > 0) {
            PAGES = getPagesCount(printStr);
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
            DocPrintJob job = printService.createPrintJob();
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            DocAttributeSet das = new HashDocAttributeSet();
            Doc doc = new SimpleDoc(this, flavor, das);

            try {
                job.print(doc, pras);
            } catch (PrintException pe) {
                pe.printStackTrace();
            }
        } else {
            JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty"
                    , JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
        }
    }

    private void printFileAction() {
        JFileChooser fileChooser = new JFileChooser(SystemProperties.USER_DIR);
        int state = fileChooser.showOpenDialog(this);
        if (state == fileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
            PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
            PrintService service = ServiceUI.printDialog(null, 200, 200, printService
                    , defaultService, flavor, pras);
            if (service != null) {
                try {
                    DocPrintJob job = service.createPrintJob();
                    FileInputStream fis = new FileInputStream(file);
                    DocAttributeSet das = new HashDocAttributeSet();
                    Doc doc = new SimpleDoc(fis, flavor, das);
                    job.print(doc, pras);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void printFrameAction() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Properties props = new Properties();
        props.put("awt.print.printer", "durango");
        props.put("awt.print.numCopies", "2");
        if (kit != null) {
            PrintJob printJob = kit.getPrintJob(this, "Print Frame", props);
            if (printJob != null) {
                Graphics pg = printJob.getGraphics();
                if (pg != null) {
                    try {
                        this.printAll(pg);
                    } finally {
                        pg.dispose();
                    }
                }
                printJob.end();
            }
        }
    }

    private void exitApp() {
        this.setVisible(false);
        this.dispose();
        System.exit(0);
    }

    public void printDoc(List<LiqCheck> liqChecks) {

        Book book = new Book();
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT);
        Paper p = new Paper();
        p.setSize(595, 1800);// 纸张大小
        p.setImageableArea(40, 20, 590, 1850);// A4(595 X 842)设置打印区域,其�?0�?0应该�?72�?72,因为A4纸的默认X,Y边距�?72
        pf.setPaper(p);
        PrintHelp printHelp = new PrintHelp();
        printInitHelp(liqChecks,printHelp);
        book.append(printHelp, pf);
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(book);
        try
        {
            job.print();
        }
        catch (PrinterException e)
        {
            throw new RuntimeException(e);
        }
    }

    private void printInitHelp(List<LiqCheck> liqChecks, PrintHelp printHelp)
    {
        if (liqChecks.size() == 1){
            LiqCheck liqCheck = liqChecks.get(0);
            printHelp.init(liqCheck.getLiqBottlebodyCode(),liqCheck.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==2)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==3)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==4)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            LiqCheck liqCheckFour = liqChecks.get(3);

            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode(),
                    liqCheckFour.getLiqBottlebodyCode(),
                    liqCheckFour.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==5)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            LiqCheck liqCheckFour = liqChecks.get(3);
            LiqCheck liqCheckFive = liqChecks.get(4);
            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode(),
                    liqCheckFour.getLiqBottlebodyCode(),
                    liqCheckFour.getLiqBottlemouthCode(),
                    liqCheckFive.getLiqBottlebodyCode(),
                    liqCheckFive.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==6)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            LiqCheck liqCheckFour = liqChecks.get(3);
            LiqCheck liqCheckFive = liqChecks.get(4);
            LiqCheck liqCheckSix = liqChecks.get(5);
            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode(),
                    liqCheckFour.getLiqBottlebodyCode(),
                    liqCheckFour.getLiqBottlemouthCode(),
                    liqCheckFive.getLiqBottlebodyCode(),
                    liqCheckFive.getLiqBottlemouthCode(),
                    liqCheckSix.getLiqBottlebodyCode(),
                    liqCheckSix.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==7)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            LiqCheck liqCheckFour = liqChecks.get(3);
            LiqCheck liqCheckFive = liqChecks.get(4);
            LiqCheck liqCheckSix = liqChecks.get(5);
            LiqCheck liqCheckSeven = liqChecks.get(6);
            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode(),
                    liqCheckFour.getLiqBottlebodyCode(),
                    liqCheckFour.getLiqBottlemouthCode(),
                    liqCheckFive.getLiqBottlebodyCode(),
                    liqCheckFive.getLiqBottlemouthCode(),
                    liqCheckSix.getLiqBottlebodyCode(),
                    liqCheckSix.getLiqBottlemouthCode(),
                    liqCheckSeven.getLiqBottlebodyCode(),
                    liqCheckSeven.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==8)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            LiqCheck liqCheckFour = liqChecks.get(3);
            LiqCheck liqCheckFive = liqChecks.get(4);
            LiqCheck liqCheckSix = liqChecks.get(5);
            LiqCheck liqCheckSeven = liqChecks.get(6);
            LiqCheck liqCheckTEight = liqChecks.get(7);


            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode(),
                    liqCheckFour.getLiqBottlebodyCode(),
                    liqCheckFour.getLiqBottlemouthCode(),
                    liqCheckFive.getLiqBottlebodyCode(),
                    liqCheckFive.getLiqBottlemouthCode(),
                    liqCheckSix.getLiqBottlebodyCode(),
                    liqCheckSix.getLiqBottlemouthCode(),
                    liqCheckSeven.getLiqBottlebodyCode(),
                    liqCheckSeven.getLiqBottlemouthCode(),
                    liqCheckTEight.getLiqBottlebodyCode(),
                    liqCheckTEight.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==9)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            LiqCheck liqCheckFour = liqChecks.get(3);
            LiqCheck liqCheckFive = liqChecks.get(4);
            LiqCheck liqCheckSix = liqChecks.get(5);
            LiqCheck liqCheckSeven = liqChecks.get(6);
            LiqCheck liqCheckTEight = liqChecks.get(7);
            LiqCheck liqCheckNine = liqChecks.get(8);

            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode(),
                    liqCheckFour.getLiqBottlebodyCode(),
                    liqCheckFour.getLiqBottlemouthCode(),
                    liqCheckFive.getLiqBottlebodyCode(),
                    liqCheckFive.getLiqBottlemouthCode(),
                    liqCheckSix.getLiqBottlebodyCode(),
                    liqCheckSix.getLiqBottlemouthCode(),
                    liqCheckSeven.getLiqBottlebodyCode(),
                    liqCheckSeven.getLiqBottlemouthCode(),
                    liqCheckTEight.getLiqBottlebodyCode(),
                    liqCheckTEight.getLiqBottlemouthCode(),
                    liqCheckNine.getLiqBottlebodyCode(),
                    liqCheckNine.getLiqBottlemouthCode());
        }
        if (liqChecks.size() ==10)
        {
            LiqCheck liqCheckOne = liqChecks.get(0);
            LiqCheck liqCheckTwo = liqChecks.get(1);
            LiqCheck liqCheckThree = liqChecks.get(2);
            LiqCheck liqCheckFour = liqChecks.get(3);
            LiqCheck liqCheckFive = liqChecks.get(4);
            LiqCheck liqCheckSix = liqChecks.get(5);
            LiqCheck liqCheckSeven = liqChecks.get(6);
            LiqCheck liqCheckTEight = liqChecks.get(7);
            LiqCheck liqCheckNine = liqChecks.get(8);
            LiqCheck liqCheckTen = liqChecks.get(9);
            printHelp.init(liqCheckOne.getLiqBottlebodyCode(),
                    liqCheckOne.getLiqBottlemouthCode(),
                    liqCheckTwo.getLiqBottlebodyCode(),
                    liqCheckTwo.getLiqBottlemouthCode(),
                    liqCheckThree.getLiqBottlebodyCode(),
                    liqCheckThree.getLiqBottlemouthCode(),
                    liqCheckFour.getLiqBottlebodyCode(),
                    liqCheckFour.getLiqBottlemouthCode(),
                    liqCheckFive.getLiqBottlebodyCode(),
                    liqCheckFive.getLiqBottlemouthCode(),
                    liqCheckSix.getLiqBottlebodyCode(),
                    liqCheckSix.getLiqBottlemouthCode(),
                    liqCheckSeven.getLiqBottlebodyCode(),
                    liqCheckSeven.getLiqBottlemouthCode(),
                    liqCheckTEight.getLiqBottlebodyCode(),
                    liqCheckTEight.getLiqBottlemouthCode(),
                    liqCheckNine.getLiqBottlebodyCode(),
                    liqCheckNine.getLiqBottlemouthCode(),
                    liqCheckTen.getLiqBottlebodyCode(),
                    liqCheckTen.getLiqBottlemouthCode());
        }
    }
}

package com.ydkj.common.print;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

/**
 * @author:myb
 * @description:
 * @date:2023/9/27 14:38
 */
public class PrintPreviewDialog extends JDialog implements ActionListener
{
    private JButton nextButton = new JButton("Next");
    private JButton previousButton = new JButton("Previous");
    private JButton closeButton = new JButton("Close");
    private JPanel buttonPanel = new JPanel();
    private PreviewCanvas canvas;

    public PrintPreviewDialog(Frame parent, String title, boolean modal, PrintHelp pt, String str) {
        super(parent, title, modal);
        canvas = new PreviewCanvas(pt, str);
        setLayout();
    }

    private void setLayout() {
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(canvas, BorderLayout.CENTER);

        nextButton.setMnemonic('N');
        nextButton.addActionListener(this);
        buttonPanel.add(nextButton);
        previousButton.setMnemonic('N');
        previousButton.addActionListener(this);
        buttonPanel.add(previousButton);
        closeButton.setMnemonic('N');
        closeButton.addActionListener(this);
        buttonPanel.add(closeButton);
        this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        this.setBounds((int) ((SystemProperties.SCREEN_WIDTH - 400) / 2), (int) ((SystemProperties.SCREEN_HEIGHT - 400) / 2), 400, 400);
    }

    public void actionPerformed(ActionEvent evt) {
        Object src = evt.getSource();
        if (src == nextButton)
            nextAction();
        else if (src == previousButton)
            previousAction();
        else if (src == closeButton)
            closeAction();
    }

    private void closeAction() {
        this.setVisible(false);
        this.dispose();
    }

    private void nextAction() {
        canvas.viewPage(1);
    }

    private void previousAction() {
        canvas.viewPage(-1);
    }

    class PreviewCanvas extends JPanel
    {
        private String printStr;
        private int currentPage = 0;
        private PrintHelp preview;

        public PreviewCanvas(PrintHelp pt, String str)
        {
            printStr = str;
            preview = pt;
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            PageFormat pf = PrinterJob.getPrinterJob().defaultPage();

            double xoff;
            double yoff;
            double scale;
            double px = pf.getWidth();
            double py = pf.getHeight();
            double sx = getWidth() - 1;
            double sy = getHeight() - 1;
            if (px / py < sx / sy)
            {
                scale = sy / py;
                xoff = 0.5 * (sx - scale * px);
                yoff = 0;
            }
            else
            {
                scale = sx / px;
                xoff = 0;
                yoff = 0.5 * (sy - scale * py);
            }
            g2.translate((float) xoff, (float) yoff);
            g2.scale((float) scale, (float) scale);

            Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);
            g2.setPaint(Color.white);
            g2.fill(page);
            g2.setPaint(Color.black);
            g2.draw(page);

            try
            {
                preview.print(g2, pf, currentPage);
            }
            catch (PrinterException pe)
            {
                g2.draw(new Line2D.Double(0, 0, px, py));
                g2.draw(new Line2D.Double(0, px, 0, py));
            }
        }

        public void viewPage(int pos)
        {
            int newPage = currentPage + pos;
            if (0 <= newPage && newPage < preview.getPagesCount(printStr))
            {
                currentPage = newPage;
                repaint();
            }
        }

    }
}

打印出来结果
打印一张卡片

打印多张图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值