java套打快遞單

TonySQ發表於2020-03-14

package org.sq.common.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.entity.StringEntity;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.sq.core.tool.utils.OkHttpUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.*;

public class EMSTest  implements Printable{
    
       private String[] value = null;// 所要列印的資料{ "001", "002", "003"};
       private int[][] position = null;// 每個資料在圖片中的座標 { { 10, 50 }, { 30, 70 }, { 50,  90 }};
       
    public static void main(String[] args) throws DocumentException {
       

        printReport();
        
        
        
    }

    @Override
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
        // TODO Auto-generated method stub
        
                if(pageIndex>0)
                {
                    return Printable.NO_SUCH_PAGE;
                }
         Graphics2D g2d = (Graphics2D) g;
           for (int i = 0; i < value.length; i++) {
               String str = value[i];
               int[] temp = position[i];
               int x = temp[0];
               int y = temp[1];
              // 設定列印字型(字型名稱、樣式和點大小)
               Font font = new Font("新宋體", Font.PLAIN, 9);
               g2d.setFont(font); //設定字型
               g2d.drawString(str, x, y);
           }
           return Printable.PAGE_EXISTS;
    }
    
     public String[] getValue() {
        return value;
    }

    public void setValue(String[] value) {
        this.value = value;
    }

    public int[][] getPosition() {
        return position;
    }

    public void setPosition(int[][] position) {
        this.position = position;
    }

    public static void  printReport(){
           PrinterJob pj = PrinterJob.getPrinterJob();//建立一個列印任務
           PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
           Paper paper = pf.getPaper();
           // 設定頁面高和寬,A4紙為595,842
           double pageWidth = 595;
           double pageHeight = 810;
           paper.setSize(pageWidth, pageHeight);
           paper.setImageableArea(0, 0, pageWidth, pageHeight);
           pf.setOrientation(PageFormat.LANDSCAPE); //設定列印方向,LANDSCAPE為橫向,列印方向預設為縱向
           pf.setPaper(paper);
           EMSTest  printTest=new EMSTest();
           printTest.setValue(new String []{"寄件人", "", "張三","寄件地址", "", "山東濟南高新區","收件人", "", "李四","收件地址", "", "山東濟南歷城區" });
           printTest.setPosition(new int [][]{{ 30, 100 }, { 90,100}, { 150,100},{ 30, 130 }, { 90,130}, { 150,130} ,{ 30, 150 }, { 90,150}, { 150,150},{ 30, 170 }, { 90,170}, { 150,170} });
           pj.setPrintable(printTest, pf);
           if (pj.printDialog()) { //彈出列印對話方塊,列印對話方塊,使用者可以通過它改變各種選項,例如:設定列印副本數目,頁面方向,或者目標印表機。
               try {
                    pj.print();
               } catch (PrinterException e) {
                    e.printStackTrace();
               }

           }

        }
}複製程式碼


相關文章