标签:spl end create fhe ext text tst tps throw
代码和图片出处
https://howtodoinjava.com/library/read-generate-pdf-java-itext/
pom引入jar包
<dependencies> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.3</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> </dependencies>
先看效果 使用wps打开的 解决了不支持中文的问题--------网站上的HelloWorld不支持中文输出
整体项目 这里先说下 HelloWorld 下文解释所有
//准备数据类
package com.wf.zhang.test; public class Person { private String name; private Integer age; private String Adress; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAdress() { return Adress; } public void setAdress(String adress) { Adress = adress; } public Person() { } public Person(String name, Integer age, String adress) { super(); this.name = name; this.age = age; Adress = adress; } @Override public String toString() { return String.format("Person [name=%s, age=%s, Adress=%s]", name, age, Adress); } }
//生成PDF的类
package com.wf.zhang.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; public class JavaPdfHelloWorld { public static void main(String[] args) throws IOException { Document document = new Document(); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf")); document.open(); //设置宋体 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //设置字大小 颜色 Font font = new Font(bfChinese, 20, Font.NORMAL, BaseColor.RED); //准备Person类 Person person = new Person(); person.setName("娃哈哈"); person.setAge(99); person.setAdress("杭州西湖区文一路"); //准备personList List<Person> personList = Arrays.asList( new Person("西湖一",18,"杭州西湖区123号东方通信大厦66楼1666号室"), new Person("滨江二",26,"杭州滨江区456号辉煌大厦99楼2999号室"), new Person("萧山三",66,"杭州萧山区789号新玛特国际大厦888楼38888号室") ); //PDF输出内容 document.add(new Paragraph(person.toString(), font)); //继续输出 document.add(new Paragraph(personList.toString(),font)); document.close(); writer.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { } } }
标签:spl end create fhe ext text tst tps throw
原文地址:https://www.cnblogs.com/wf-zhang/p/12012584.html