- First add dependencies, the version must be consistent, here is a pit, otherwise the font cannot be recognized
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version>
java code
/** * Export PDF file java project www.fhadmin.org * @param orkQ * @param request * @param response * @return */ @ResponseBody @RequestMapping(value = "exportPdf") public AjaxJson exportPdf(orkQ orkQ, HttpServletRequest request, HttpServletResponse response) throws Exception { AjaxJson j = new AjaxJson(); Page<orkQ> page = orkQService.findPage(new Page<orkQ>(request, response, -1), orkQ); List<orkQ> list = page.getList(); // The first step is to instantiate a document object Document document = new Document(new RectangleReadOnly(842F, 595F)); // The second step is to set the path to out // The second step is to set the path to out //FileOutputStream out = new FileOutputStream("D:/workbook111.pdf"); //If the browser needs to output in the browser through request, use the following method OutputStream out = response.getOutputStream(); // The third step, set the character BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); Font fontZH = new Font(bfChinese, 12.0F, 0); // The fourth step is to output the pdf file to disk PdfWriter writer = PdfWriter.getInstance(document, out); // The fifth step, open the generated pdf file document.open(); // The sixth step, set the content String title = "IT事"; try { Paragraph elements = new Paragraph(title, fontZH); elements.setAlignment(Paragraph.TITLE); document.add(elements); // document.add(new Paragraph(new Chunk(title,fontZH).setLocalDestination(title))); document.add(new Paragraph("\n")); } catch (DocumentException e) { e.printStackTrace (); } // add pictures /*Image image = Image.getInstance("Picture Path"); image.setAlignment(Image.ALIGN_CENTER); image.scalePercent(40); //Scaling according to proportion //image.setAbsolutePosition(40,60); document.add(image);*/ // Create a table. Note that the 3 here means three columns. When adding through table.addCell, you must add all the columns of the entire row. //Create table object PdfPTable table = new PdfPTable(3); PdfPCell cell = new PdfPCell(); Paragraph zhi = new Paragraph("Event Summary",fontZH); cell.setPhrase(zhi); //Cells are aligned horizontally in the center cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph("Contact",fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph("Event Type",fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); for (int i = 0;i<list.size();i++) { zhi = new Paragraph(list.get(i).getItSummary(),fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph(list.get(i).getContacts(),fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); zhi = new Paragraph(list.get(i).getItType(),fontZH); cell.setPhrase(zhi); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); } document.add(table); document.add(new Paragraph("\n")); // The seventh step, close the document document.close(); return j; }
java export pdf function
Guess you like
Origin blog.51cto.com/14622073/2554315
Ranking