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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.lowagie.text.Font; import com.lowagie.text.pdf.BaseFont; import fr.opensagres.xdocreport.itext.extension.font.IFontProvider; import fr.opensagres.xdocreport.itext.extension.font.ITextFontRegistry; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.util.Units; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.XWPFDocument;
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.*; import java.util.List;
public class Main { public static final int CODEWIDTH = 130; public static int HEIGHT = 80; public static final int CODEHEIGHT = 70; public static int FONTSIZE = 10;
public static void main(String[] args) throws Exception { List<String> list = readXls(); File file = new File("src/main/resources/code.png"); generateCode(file, list.get(2), CODEWIDTH, CODEHEIGHT); generateFont(file, list.get(2)); Map<String, Object> params = new HashMap<String, Object>(); params.put("{name}", list.get(0)); params.put("{sex}", list.get(1)); params.put("{barcode}", list.get(2)); parserDoc(params); writePdf(); System.out.println("generate successfully!"); }
public static List<String> readXls() throws Exception { List<String> list = new ArrayList<>(); InputStream ExcelFileToRead = new FileInputStream("src/main/resources/student.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(ExcelFileToRead); XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.iterator(); while (rows.hasNext()) { XSSFRow row = (XSSFRow) rows.next(); Iterator cells = row.cellIterator(); while (cells.hasNext()) { XSSFCell cell = (XSSFCell) cells.next(); if (cell.getCellTypeEnum() == CellType.STRING) { list.add(cell.getStringCellValue()); } else if (cell.getCellTypeEnum() == CellType.NUMERIC) { list.add("" + cell.getNumericCellValue()); } } } ExcelFileToRead.close(); return list; }
public static void replaceRun(XWPFParagraph xwpfParagraph, Map<String, Object> params) throws Exception { List<XWPFRun> runs = xwpfParagraph.getRuns(); if (runs.size() > 0) { for (XWPFRun re : runs) { if (re.text() != null || re.text().length() > 0) { String match = re.text(); if (params.containsKey(match)) { if (match.equals("{barcode}")) { xwpfParagraph.removeRun(0); XWPFRun run = xwpfParagraph.createRun(); int format = XWPFDocument.PICTURE_TYPE_PNG; run.addPicture(new FileInputStream("src/main/resources/code_new.png"), format, "src/main/resources/code_new.png", Units.toEMU(CODEWIDTH), Units.toEMU(CODEHEIGHT)); } else { xwpfParagraph.removeRun(0); XWPFRun run = xwpfParagraph.createRun(); run.setText(params.get(match).toString()); } } } } } }
public static void parserDoc(Map<String, Object> params) throws Exception { InputStream WordFileToRead = new FileInputStream("src/main/resources/student.docx"); XWPFDocument xwpfDocument = new XWPFDocument(WordFileToRead); List<IBodyElement> iBodyElements = xwpfDocument.getBodyElements(); for (IBodyElement ibs : iBodyElements) { BodyElementType bodyElementType = ibs.getElementType(); if (bodyElementType == BodyElementType.TABLE) { XWPFTable xwpfTable = (XWPFTable) ibs; List<XWPFTableRow> rows = xwpfTable.getRows(); for (XWPFTableRow row : rows) { List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { List<XWPFParagraph> paras = cell.getParagraphs(); for (XWPFParagraph para : paras) { replaceRun(para, params); } } } } else if (bodyElementType == BodyElementType.PARAGRAPH) { XWPFParagraph xwpfParagraph = (XWPFParagraph) ibs; replaceRun(xwpfParagraph, params); } } OutputStream outputStream = new FileOutputStream("src/main/resources/student_new.docx"); xwpfDocument.write(outputStream); WordFileToRead.close(); outputStream.close(); }
public static void writePdf() throws Exception { FileInputStream inputStream = new FileInputStream("src/main/resources/student_new.docx"); XWPFDocument document = new XWPFDocument(inputStream); PdfOptions options = PdfOptions.create(); options.fontProvider(new IFontProvider() { @Override public Font getFont(String familyName, String encoding, float size, int style, Color color) { try { BaseFont bfChinese=BaseFont.createFont("C:/Windows/Fonts/simfang.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED); Font fontChinese = new Font(bfChinese, size, style, color); if (familyName != null) fontChinese.setFamily(familyName); return fontChinese; } catch(Exception e) { e.printStackTrace(); return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color); } } }); PdfConverter.getInstance().convert(document, new FileOutputStream("src/main/resources/student.pdf"), options); inputStream.close(); }
public static void generateCode(File file, String code, int width, int height) { BitMatrix matrix = null; try { MultiFormatWriter writer = new MultiFormatWriter(); matrix = writer.encode(code, BarcodeFormat.CODE_128, width, height, null); } catch (Exception e) { e.printStackTrace(); } try (FileOutputStream outputStream = new FileOutputStream(file)) { ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png", outputStream); outputStream.flush(); } catch (Exception e) { e.printStackTrace(); } }
public static void generateFont(File file, String s) throws Exception { BufferedImage code = ImageIO.read(file); BufferedImage font = new BufferedImage(CODEWIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) font.getGraphics(); g.clearRect(0, 0, CODEWIDTH, HEIGHT); g.setColor(Color.WHITE); g.fillRect(0, 0, CODEWIDTH, HEIGHT); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); for (int i = 0; i < s.length(); i++) { g.setColor(Color.black); java.awt.Font font_ = new java.awt.Font("Consolas", 0, FONTSIZE); g.setFont(font_); g.drawString(s.charAt(i) + "", (FONTSIZE * 4 + CODEWIDTH - s.length() * FONTSIZE) / 2 + (i - 1) * FONTSIZE * 5 / 6, HEIGHT); } g.drawImage(code, 0, 0, null); g.dispose(); File outputfile = new File("src/main/resources/code_new.png"); ImageIO.write(font, "png", outputfile); } }
|