|
@@ -0,0 +1,233 @@
|
|
|
+package com.kmall.admin.utils.pdf.type;
|
|
|
+
|
|
|
+import com.kmall.api.entity.exportpdf.PDFGoodsDto;
|
|
|
+import com.lowagie.text.*;
|
|
|
+import com.lowagie.text.pdf.*;
|
|
|
+
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author zhangchuangbiao
|
|
|
+ * @version 1.0
|
|
|
+ * 2020-09-11 09:51
|
|
|
+ */
|
|
|
+public class PDFWrite_36 {
|
|
|
+
|
|
|
+
|
|
|
+ public void write(Document doc, PdfWriter writer, PDFGoodsDto pdfGoodsDto
|
|
|
+ , String fontUrl, String uploadDir) throws IOException, DocumentException {
|
|
|
+
|
|
|
+ // 设置字体
|
|
|
+ BaseFont bfHei = BaseFont.createFont(fontUrl,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
|
|
|
+ Font FontChinese11 = new Font(bfHei, 11);
|
|
|
+
|
|
|
+ String path = uploadDir;
|
|
|
+ FileOutputStream out = new FileOutputStream(path);
|
|
|
+
|
|
|
+ doc.setMargins(0,0,0,0);
|
|
|
+
|
|
|
+ writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);//版本(默认1.4)
|
|
|
+ doc.open();// 打开文档
|
|
|
+
|
|
|
+ // -----------------------标签头----------------------
|
|
|
+ getPdfPTable(FontChinese11, bfHei, doc,pdfGoodsDto);
|
|
|
+ getPdfPTable(FontChinese11, bfHei, doc,pdfGoodsDto);
|
|
|
+ getPdfPTable(FontChinese11, bfHei, doc,pdfGoodsDto);
|
|
|
+ getPdfPTable(FontChinese11, bfHei, doc,pdfGoodsDto);
|
|
|
+ getPdfPTable(FontChinese11, bfHei, doc,pdfGoodsDto);
|
|
|
+ getPdfPTable(FontChinese11, bfHei, doc,pdfGoodsDto);
|
|
|
+
|
|
|
+
|
|
|
+ doc.close(); //记得关闭document
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void getPdfPTable(Font fontChinese11, BaseFont bfHei, Document doc, PDFGoodsDto pdfGoodsDto) throws IOException, DocumentException {
|
|
|
+ PdfPTable table = new PdfPTable(3);
|
|
|
+ table = new PdfPTable(6);
|
|
|
+
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ Image img = Image.getInstance("H:\\Demo\\cheaper.jpg");
|
|
|
+ img.setAlignment(Image.ALIGN_CENTER); //设置图片居中
|
|
|
+
|
|
|
+ PdfPCell p = new PdfPCell(img,true);
|
|
|
+ p.setPaddingLeft(2);
|
|
|
+ p.setPaddingRight(2);
|
|
|
+ p.setPaddingTop(2);
|
|
|
+ p.setBorder(0);
|
|
|
+ p.setCellEvent((cell,position,canvases)->{
|
|
|
+ PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
|
|
|
+ cellRight(position, canvas);
|
|
|
+ canvas.moveTo(position.getLeft(), position.getTop());
|
|
|
+ canvas.lineTo(position.getRight(), position.getTop());
|
|
|
+ });
|
|
|
+
|
|
|
+ table.setWidthPercentage(100f);
|
|
|
+ table.addCell(p);
|
|
|
+ }
|
|
|
+ doc.add(table);
|
|
|
+
|
|
|
+
|
|
|
+ // --------------产品名称-------------------------------
|
|
|
+ table = new PdfPTable(6);
|
|
|
+
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ setTable(bfHei, table, 11, pdfGoodsDto.getProductName());
|
|
|
+ }
|
|
|
+
|
|
|
+ doc.add(table);
|
|
|
+
|
|
|
+
|
|
|
+ Paragraph blankRow31 = new Paragraph(18f, " ", fontChinese11);
|
|
|
+ PdfPCell nextLine = new PdfPCell(blankRow31);
|
|
|
+ nextLine.setBorder(0);
|
|
|
+ nextLine.setCellEvent((cell,position,canvases)->{
|
|
|
+ PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
|
|
|
+ cellRight(position, canvas);
|
|
|
+ });
|
|
|
+// doc.add(nextLine);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // --------------产品英文名称-------------------------------
|
|
|
+ table = new PdfPTable(6);
|
|
|
+
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ setTable(bfHei, table, 7, pdfGoodsDto.getEnglishName());
|
|
|
+ }
|
|
|
+ doc.add(table);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ table = new PdfPTable(6);
|
|
|
+
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ PdfPTable pdfPTable4 = new PdfPTable(2);
|
|
|
+ pdfPTable4.setWidths(new int[]{10,6});
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // --------------大价格-------------------------------
|
|
|
+ Font font = new Font(bfHei, 22);
|
|
|
+ Paragraph bigPrice = new Paragraph("¥"+pdfGoodsDto.getPrice().substring(0,pdfGoodsDto.getPrice().indexOf(".")), font);
|
|
|
+ PdfPCell priceCell = new PdfPCell(bigPrice);
|
|
|
+ setBasisStyle(priceCell, Element.ALIGN_RIGHT, Element.ALIGN_CENTER);
|
|
|
+ pdfPTable4.addCell(priceCell);
|
|
|
+
|
|
|
+ // --------------小价格-------------------------------
|
|
|
+ font = new Font(bfHei, 14);
|
|
|
+ Paragraph smallPrice = new Paragraph(pdfGoodsDto.getPrice().substring(pdfGoodsDto.getPrice().indexOf(".")), font);
|
|
|
+ PdfPCell smallPriceCell = new PdfPCell(smallPrice);
|
|
|
+ setBasisStyle(smallPriceCell, Element.ALIGN_LEFT, Element.ALIGN_CENTER);
|
|
|
+ pdfPTable4.addCell(smallPriceCell);
|
|
|
+
|
|
|
+ PdfPCell p = new PdfPCell(pdfPTable4);
|
|
|
+ p.setBorder(0);
|
|
|
+ p.setCellEvent((cell,position,canvases)->{
|
|
|
+ PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
|
|
|
+ cellRight(position, canvas);
|
|
|
+ });
|
|
|
+ table.setWidthPercentage(100f);
|
|
|
+ table.addCell(p);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ doc.add(table);
|
|
|
+
|
|
|
+
|
|
|
+ // --------------空格--------------
|
|
|
+
|
|
|
+// doc.add(nextLine);
|
|
|
+
|
|
|
+ // --------------底部1-------------------------------
|
|
|
+ table = new PdfPTable(6);
|
|
|
+
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ setTable(bfHei, table, 8, "比建议零售价省¥"+pdfGoodsDto.getSavings());
|
|
|
+ }
|
|
|
+
|
|
|
+ doc.add(table);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // --------------底部2-------------------------------
|
|
|
+ table = new PdfPTable(6);
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ setTable(bfHei, table, 6, "税率:"+pdfGoodsDto.getStartFax());
|
|
|
+ }
|
|
|
+ doc.add(table);
|
|
|
+
|
|
|
+
|
|
|
+ table = new PdfPTable(6);
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ // --------------底部左边PLU-------------------------------
|
|
|
+ PdfPTable pdfPTable7 = new PdfPTable(2);
|
|
|
+ Font font = new Font(bfHei, 5);
|
|
|
+ Paragraph PLU = new Paragraph(pdfGoodsDto.getPlu(), font);
|
|
|
+ PdfPCell pluCell = new PdfPCell(PLU);
|
|
|
+ setBasisStyle(pluCell, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE);
|
|
|
+ pluCell.setPaddingLeft(10);
|
|
|
+ // --------------底部右边条形码-------------------------------
|
|
|
+ Paragraph barCode = new Paragraph(pdfGoodsDto.getBarCode(), font);
|
|
|
+ PdfPCell barCodeCell = new PdfPCell(barCode);
|
|
|
+ setBasisStyle(barCodeCell, Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE);
|
|
|
+ barCodeCell.setPaddingRight(10);
|
|
|
+ pdfPTable7.addCell(pluCell);
|
|
|
+ pdfPTable7.addCell(barCodeCell);
|
|
|
+ PdfPCell p = new PdfPCell(pdfPTable7);
|
|
|
+ p.setBorder(0);
|
|
|
+ p.setCellEvent((cell,position,canvases)->{
|
|
|
+ PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
|
|
|
+ cellRight(position, canvas);
|
|
|
+ });
|
|
|
+ table.setWidthPercentage(100f);
|
|
|
+ table.addCell(p);
|
|
|
+ }
|
|
|
+ doc.add(table);
|
|
|
+
|
|
|
+
|
|
|
+// doc.add(nextLine);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 绘制右边边框虚线
|
|
|
+ private static void cellRight(Rectangle position, PdfContentByte canvas) {
|
|
|
+ canvas.saveState();
|
|
|
+ canvas.setLineWidth(0.5f);
|
|
|
+ canvas.setLineDash(new float[]{5.0f, 5.0f}, 0);
|
|
|
+ canvas.moveTo(position.getRight(), position.getTop());
|
|
|
+ canvas.lineTo(position.getRight(), position.getBottom());
|
|
|
+ canvas.stroke();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 去除边框,设置水平垂直居中
|
|
|
+ private static void setBasisStyle(PdfPCell pluCell, int alignLeft, int alignMiddle) {
|
|
|
+ pluCell.setBorder(0);
|
|
|
+ pluCell.setHorizontalAlignment(alignLeft); // 水平居中
|
|
|
+ pluCell.setVerticalAlignment(alignMiddle); // 垂直居中
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void setTable(BaseFont bfHei, PdfPTable table, int i2, String s) {
|
|
|
+ Font font = new Font(bfHei, i2); // 设置字体
|
|
|
+ PdfPTable pdfPTable3 = new PdfPTable(1);
|
|
|
+ Paragraph pararaph = new Paragraph(s, font); // 段落
|
|
|
+ PdfPCell nameCell = new PdfPCell(pararaph); // 单元Cell
|
|
|
+ setBasisStyle(nameCell, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE);
|
|
|
+ pdfPTable3.addCell(nameCell);
|
|
|
+
|
|
|
+ // 创建PCell来存储PTable,用来去除边框
|
|
|
+ PdfPCell p = new PdfPCell(pdfPTable3);
|
|
|
+ p.setBorder(0);
|
|
|
+ // 设置虚线边框
|
|
|
+ p.setCellEvent((cell, position, canvases) -> {
|
|
|
+ PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
|
|
|
+ cellRight(position, canvas);
|
|
|
+ });
|
|
|
+ // 填充满
|
|
|
+ table.setWidthPercentage(100f);
|
|
|
+ table.addCell(p);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|