JRT打印报告示例

发布时间:2024年01月19日

借助JRT实现的打印客户端和打印元素绘制协议及表格元素,设计器基本成型,这次可以试着写一个用模板控制布局的打印报告示例了,测试点报告表格维护、打印标签、打印数据、打印条码、打印图片、打印表格。基于新架构的代码比M写打印简单多了,容错性更好。

设计器维护的模板
在这里插入图片描述

界面调用
在这里插入图片描述
打印效果
在这里插入图片描述
界面调用代码
在这里插入图片描述

示例代码,底层基础牢固了实现起来就很简单了,这个例子就能很好的代码基于JRT的打印业务实现

import JRT.Core.DataGrid.GridDto;
import JRT.Core.Dto.HashParam;
import JRT.Core.Dto.OutValue;
import JRT.Core.Dto.PrintElement;
import JRT.Core.Util.Convert;
import JRT.Core.Util.PrintDrawProtocol;
import JRT.Model.Entity.JRTPrintTemplate;
import JRT.Model.Entity.JRTPrintTemplateEle;
import JRTBLLBase.BaseHttpHandlerNoSession;
import JRTBLLBase.Helper;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * 调用模板数据来打印,这是一个模拟打印的demo,调用模板数据组装打印数据
 */
public class PrintTemplateTest extends BaseHttpHandlerNoSession {
    /**
     * 按传入的RowID输出符合打印元素绘制协议的数据来实现打印控制
     *
     * @param RowID   业务主键
     * @param P1
     * @param P2
     * @param P3
     * @param P4
     * @param P5
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     */
    public String GetData(String RowID, String P1, String P2, String P3, String P4, String P5, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception {
        List<PrintElement> retList = new ArrayList<>();
        //查询模板数据
        JRTPrintTemplateDto template = GetOneTemplate(null, "JRTReport");
        //存打印的Data数据
        Hashtable<String, String> printDataMap = new Hashtable<>();
        printDataMap.put("姓名", "小乌鱼");
        printDataMap.put("性别", "男");
        printDataMap.put("年龄", "32岁");
        printDataMap.put("登记号", "0947809");
        printDataMap.put("检验号", "24011901");
        printDataMap.put("科室", "体检科");
        printDataMap.put("标本", "全血");
        printDataMap.put("床号", "");
        printDataMap.put("诊断", "体检");
        printDataMap.put("采集日期", "2024-01-19");
        printDataMap.put("采集时间", "16:00");
        printDataMap.put("医嘱备注", "体检");
        printDataMap.put("医嘱名称", "血细胞分析五分类");
        printDataMap.put("申请医生", "乌鱼");
        printDataMap.put("申请日期", "2024-01-19");
        printDataMap.put("操作者", "李四");
        printDataMap.put("审核日期", "2024-01-19");
        printDataMap.put("审核时间", "17:00");
        printDataMap.put("审核者", "张三");

        //存打印的图数据
        Hashtable<String, String> printImgMap = new Hashtable<>();
        printImgMap.put("RPC", "http://localhost:8080/JRTWeb/FileService/zlz/logo.png");

        //存项目结果数据
        List<Object> repResList = new ArrayList<>();
        RepResPrintDto oneRes = new RepResPrintDto();
        oneRes.AbFlag = "*";
        oneRes.RefRange = "12-34um/ml";
        oneRes.Result = "50";
        oneRes.Seq = "1";
        oneRes.TestCodeName = "白细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "*";
        oneRes.RefRange = "12-34um/ml";
        oneRes.Result = "50";
        oneRes.Seq = "2";
        oneRes.TestCodeName = "红细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "";
        oneRes.RefRange = "12-34um/ml";
        oneRes.Result = "33";
        oneRes.Seq = "3";
        oneRes.TestCodeName = "淋巴细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "4";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "5";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "6";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "7";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "8";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);

        //画图片元素
        PrintImage(template, retList, printImgMap);
        //画数据元素
        PrintData(template, retList, printDataMap);
        //画标签元素
        PrintLabel(template, retList);
        //画线元素
        PrintLine(template, retList);
        //画表格元素
        PrintDataGrid(template, retList, repResList);
        return Helper.Object2Json(retList);
    }

    /**
     * 输出报告结果
     *
     * @param template
     * @param template   模板
     * @param retList    元素列表
     * @param repResList 报告结果列表
     */
    private void PrintDataGrid(JRTPrintTemplateDto template, List<PrintElement> retList, List<Object> repResList) throws Exception {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出表格
            if (ele.PrintType.equals("DataGrid")) {
                //得到表格定义
                GridDto defGrid = (GridDto) Helper.Json2Object(ele.PrintText, GridDto.class);
                int lastY = PrintDrawProtocol.DrawGrid(retList, defGrid, null, repResList, "", null);
            }
        }
    }

