程序员社区

java 导出pdf 功能

  • 首先添加依赖,版本要一致,这里是个坑,不然会出现字体无法识别的情况
<!-- 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代码

/**
	 * 导出PDF文件 java项目 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();
		// 第一步,实例化一个document对象
		Document document = new Document(new RectangleReadOnly(842F, 595F));
		// 第二步,设置要到出的路径
		// 第二步,设置要到出的路径
		//FileOutputStream out = new FileOutputStream("D:/workbook111.pdf");
		//如果是浏览器通过request请求需要在浏览器中输出则使用下面方式
		OutputStream out = response.getOutputStream();
		// 第三步,设置字符
		BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
		Font fontZH = new Font(bfChinese, 12.0F, 0);
		// 第四步,将pdf文件输出到磁盘
		PdfWriter writer = PdfWriter.getInstance(document, out);
		// 第五步,打开生成的pdf文件
		document.open();
		// 第六步,设置内容
		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();
		}
		// 添加图片
 
		/*Image image = Image.getInstance("图片路径");
		image.setAlignment(Image.ALIGN_CENTER);
		image.scalePercent(40); //依照比例缩放
		//image.setAbsolutePosition(40,60);
		document.add(image);*/
		// 创建table,注意这里的3是三列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列
		//创建表格对象
		PdfPTable table = new PdfPTable(3);
		PdfPCell cell = new PdfPCell();
		Paragraph zhi = new Paragraph("事件概要",fontZH);
		cell.setPhrase(zhi);
		//单元格水平居中对齐
		cell.setUseAscender(true);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
 
		zhi = new Paragraph("联系人",fontZH);
		cell.setPhrase(zhi);
		cell.setUseAscender(true);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
 
		zhi = new Paragraph("事件类型",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"));
		// 第七步,关闭document
		document.close();
 
 
	
		return j;
}

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

赞(0) 打赏
未经允许不得转载:IDEA激活码 » java 导出pdf 功能

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区