⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printutil.java

📁 mywork是rcp开发的很好的例子
💻 JAVA
字号:
package net.sf.pim;

import java.util.ArrayList;

import net.sf.component.config.ConfigHelper;
import net.sf.pim.model.psp.Work;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.printing.Printer;

public class PrintUtil {

	/**
	 * 打印功能
	 * 分页,单元格内分行
	 */
	public static void printWork(Work[] list) {
	    if (list == null && list.length == 0)
	        return;
	    //标题
	    String bt[] = UiUtil.getHeaderMc();
	    //标题大小比例
	    int headSize[] = ConfigHelper.getIntegerArrayProperty("work.headersize");
	
	    Printer print = new Printer();
	    print.startJob("MyWork");
	
	    int mTop = 10;
	    int mBottom = 10;
	    int mLeft = 10;
	    int mRight = 10;
	
	    Rectangle rt = print.getClientArea();
	    GC gc = new GC(print);
	    Font font = new Font(print, "宋体", 10, SWT.NORMAL);
	    gc.setFont(font);
	    gc.setBackground(print.getSystemColor(SWT.COLOR_WHITE));
	
	    //常数获取
	    int x = rt.x + mLeft;
	    int y = rt.y + mTop;
	    int width = rt.width - mLeft - mRight;
	    int height = rt.height - mTop - mBottom;
	
	    FontMetrics fm = gc.getFontMetrics();
	    int charWidth = fm.getAverageCharWidth();
	    int charHeight = fm.getHeight();
	    int lineLeading = charHeight / 5;
	    int lineHeight = charHeight + lineLeading * 2;
	
	
	    //总大小
	    int sizeTotal = 0;
	    for (int i = 0; i < headSize.length; i++)
	        sizeTotal += headSize[i];
	
	    //各标题宽度
	    int xWidth[] = new int[headSize.length];
	    for (int i = 0; i < xWidth.length; i++) {
	        xWidth[i] = headSize[i] * 10000 / sizeTotal * width / 10000;
	    }
	    //校准各标题宽度
	    int delta = 0;
	    //日期
	    if (xWidth[0] < charWidth * 8 + lineLeading * 2) {
	        delta += charWidth * 8 + lineLeading * 2 - xWidth[0];
	        xWidth[0] = charWidth * 8 + lineLeading * 2;
	    }
	    //开始时间和结束时间、打断时间、工作时间
	    if (xWidth[2] < charWidth * 8 + lineLeading * 2) {
	        delta += 2 * (charWidth * 8 + lineLeading * 2 - xWidth[2]);
	        xWidth[2] = charWidth * 8 + lineLeading * 2;
	        xWidth[3] = xWidth[2];
	        xWidth[4] = xWidth[2];
	        xWidth[5] = xWidth[2];
	    }
	    //备注
	    if (xWidth[6] < charWidth * 16 + lineLeading * 2) {
	        delta += charWidth * 16 + lineLeading * 2 - xWidth[6];
	        xWidth[6] = charWidth * 16 + lineLeading * 2;
	    }
	
	    //项目组
	    if (xWidth[9] < charWidth * 12 + lineLeading * 2) {
	        delta += charWidth * 12 + lineLeading * 2 - xWidth[9];
	        xWidth[9] = charWidth * 12 + lineLeading * 2;
	    }
	    //工作说明
	    xWidth[8] = xWidth[8] - delta;
	
	    //校准width
	    width = 0;
	    for (int i = 0; i < xWidth.length; i++)
	        width += xWidth[i];
	
	    //各标题起始x坐标
	    int xPos[] = new int[headSize.length + 1];
	    xPos[0] = x;
	    for (int i = 1; i < xPos.length; i++) {
	        xPos[i] = xPos[i - 1] + xWidth[i - 1];
	    }
	
	    //每单元格工作说明的字符
	    int charPerCell4 = xWidth[4] / charWidth / 2 * 2;	//取偶数
	
	    //每页的行数,除标题行
	    int rowPerPage = height / (charHeight + lineLeading * 2) - 1;
	
	    //重整内容,进行分行处理
	    ArrayList al = new ArrayList();
	    for (int i = 0; i < list.length; i++) {
	        String work[] = list[i].getContent();
	        //work[4]为工作说明
	        byte schar[] = work[4].getBytes();
	        int line = schar.length / charPerCell4 + 1;
	        //最后一字是否为中文的补偿,[补偿后不能大于行数,要大于行数就出错了]
	        int flag = 0;
	        for (int k = 0; k < line; k++) {
	            int len = Math.min(charPerCell4, schar.length - k * charPerCell4 + flag);
	            byte dchar[] = new byte[len];
	            System.arraycopy(schar, k * charPerCell4 - flag, dchar, 0, len);
	
	            String str = new String(dchar);
	            //断行不正确
	            if (str.length() == 0) {
	                dchar = new byte[len - 1];
	                System.arraycopy(schar, k * charPerCell4 - flag, dchar, 0, len - 1);
	                str = new String(dchar);
	                flag++;
	            }
	
	            String[] newwork = new String[work.length];
	            if (k == 0) {
	                System.arraycopy(work, 0, newwork, 0, work.length);
	                newwork[4] = str;
	                al.add(newwork);
	            }
	            else {
	                newwork[4] = str;
	                al.add(newwork);
	            }
	        }
	    }
	    int mod = 0;
	    for (int i = 0; i < al.size(); i++) {
	        String work[] = (String[]) al.get(i);
	        mod = i % rowPerPage;
	        if (mod == 0) {
	            //换页
	            if (i != 0) {
	                //画横线
	                gc.drawLine(x, y, x + width, y);
	                for (int j = 0; j < rowPerPage; j++)
	                    if (((String[]) al.get(i - rowPerPage + j))[0] != null)
	                        gc.drawLine(x, y + lineHeight * (j + 1), x + width, y + lineHeight * (j + 1));
	                gc.drawLine(x, y + lineHeight * (rowPerPage + 1), x + width, y + lineHeight * (rowPerPage + 1));
	                //竖线
	                for (int j = 0; j < xPos.length; j++)
	                    gc.drawLine(xPos[j], y, xPos[j], y + lineHeight * (rowPerPage + 1));
	                //页码
	                gc.drawString("- " + String.valueOf(i / rowPerPage) + " -", x + width / 2 - charWidth * 2, y + lineHeight * (rowPerPage + 1));
	                print.endPage();
	
	            }
	
	            //新页
	            print.startPage();
	            //标题
	            for (int j = 0; j < bt.length; j++)
	                gc.drawString(bt[j], xPos[j] + lineLeading, y + lineLeading);
	        }
	
	        for (int j = 0; j < work.length; j++)
	            if (work[j] != null)
	                gc.drawString(work[j], xPos[j] + lineLeading, y + lineHeight * (mod + 1) + lineLeading);
	
	    }
	
	    //最后一页
	    //画横线
	    gc.drawLine(x, y, x + width, y);
	    for (int j = 0; j <= mod; j++)
	        if (((String[]) al.get(al.size() - 1 - mod + j))[0] != null)
	            gc.drawLine(x, y + lineHeight * (j + 1), x + width, y + lineHeight * (j + 1));
	    gc.drawLine(x, y + lineHeight * (mod + 2), x + width, y + lineHeight * (mod + 2));
	
	    //竖线
	    for (int j = 0; j < xPos.length; j++)
	        gc.drawLine(xPos[j], y, xPos[j], y + lineHeight * (mod + 2));
	    //页码
	    gc.drawString("- " + String.valueOf(al.size() / rowPerPage + 1) + " -", x + width / 2 - charWidth * 2, y + lineHeight * (rowPerPage + 1));
	
	    print.endPage();
	
	    print.endJob();
	
	    gc.dispose();
	    print.dispose();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -