zoukankan      html  css  js  c++  java
  • Java绘出pdf实现方法

      public InputStream generateLabel(List<Map<String, Object>> ordersList) throws Exception {
    
            Document document = getDocument();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, out);
            document.open();
            createPdfBody(document, writer, ordersList);
            document.close();
            return new ByteArrayInputStream(out.toByteArray());
        }
    
        protected void createPdfBody(Document document, PdfWriter writer, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            oneRow(table, ordersList);
            twoRow(table, ordersList);
            threeRow(table, ordersList);
            fourRow(table, ordersList);
            fiveRow(table, ordersList);
            sixRow(table, ordersList);
            sevenRow(table, ordersList);
            eightRow(table, ordersList);
            document.add(table);
        }
    
        private void oneRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(2);
            nestedTable.setWidths(new float[] { 0.4f, 0.6f });
    
            PdfPCell nestedCell1 = new PdfPCell(new Phrase("订单号:", smallFont));
            nestedCell1.setFixedHeight(25);
            nestedCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell nestedCell2 = new PdfPCell();
            nestedCell2.setFixedHeight(25);
            nestedCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            String orderNum = null;
            for (Map map : ordersList) {
                orderNum = (String) map.get("orderNum");
            }
            nestedCell2.addElement(new Phrase(orderNum, smallFont));
            nestedTable.addCell(nestedCell2);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(25);
            table.addCell(cell);
        }
    
        private void twoRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(2);
            nestedTable.setWidths(new float[] { 0.4f, 0.6f });
    
            PdfPCell nestedCell1 = new PdfPCell(new Phrase("寄件人:", smallFont));
            nestedCell1.setFixedHeight(25);
            nestedCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell nestedCell2 = new PdfPCell();
            nestedCell2.setFixedHeight(25);
            nestedCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            String senderName = null;
            for (Map map : ordersList) {
                senderName = (String) map.get("senderName");
            }
            nestedCell2.addElement(new Phrase(senderName, smallFont));
            nestedTable.addCell(nestedCell2);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(25);
            table.addCell(cell);
        }
    
        private void threeRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(2);
            nestedTable.setWidths(new float[] { 0.4f, 0.6f });
    
            PdfPCell nestedCell1 = new PdfPCell(new Phrase("收件人:", smallFont));
            nestedCell1.setFixedHeight(25);
            nestedCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell nestedCell2 = new PdfPCell();
            nestedCell2.setFixedHeight(25);
            nestedCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            String receiverName = null;
            for (Map map : ordersList) {
                receiverName = (String) map.get("receiverName");
            }
            nestedCell2.addElement(new Phrase(receiverName, smallFont));
            nestedTable.addCell(nestedCell2);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(25);
            table.addCell(cell);
        }
    
        private void fourRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(2);
            nestedTable.setWidths(new float[] { 0.4f, 0.6f });
    
            PdfPCell nestedCell1 = new PdfPCell(new Phrase("行李件数:", smallFont));
            nestedCell1.setFixedHeight(25);
            nestedCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell nestedCell2 = new PdfPCell();
            nestedCell2.setFixedHeight(25);
            nestedCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            String packagesNum = null;
            for (Map map : ordersList) {
                packagesNum = String.valueOf(map.get("packagesNum"));
            }
            nestedCell2.addElement(new Phrase(packagesNum, smallFont));
            nestedTable.addCell(nestedCell2);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(25);
            table.addCell(cell);
        }
    
        private void fiveRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(2);
            nestedTable.setWidths(new float[] { 0.4f, 0.6f });
    
            PdfPCell nestedCell1 = new PdfPCell(new Phrase("行李位置:", smallFont));
            nestedCell1.setFixedHeight(25);
            nestedCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell nestedCell2 = new PdfPCell();
            nestedCell2.setFixedHeight(25);
            nestedCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            String shelvesnum = null;
            for (Map map : ordersList) {
                shelvesnum = (String) map.get("shelvesnum");
            }
            nestedCell2.addElement(new Phrase(shelvesnum, smallFont));
            nestedTable.addCell(nestedCell2);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(25);
            table.addCell(cell);
        }
    
        private void sixRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(1);
            nestedTable.setWidths(new float[] { 1f });
            PdfPCell nestedCell1 = new PdfPCell(new Phrase("境内运单号与国际运单号对应关系:", smallFont));
            nestedCell1.setFixedHeight(25);
            nestedCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(25);
            table.addCell(cell);
        }
    
        private void sevenRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(1);
            nestedTable.setWidths(new float[] { 1f });
    
            PdfPCell nestedCell1 = new PdfPCell(new Phrase("境内运单号   - - - - - - 国际运单号", smallFont));
            nestedCell1.setFixedHeight(25);
            nestedCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(25);
            table.addCell(cell);
        }
    
        private void eightRow(PdfPTable table, List<Map<String, Object>> ordersList) throws Exception {
            PdfPTable nestedTable = new PdfPTable(1);
            nestedTable.setWidths(new float[] { 1f });
    
            List<Map<String, String>> uscnList = null;
            String trackNumUsCnStr = "";
            for (int n = 0; n < ordersList.size(); n++) {
                uscnList = (List<Map<String, String>>) ordersList.get(n).get("trackNumUsCnList");
                if (uscnList.size() > 0) {
                    for (Map uscnMap : uscnList) {
                        if (uscnMap.get("trackNumUs") == null) {
                            trackNumUsCnStr = trackNumUsCnStr + uscnMap.get("trackNumUs") + " - - - - - - " + " " + "
    ";
                        } else {
                            trackNumUsCnStr = trackNumUsCnStr + uscnMap.get("trackNumUs") + " - - - - - - " + uscnMap.get("trackNumCn") + "
    ";
                        }
                    }
                }
    
            }
    
            PdfPCell nestedCell1 = new PdfPCell(new Phrase(trackNumUsCnStr, smallFont));
            nestedCell1.setFixedHeight(10);
            nestedCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            nestedTable.addCell(nestedCell1);
    
            PdfPCell cell = new PdfPCell(nestedTable);
            cell.setFixedHeight(245);
            table.addCell(cell);
        }
    

      

  • 相关阅读:
    PAT L2-014【二分】
    CodeForces 137C【贪心+优先队列】
    CodeForces 131D【图特性+BFS】
    CodeForces 125D【鸽巢原理】
    PAT1060【大模拟啊】
    CodeForces 124C【连通块】
    PAT 1071【STL string应用】
    CodeForces 116C 【BFS】
    CodeForces 116B【二分匹配】
    CodeForces 118C 【模拟】
  • 原文地址:https://www.cnblogs.com/dreammyone/p/7418646.html
Copyright © 2011-2022 走看看