1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| public byte[] getPdfByte(OutboundDetailVO detailVO) { String filePath = "output1.pdf"; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Document document = new Document(); PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(filePath))); document.open();
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font font = new Font(baseFont, 9, Font.NORMAL); Font headerFont = new Font(baseFont, 10, Font.BOLD); Font titleFont = new Font(baseFont, 14, Font.BOLD);
Paragraph title = new Paragraph("XXX",titleFont); title.setAlignment(Element.ALIGN_CENTER); document.add(title); document.add(new Paragraph("\n"));
String format = "{}: {}"; String customerName = StrUtil.format(format, "aaa", detailVO.getCustomerName()); String orderNumber = StrUtil.format(format, "bbb", detailVO.getOrdernumber()); String date = StrUtil.format(format, "ccc", DateUtil.format(detailVO.getDate(),"yyyy-MM-dd")); String outboundNumber = StrUtil.format(format, "ddd", detailVO.getWarehouseOutN()); String outboundBy = StrUtil.format(format, "eee", detailVO.getOutstockuname()); String checkBy = StrUtil.format(format, "fff", detailVO.getCheckuname());
PdfPTable customerInfoTable = new PdfPTable(new float[]{4, 2, 2, 3}); customerInfoTable.setWidthPercentage(100); customerInfoTable.addCell(createCell(customerName, font, false, true, true, Element.ALIGN_LEFT)); customerInfoTable.addCell(createCell(orderNumber, font, false, true, true, Element.ALIGN_LEFT)); customerInfoTable.addCell(createCell(date, font, false, true, true, Element.ALIGN_LEFT)); customerInfoTable.addCell(createCell(outboundNumber, font, false, true, true, Element.ALIGN_LEFT)); document.add(customerInfoTable); document.add(new Paragraph("\n"));
PdfPTable table = new PdfPTable(new float[]{2, 2, 2, 2, 2}); table.setWidthPercentage(100); table.addCell(createCell("1", headerFont, true, false, false, Element.ALIGN_CENTER)); table.addCell(createCell("2", headerFont, true, false, false, Element.ALIGN_CENTER)); table.addCell(createCell("3", headerFont, true, false, false, Element.ALIGN_CENTER)); table.addCell(createCell("4", headerFont, true, false, false, Element.ALIGN_CENTER)); table.addCell(createCell("5", headerFont, true, false, false, Element.ALIGN_CENTER));
for (outstockeditspecification one : detailVO.getDetailList()) { table.addCell(createCell(one.getSpecification(), font, false, false, true, Element.ALIGN_CENTER)); table.addCell(createCell(one.getPieces(), font, false, false, true, Element.ALIGN_CENTER)); table.addCell(createCell(one.getLevel(), font, false, false, true, Element.ALIGN_CENTER)); table.addCell(createCell(one.getQuantity(), font, false, false, true, Element.ALIGN_CENTER)); table.addCell(createCell(one.getExplain(), font, false, false, true, Element.ALIGN_CENTER)); }
document.add(table); document.add(new Paragraph("\n"));
PdfPTable footerTable = new PdfPTable(new float[]{1, 1}); footerTable.setWidthPercentage(100); footerTable.addCell(createCell(outboundBy, font, false, true, true, Element.ALIGN_LEFT)); footerTable.addCell(createCell(checkBy, font, false, true, true, Element.ALIGN_RIGHT));
document.add(footerTable);
document.close();
return baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return null; }
private PdfPCell createCell(String content, Font font, boolean isHeader, boolean noBorder, boolean noBackground, int alignment) { PdfPCell cell = new PdfPCell(new Phrase(content, font)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(alignment); cell.setPadding(5);
if (noBorder) { cell.setBorder(Rectangle.NO_BORDER); }
if (isHeader) { cell.setBackgroundColor(new Color(250, 250, 251)); }
return cell; }
|