我在 java 中有两个用户对象,其中包含数据 id、姓名、姓氏、中间名、国家、地区、地区等,我想将他们的数据写入两个单独列中的 pdf(iText7 pdf) 文件。任何建议!
我的代码是:
用户等级:
private int id ;
private String name ;
private String middleName;
private String surname ;
private String email;
private String country;
private String zone;
private String district;
private String city;
private String tole;
private int zip_code;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTole() {
return tole;
}
public void setTole(String tole) {
this.tole = tole;
}
public int getZip_code() {
return zip_code;
}
public void setZip_code(int zip_code) {
this.zip_code = zip_code;
}
JptTut 类
public static final String DEST = "result/jpt.pdf";
public void createPdf(List<User> users) throws IOException{
int i = 0;
File file = new File(DEST);
file.getParentFile().mkdir();
PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
PageSize ps = PageSize.A4;
float topBottom = 100;
float offset = 20;
float margin = 10;
int colWidth = (int) ((ps.getWidth()-2*offset-2*margin)/3);
int colHeight = (int) (ps.getHeight()-2*topBottom);
PdfPage page = pdf.addNewPage(ps);
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle1 = new Rectangle(20,topBottom,colWidth,colHeight);
Rectangle rectangle2 = new Rectangle(208,topBottom,colWidth,colHeight);
Rectangle rectangle3 = new Rectangle(396,topBottom,colWidth,colHeight);
pdfCanvas.rectangle(rectangle1);
pdfCanvas.rectangle(rectangle2);
pdfCanvas.rectangle(rectangle3);
Canvas canvas1 = new Canvas(pdfCanvas, pdf, rectangle1);
Canvas canvas2 = new Canvas(pdfCanvas,pdf,rectangle2);
Canvas canvas3 = new Canvas(pdfCanvas,pdf,rectangle3);
Paragraph p1 = new Paragraph("Details").setFontColor(Color.BLUE).setFontSize(20);
Paragraph p2 = new Paragraph("User ID :");
Paragraph p3 = new Paragraph("User Name :");
Paragraph p4 = new Paragraph("Surname :");
Paragraph p5 = new Paragraph("Middle Name :");
Paragraph p6 = new Paragraph("Email :");
Paragraph p7 = new Paragraph("Country :");
Paragraph p8 = new Paragraph("Zone :");
Paragraph p9 = new Paragraph("District :");
Paragraph p10 = new Paragraph("City :");
Paragraph p11 = new Paragraph("Tole :");
Paragraph p12 = new Paragraph("Zip Code :");
canvas1.add(p1).add(p2).add(p3).add(p4).add(p5).add(p6).add(p7).add(p8).add(p9).add(p10).add(p11).add(p12);
for(User user:users){
if(i==0){
Paragraph para0 = new Paragraph("To ").setFontColor(Color.GREEN).setFontSize(20);
Paragraph para1 = new Paragraph(Integer.toString(user.getId()));
Paragraph para2 = new Paragraph(user.getName());
Paragraph para3 = new Paragraph(user.getSurname());
Paragraph para4 = new Paragraph(user.getMiddleName());
Paragraph para5 = new Paragraph(user.getEmail());
Paragraph para6 = new Paragraph(user.getCountry());
Paragraph para7 = new Paragraph(user.getZone());
Paragraph para8 = new Paragraph(user.getDistrict());
Paragraph para9 = new Paragraph(user.getCity());
Paragraph para10 = new Paragraph(user.getTole());
Paragraph para11 = new Paragraph(Integer.toString(user.getZip_code()));
canvas2.add(para0).add(para1).add(para2).add(para3).add(para4).add(para5).add(para6).add(para7).add(para8).add(para9).add(para10).add(para11);
i++;
}
else{
Paragraph para0 = new Paragraph("From ").setFontColor(Color.MAGENTA).setFontSize(20);
Paragraph para1 = new Paragraph(Integer.toString(user.getId()));
Paragraph para2 = new Paragraph(user.getName());
Paragraph para3 = new Paragraph(user.getSurname());
Paragraph para4 = new Paragraph(user.getMiddleName());
Paragraph para5 = new Paragraph(user.getEmail());
Paragraph para6 = new Paragraph(user.getCountry());
Paragraph para7 = new Paragraph(user.getZone());
Paragraph para8 = new Paragraph(user.getDistrict());
Paragraph para9 = new Paragraph(user.getCity());
Paragraph para10 = new Paragraph(user.getTole());
Paragraph para11 = new Paragraph(Integer.toString(user.getZip_code()));
canvas3.add(para0).add(para1).add(para2).add(para3).add(para4).add(para5).add(para6).add(para7).add(para8).add(para9).add(para10).add(para11);
}
}
pdf.close();
}
}
测试类
public static void main(String[] args) throws IOException {
User user1 = new User();
user1.setId(1);
user1.setName("Kamal");
user1.setSurname("Rana");
user1.setMiddleName("Magar");
user1.setEmail("ranakamal@gmail.com");
user1.setCountry("Nepal");
user1.setZone("Bagmati");
user1.setDistrict("Kathmandu");
user1.setCity("Patan");
user1.setTole("kumaripati");
user1.setZip_code(234);
User user2 = new User();
user2.setId(2);
user2.setName("Santosh");
user2.setSurname("Shrestha");
user2.setMiddleName("Kumar");
user2.setEmail("kumarsantos@gmail.com");
user2.setCountry("Nepal");
user2.setZone("Lumbini");
user2.setDistrict("Nawalparasi");
user2.setCity("Pragatinagar");
user2.setTole("Bagwati Tole");
user2.setZip_code(432);
List<User> users = new ArrayList<User>();
users.add(user1);
users.add(user2);
new JptTut().createPdf(users);
}
}