生成pdf常用的插件有iReport、和itext,这里将使用itext生成pdf文件。
多于的话不说直接上demo和需要的jar,如果pdf中有图片要画的话可以用jfreeChart画。
package com.pdf; import java.awt.Color; import java.io.File; import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class PDFReport1 { int maxWidth = 520; private static Font keyfont; // 设置字体大小 private static Font textfont; // 设置字体大小 // 建立一个Document对象 Document document = new Document(); static { BaseFont bfChinese; try { bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); keyfont = new Font(bfChinese, 8, Font.BOLD); // 设置字体大小 textfont = new Font(bfChinese, 8, Font.NORMAL); // 设置字体大小 } catch (Exception e) { e.printStackTrace(); } } /** * 设置pdf样式 * @param file */ public PDFReport1(File file) { // 设置页面大小 document.setPageSize(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); } catch (Exception e) { e.printStackTrace(); } } /** * 添加表格前的说明 * @param value * @param font * @param align * @param colspan * @param boderFlag * @return */ public PdfPCell createCell(String value, com.lowagie.text.Font font, int align, int colspan, boolean boderFlag) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPhrase(new Phrase(value, font)); cell.setPadding(3.0f); if (!boderFlag) { cell.setBorder(0); cell.setPaddingTop(15.0f); cell.setPaddingBottom(8.0f); } return cell; } /** * 向单元格添加字符串、设置单元格属性 * @param value 字符 * @param font 字体 * @param align 对齐方式 * @return */ public PdfPCell createCell(String value, com.lowagie.text.Font font, int align) { PdfPCell cell = new PdfPCell(); //设置单元格对齐方式 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setPhrase(new Phrase(value, font)); //设置边框颜色 cell.setBorderColor(new Color(15, 15, 15)); //设置单元格背景颜色 cell.setBackgroundColor(new Color(118, 59, 167)); return cell; } /** * 向单元格添加字符串、设置单元格属性 * @param value 字符 * @param font 字体 * @return */ public PdfPCell createCell(String value, com.lowagie.text.Font font) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPhrase(new Phrase(value, font)); cell.setBorderColor(new Color(15, 15, 15)); cell.setBackgroundColor(new Color(59,162,167)); return cell; } /** * 向单元格中添加图片 * @param image 图片 */ public PdfPCell createCell() { PdfPCell cell = new PdfPCell(); try { cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); Image image = Image.getInstance ("D:\\Arrows_Black_InTable_Down.png"); //设置图片大小 // image.scaleAbsoluteWidth(7); // image.scaleAbsoluteHeight(11); image.scaleAbsolute(7,11); //添加图片 cell.addElement(image); //设置边框颜色 cell.setBorderColor(new Color(15, 15, 15)); //设置单元格背景颜色 cell.setBackgroundColor(new Color(59,162,167)); } catch (Exception e) { e.printStackTrace(); } return cell; } public PdfPCell createCell(String value, com.lowagie.text.Font font, int align, int colspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPhrase(new Phrase(value, font)); return cell; } public PdfPTable createTable(int colNumber) { PdfPTable table = new PdfPTable(colNumber); try { table.setTotalWidth(maxWidth); table.setLockedWidth(true); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder(1); } catch (Exception e) { e.printStackTrace(); } return table; } public PdfPTable createTable(float[] widths) { PdfPTable table = new PdfPTable(widths); try { table.setTotalWidth(maxWidth); table.setLockedWidth(true); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder(1); } catch (Exception e) { e.printStackTrace(); } return table; } public PdfPTable createBlankTable() { PdfPTable table = new PdfPTable(1); table.getDefaultCell().setBorder(0); table.addCell(createCell("", keyfont)); table.setSpacingAfter(20.0f); table.setSpacingBefore(20.0f); return table; } public void generatePDF() throws Exception { PdfPTable table = createTable(5); table.addCell(createCell("学生信息列表:", keyfont, Element.ALIGN_LEFT, 5, false)); table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER)); table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER)); table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER)); table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER)); table.addCell(createCell("升降", keyfont, Element.ALIGN_CENTER)); for (int i = 0; i < 5; i++) { table.addCell(createCell("姓名" + i, textfont)); table.addCell(createCell(i + 15 + "", textfont)); table.addCell(createCell((i % 2 == 0) ? "男" : "女", textfont)); table.addCell(createCell("地址" + i, textfont)); //添加图片 table.addCell(createCell()); } document.add(table); document.close(); } public static void main(String[] args) throws Exception { File file = new File("D:\\text.pdf"); file.createNewFile(); new PDFReport1(file).generatePDF(); } }
相关推荐
包含 itext-asian-5.2.0.jar itextpdf-5.5.5.jar
itextpdf-5.5.10.jar、itext-asian-5.2.0.jar、iText中文帮助、iText英文帮助文档 以及生成PDF文件时生成表格时单元格合并处理方法
html渲染页面转换为pdf并设置页眉,页码,水印,目录,二维码
用itext生成pdf文档,生成的内容有表格形式,文本形式,设置字体样式,文档页边距。
Java iText 生成pdf文档源代码(包括插入文本、表格、图像)
文档POI只支持往生成的中填入文本,对于图片根本就不支持。使用itext 生成rtf格式的直接保存为word
1、生成PDF文件 2、PDF文件注明版权 3、PDF增加文字和图片水印 4、PDF表格列数可能很多,比如1-50列信息,导出时需判断A4纸格式或其他格式宽度。 5、PDF表格行数量可能超大,比如10万以上,甚至100万以上。
iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。
itext7 html转换为pdf;iText7页码、页眉、页脚,itext 的复杂表格实现;完整springboot项目代码
1、生成PDF文件 2、支持中文字体 3、PDF文件内容为表格,表格有表头 4、PDF文件内容支持中文,表格内容上下居中,左右居中或左对齐/右对齐 5、PDF列信息多寡不同,PDF采用页面宽度也能根据列信息按比例调整 6、PDF行...
代码说明 https://blog.csdn.net/ruixue0117/article/details/77599808?spm=1001.2014.3001.5501
使用iTextSharp,可以生成PDF报表中的文本(Paragraph)、图片(Image)、 表格(PdfPTable)、单元格(PdfPCell),里面有多个教程可供参考。
java使用itext导出pdf,可以生成表格、文字、图片等,demo中详细介绍了使用的版本,可能出现的问题,以及生成模板的方法。全网最全!!
1.先创建PdfPTable对象,生成PDF表格cell之后,添加隔行换色的事件,将此事件在PdfPTable加入Document对象之前,插入进去。 2.隔行换色的事件需要自己写一个java类,里面去定义背景颜色和长宽高,实质就是在pdf表格...
NULL 博文链接:https://keep-going.iteye.com/blog/1907664
asp.net生成PDF详解 asp.net生成PDF PDF详解 用C#制作PDF文件全攻略 丽水市汽车运输集团有限公司信息中心 苟安廷 目 录 前 言 3 第一部分 iText的简单应用 4 第一章 创建一个Document 4 第一步 创建一个Document实例...
主要介绍了Java开源工具iText生成PDF简单实例,本文给出了3段代码实例,讲解创建一个简单PDF文件,在PDF中添加表格以及在PDF中添加图片,需要的朋友可以参考下
如果应用程序需要动态生成PDF文档,则需要iText库。开放源码的iText库使PDF文档的创建能够在瞬间完成。本文介绍了iText并提供了使用它从Java™技术应用程序生成PDF文档的由浅入深的指南。
涉及生成PDF(添加往新生成的PDF的文件中添加表格及段落,表头,标题等),往PDF模板里面插入文件的jar包,及发邮件的jar包