| 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); |
| } |
| } |