java: create pdf using itextpdf 2.1.7 Library
2022/1/19 17:50:53
本文主要是介绍java: create pdf using itextpdf 2.1.7 Library,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * IDE:IntelliJ IDEA 2021.2.3 * 数据库:MSSQL Server 2019 * OS:windows 10 x64 * 历史版本: JDK 14.02 * 2022-1-12 创建者 geovindu * 2022-1-15 添加 Lambda * 2022-1-15 修改:date * 接口类 mssql-jdbc-9.4.1.jre16.jar. * * 2022-1-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc iTextHelper.java https://mvnrepository.com/artifact/com.itextpdf https://mvnrepository.com/artifact/com.lowagie/itext/2.1.7 https://mvnrepository.com/artifact/com.lowagie/itext/4.2.1 https://sourceforge.net/projects/itext/ https://github.com/itext java write stringbuilder to file http://guava-libraries.googlecode.com/ https://github.com/google/guava Files.write(stringBuilder, file, Charsets.UTF_8) http://commons.apache.org/io/ You could use the Apache Commons IO library, which gives you FileUtils: FileUtils.writeStringToFile(file, stringBuilder.toString(), Charset.forName("UTF-8")) https://github.com/weiyeh/iText-4.2.0 https://github.com/ymasory/iText-4.2.0 https://mvnrepository.com/artifact/com.itextpdf/html2pdf http://www.java2s.com/Code/Jar/i/Downloaditextpdf541jar.htm http://www.java2s.com/Code/Jar/i/Downloaditextrtf215jar.htm https://mvnrepository.com/artifact/com.lowagie/itext-rtf/2.1.7 http://www.java2s.com/Code/Jar/i/Downloaditextasian217jar.htm https://mvnrepository.com/artifact/com.itextpdf/itext-asian/5.2.0 https://mvnrepository.com/artifact/com.itextpdf https://mvnrepository.com/artifact/com.itextpdf.tool http://www.java2s.com/Code/Jar/i/itext.htm * */ package Geovin.Common; import java.awt.Color; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; /* 5.4.1 import com.itextpdf.io.*; import com.itextpdf.pdfa.*; import com.itextpdf.test.*; import com.itextpdf.commons.*; import com.itextpdf.pdfa.PdfADocument; import com.itextpdf.barcodes.*; import com.itextpdf.svg.*; import com.itextpdf.forms.*; import com.itextpdf.kernel.*; import com.itextpdf.layout.*; import com.itextpdf.layout.font.*; import com.itextpdf.styledxmlparser.*; import com.itextpdf.signatures.*; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.Image; import com.itextpdf.text.Phrase; import com.itextpdf.text.Header; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.TextField; import com.itextpdf.text.*; */ import Geovin.Model.Person; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.tools.*; import com.lowagie.text.pdf.fonts.*; /** *itext-rtf 2.1.7 * itextasian 2.1.7 *itextpdf 2.1.7 * @author geovindu * @version 1.0 * */ public class iTextHelper { /** * * * * */ public void Create() throws DocumentException, IOException { // 创建Document对象(页面的大小为A4,左、右、上、下的页边距为10) Document document = new Document(PageSize.A4, 10, 10, 10, 10); // 建立书写器 PdfWriter.getInstance(document, new FileOutputStream("src/geovindu.PDF")); // 设置相关的参数 setParameters(document, "开发者测试", "涂聚文测试", "测试 开发者 调试", "geovindu", "geovindu"); // 打开文档 document.open(); // 使用iTextAsian.jar中的字体 BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font font = new Font(baseFont); List<Person> personList = new ArrayList<Person>(); // 循环添加对象 for (int i = 0; i < 5; i++) { Person user = new Person(); user.setLastName("geovindu:"+i); user.setFirstName("开发者测试"+i); user.setSex("测试"+i); personList.add(user); } Table table = setTable(personList); document.add(new Paragraph("用户信息如下:",setFont())); document.add(table); // 关闭文档 document.close(); } /** * * * */ public Table setTable(List<Person> personList) throws BadElementException{ //创建一个有3列的表格 Table table = new Table(3); table.setBorderWidth(1); table.setBorderColor(new Color(0, 0, 255)); table.setPadding(5); table.setSpacing(5); // 创建表头 Cell cell1 = setTableHeader("姓"); Cell cell2 = setTableHeader("名"); Cell cell3 = setTableHeader("性别"); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); // 添加此代码后每页都会显示表头 table.endHeaders(); for (int i = 0; i < personList.size(); i++) { Cell celli1 = setTableHeader(personList.get(i).getLastName()); Cell celli2 = setTableHeader(personList.get(i).getFirstName()); Cell celli3 = setTableHeader(personList.get(i).getSex()); table.addCell(celli1); table.addCell(celli2); table.addCell(celli3); } return table; } /** * itextasian 2.1.7 * * */ public Font setFont(){ BaseFont baseFont = null; try { baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Font font = new Font(baseFont, 8, Font.NORMAL,Color.BLUE); return font; } /** * 设置cell * @param name * @return * @throws BadElementException */ public Cell setTableHeader(String name) throws BadElementException { Cell cell = new Cell(new Phrase(name, setFont())); //单元格水平对齐方式 cell.setHorizontalAlignment(Element.ALIGN_CENTER); //单元格垂直对齐方式 cell.setVerticalAlignment(Element.ALIGN_CENTER); // cell.setHeader(true); //cell.setBackgroundColor(Color.RED); return cell; } /** * 设置相关参数 * @param document * @return */ public Document setParameters(Document document,String title,String subject,String keywords,String author, String creator){ // 设置标题 document.addTitle(title); // 设置主题 document.addSubject(subject); // 设置作者 document.addKeywords(keywords); // 设置作者 document.addAuthor(author); // 设置创建者 document.addCreator(creator); // 设置生产者 document.addProducer(); // 设置创建日期 document.addCreationDate(); return document; } }
这篇关于java: create pdf using itextpdf 2.1.7 Library的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南