    /**
     * 输出图片
     *
     * @param template
     * @param template     模板
     * @param retList      元素列表
     * @param printDataMap 数据哈希
     */
    private void PrintImage(JRTPrintTemplateDto template, List<PrintElement> retList, Hashtable<String, String> printDataMap) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出图片
            if (ele.PrintType.equals("Graph")) {
                boolean isSign = false;
                if (ele.PrintFlag.equals("D")) {
                    isSign = true;
                }
                String dataField = ele.DataField;
                if (!ele.DataField.isEmpty() && printDataMap.containsKey(ele.DataField)) {
                    dataField = printDataMap.get(ele.DataField);
                }
                PrintElement printEle = PrintDrawProtocol.DrawGraph(ele.PrintX, ele.PrintY, ele.PrintWidth, ele.PrintHeight, dataField, isSign, false);
                retList.add(printEle);
            }
        }
    }

    /**
     * 输出数据
     *
     * @param template     模板
     * @param retList      元素列表
     * @param printDataMap 数据哈希
     */
    private void PrintData(JRTPrintTemplateDto template, List<PrintElement> retList, Hashtable<String, String> printDataMap) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出标签
            if (ele.PrintType.equals("Data")) {
                String dataFiled = ele.DataField;
                if (!dataFiled.isEmpty() && printDataMap.containsKey(dataFiled)) {
                    //带字的条码
                    if (ele.PrintFlag.contains("BarCode")) {
                        String BarType = ele.PrintFlag.replace("BarCode", "");
                        PrintElement printEle = PrintDrawProtocol.DrawBarCode(ele.PrintX, ele.PrintY, printDataMap.get(dataFiled), true, ele.PrintWidth, ele.PrintHeight, BarType, ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, ele.Angle);
                        retList.add(printEle);
                    }
                    //不带字的条码
                    else if (ele.PrintFlag.contains("BarCodeN")) {
                        String BarType = ele.PrintFlag.replace("BarCodeN", "");
                        PrintElement printEle = PrintDrawProtocol.DrawBarCode(ele.PrintX, ele.PrintY, printDataMap.get(dataFiled), false, ele.PrintWidth, ele.PrintHeight, BarType, ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, ele.Angle);
                        retList.add(printEle);
                    }
                    //画普通数据
                    else {
                        PrintElement printEle = PrintDrawProtocol.DrawLabel(ele.PrintX, ele.PrintY, printDataMap.get(dataFiled), ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, "", ele.Angle);
                        retList.add(printEle);
                    }
                }
            }
        }
    }

    /**
     * 输出标签
     *
     * @param template
     * @param retList
     */
    private void PrintLabel(JRTPrintTemplateDto template, List<PrintElement> retList) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出标签
            if (ele.PrintType.equals("Label")) {
                PrintElement printEle = PrintDrawProtocol.DrawLabel(ele.PrintX, ele.PrintY, ele.PrintText, ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, "", ele.Angle);
                retList.add(printEle);
            }
        }
    }

    /**
     * 输出线
     *
     * @param template
     * @param retList
     */
    private void PrintLine(JRTPrintTemplateDto template, List<PrintElement> retList) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出先
            if (ele.PrintType.equals("ILineN")) {
                int width = ele.PrintWidth;
                int height = ele.PrintHeight;
                int endX = ele.PrintX + width;
                int endY = ele.PrintY;
                int lineWidth = height;
                if (height > width) {
                    endX = ele.PrintX;
                    endY = ele.PrintY;
                    lineWidth = width;
                }
                PrintElement printEle = PrintDrawProtocol.DrawLine(ele.PrintX, ele.PrintY, endX, endY, lineWidth, ele.PrintFlag, ele.Color);
                retList.add(printEle);
            }
        }
    }

    /**
     * 通过模板主键或者代码得到模板数据供打印使用
     *
     * @param rowID
     * @param code
     * @return
     */
    private JRTPrintTemplateDto GetOneTemplate(Integer rowID, String code) throws Exception {
        JRTPrintTemplateDto retDto = null;
        //按主键取
        if (rowID != null) {
            retDto = EntityManager().GetById(JRTPrintTemplateDto.class, rowID);
        }
        //按代码取
        else if (code != null && !code.isEmpty()) {
            retDto = EntityManager().GetByColVal(JRTPrintTemplateDto.class, "Code", code);
        }
        if (retDto != null) {
            HashParam hs = new HashParam();
            hs.Add("PrintTemplateDR", retDto.RowID);
            retDto.EleList = EntityManager().FindAllSimple(JRTPrintTemplateEle.class, hs);
        }
        return retDto;
    }

    /**
     * 存获得的一个模板数据
     */
    public static class JRTPrintTemplateDto extends JRTPrintTemplate {
        /**
         * 模板元素
         */
        public List<JRTPrintTemplateEle> EleList;
    }

    /**
     * 打印的报告结果实体
     */
    public static class RepResPrintDto {
        /**
         * 序号
         */
        public String Seq;

        /**
         * 项目名称
         */
        public String TestCodeName;

        /**
         * 结果
         */
        public String Result;

        /**
         * 标志
         */
        public String AbFlag;

        /**
         * 参考范围
         */
        public String RefRange;
    }
}

后面会进行更细致的打磨,包括集中打印等测试示例实现

文章来源:https://blog.csdn.net/zhanglianzhu_91/article/details/135703404
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。