-1

我在 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);

}

}

4

1 回答 1

2

你的问题不够具体,但请允许我做一些猜测。

我创建了一个名为KeyValueTable的示例,它将用户对象中的数据分两列写入 PDF。结果如下所示:

在此处输入图像描述

你说我在java中有两个用户对象,数据ID,名称,......

我假设此类用户对象的类是这样的 POJO:

class UserObject {

    protected String name;
    protected String id;
    protected int reputation;
    protected String jobtitle;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getReputation() {
        return reputation;
    }

    public void setReputation(int reputation) {
        this.reputation = reputation;
    }

    public String getJobtitle() {
        return jobtitle;
    }

    public void setJobtitle(String jobtitle) {
        this.jobtitle = jobtitle;
    }
}

你说你有两个这样的对象,例如:

UserObject rohit = new UserObject();
rohit.setName("Rohit");
rohit.setId("6633429");
rohit.setReputation(1);
rohit.setJobtitle("Copy/paste artist");

UserObject bruno = new UserObject();
bruno.setName("Bruno Lowagie");
bruno.setId("1622493");
bruno.setReputation(42690);
bruno.setJobtitle("Java Rockstar");

然后你说我想将他们的数据写入 PDF 中的两个单独的列。我假设您要创建一个表,其中一列中的键和另一列中的相应值。这很简单,您必须按照iText 7: Building Blocks教程第 5 章Table中的说明创建一个:

public Table createTable(UserObject user) {
    Table table = new Table(2);
    table.setWidthPercent(30).setMarginBottom(10);
    table.addHeaderCell(new Cell().setFont(bold).add("Key"));
    table.addHeaderCell(new Cell().setFont(bold).add("Value"));
    table.addCell(new Cell().setFont(bold).add("Name"));
    table.addCell(new Cell().setFont(regular).add(user.getName()));
    table.addCell(new Cell().setFont(bold).add("Id"));
    table.addCell(new Cell().setFont(regular).add(user.getId()));
    table.addCell(new Cell().setFont(bold).add("Reputation"));
    table.addCell(new Cell().setFont(regular).add(String.valueOf(user.getReputation())));
    table.addCell(new Cell().setFont(bold).add("Job title"));
    table.addCell(new Cell().setFont(regular).add(user.getJobtitle()));
    return table;
}

现在我们有了一个将 a 转换为 a 的方法UserObjectTable我们可以使用该方法将这些用户对象呈现为 PDF:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
document.add(createTable(rohit));
document.add(createTable(bruno));
document.close();

就是这么简单。你为什么问?文档中是否有一些不清楚的地方?你的问题是什么?

当然:也许您想要彼此相邻rohit的用户数据。bruno这在本教程的第二章中进行了解释。无论如何:解决您的问题;阅读文档。

更新:

您似乎正在创建三列。在第一列中添加关键字,在第二列和第三列中添加与这些关键字对应的值。您指望这样一个事实,即没有任何数据在其列中占用超过一行。这不是一个安全的赌注。也许现在还可以,但是您无法预测将来需要什么。

我已经修改了我的例子;见KeyValueTable2。数据现在呈现如下:

在此处输入图像描述

createTable()方法现在看起来像这样:

public Table createTable(UserObject user1, UserObject user2) {
    if (user1 == null) user1 = new UserObject();
    if (user2 == null) user2 = new UserObject();
    Table table = new Table(3);
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Name:"));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user1.getName()));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user2.getName()));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Id:"));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user1.getId()));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user2.getId()));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Reputation:"));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(String.valueOf(user1.getReputation())));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(String.valueOf(user2.getReputation())));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Job title:"));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user1.getJobtitle()));
    table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user2.getJobtitle()));
    return table;
}

我这样称呼它:

document.add(createTable(rohit, bruno));

我认为这就是您想要实现的目标,而且我很确定该Table课程比您迄今为止所尝试的更适合您的需求。

于 2016-08-28T13:33:05.477 回答