Tables to show keys and values
keyvaluetable
KeyValueTable Java
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class KeyValueTable {
public static final String DEST = "./target/sandbox/tables/key_value_table.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new KeyValueTable().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
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");
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
PdfFont regular = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(StandardFonts.TIMES_BOLD);
document.add(createTable(rohit, bold, regular));
document.add(createTable(bruno, bold, regular));
document.close();
}
private static Table createTable(UserObject user, PdfFont titleFont, PdfFont defaultFont) {
Table table = new Table(UnitValue.createPercentArray(2));
table.setWidth(UnitValue.createPercentValue(30)).setMarginBottom(10);
table.addHeaderCell(new Cell().setFont(titleFont).add(new Paragraph("Key")));
table.addHeaderCell(new Cell().setFont(titleFont).add(new Paragraph("Value")));
table.addCell(new Cell().setFont(titleFont).add(new Paragraph("Name")));
table.addCell(new Cell().setFont(defaultFont).add(new Paragraph(user.getName())));
table.addCell(new Cell().setFont(titleFont).add(new Paragraph("Id")));
table.addCell(new Cell().setFont(defaultFont).add(new Paragraph(user.getId())));
table.addCell(new Cell().setFont(titleFont).add(new Paragraph("Reputation")));
table.addCell(new Cell().setFont(defaultFont).add(new Paragraph(String.valueOf(user.getReputation()))));
table.addCell(new Cell().setFont(titleFont).add(new Paragraph("Job title")));
table.addCell(new Cell().setFont(defaultFont).add(new Paragraph(user.getJobtitle())));
return table;
}
private static 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;
}
}
}
KeyValueTable C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Tables
{
public class KeyValueTable
{
public static readonly string DEST = "results/sandbox/tables/key_value_table.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new KeyValueTable().ManipulatePdf(DEST);
}
private void ManipulatePdf(string dest)
{
UserObject rohit = new UserObject();
rohit.Name = "Rohit";
rohit.Id = "6633429";
rohit.Reputation = 1;
rohit.JobTitle = "Copy/paste artist";
UserObject bruno = new UserObject();
bruno.Name = "Bruno Lowagie";
bruno.Id = "1622493";
bruno.Reputation = 42690;
bruno.JobTitle = "Java Rockstar";
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
PdfFont regular = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.TIMES_BOLD);
document.Add(CreateTable(rohit, bold, regular));
document.Add(CreateTable(bruno, bold, regular));
document.Close();
}
private static Table CreateTable(UserObject user, PdfFont titleFont, PdfFont defaultFont)
{
Table table = new Table(UnitValue.CreatePercentArray(2));
table.SetWidth(UnitValue.CreatePercentValue(30)).SetMarginBottom(10);
table.AddHeaderCell(new Cell().SetFont(titleFont).Add(new Paragraph("Key")));
table.AddHeaderCell(new Cell().SetFont(titleFont).Add(new Paragraph("Value")));
table.AddCell(new Cell().SetFont(titleFont).Add(new Paragraph("Name")));
table.AddCell(new Cell().SetFont(defaultFont).Add(new Paragraph(user.Name)));
table.AddCell(new Cell().SetFont(titleFont).Add(new Paragraph("Id")));
table.AddCell(new Cell().SetFont(defaultFont).Add(new Paragraph(user.Id)));
table.AddCell(new Cell().SetFont(titleFont).Add(new Paragraph("Reputation")));
table.AddCell(new Cell().SetFont(defaultFont).Add(new Paragraph(user.Reputation.ToString())));
table.AddCell(new Cell().SetFont(titleFont).Add(new Paragraph("Job title")));
table.AddCell(new Cell().SetFont(defaultFont).Add(new Paragraph(user.JobTitle)));
return table;
}
private class UserObject
{
public string Name { set; get; }
public string Id { set; get; }
public string JobTitle { set; get; }
public int Reputation { set; get; }
}
}
}
keyvaluetable2
KeyValueTable2 Java
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class KeyValueTable2 {
public static final String DEST = "./target/sandbox/tables/key_value_table2.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new KeyValueTable2().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfFont bold = PdfFontFactory.createFont(StandardFonts.TIMES_BOLD);
PdfFont regular = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
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");
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
document.add(createTable(rohit, bruno, bold, regular));
document.close();
}
private static Table createTable(UserObject user1, UserObject user2, PdfFont bold, PdfFont regular) {
Table table = new Table(UnitValue.createPercentArray(3)).useAllAvailableWidth();
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add(new Paragraph("Name:")));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(user1.getName())));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(user2.getName())));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add(new Paragraph("Id:")));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(user1.getId())));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(user2.getId())));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add(new Paragraph("Reputation:")));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(String.valueOf(user1.getReputation()))));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(String.valueOf(user2.getReputation()))));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add(new Paragraph("Job title:")));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(user1.getJobtitle())));
table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(new Paragraph(user2.getJobtitle())));
return table;
}
private static class UserObject {
protected String name = "";
protected String id = "";
protected int reputation = 0;
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;
}
}
}
KeyValueTable2 C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Tables
{
public class KeyValueTable2
{
public static readonly string DEST = "results/sandbox/tables/key_value_table2.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new KeyValueTable2().ManipulatePdf(DEST);
}
private void ManipulatePdf(string dest)
{
PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.TIMES_BOLD);
PdfFont regular = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
UserObject rohit = new UserObject();
rohit.Name = "Rohit";
rohit.Id = "6633429";
rohit.Reputation = 1;
rohit.JobTitle = "Copy/paste artist";
UserObject bruno = new UserObject();
bruno.Name = "Bruno Lowagie";
bruno.Id = "1622493";
bruno.Reputation = 42690;
bruno.JobTitle = "Java Rockstar";
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
document.Add(CreateTable(rohit, bruno, bold, regular));
document.Close();
}
private static Table CreateTable(UserObject user1, UserObject user2, PdfFont bold, PdfFont regular)
{
Table table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth();
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(bold).Add(new Paragraph("Name:")));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular).Add(new Paragraph(user1.Name)));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular).Add(new Paragraph(user2.Name)));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(bold).Add(new Paragraph("Id:")));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular).Add(new Paragraph(user1.Id)));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular).Add(new Paragraph(user2.Id)));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(bold).Add(new Paragraph("Reputation:")));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular)
.Add(new Paragraph(user1.Reputation.ToString())));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular)
.Add(new Paragraph(user2.Reputation.ToString())));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(bold).Add(new Paragraph("Job title:")));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular).Add(new Paragraph(user1.JobTitle)));
table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetFont(regular).Add(new Paragraph(user2.JobTitle)));
return table;
}
private class UserObject
{
public string Name { set; get; }
public string Id { set; get; }
public string JobTitle { set; get; }
public int Reputation { set; get; }
}
}
}