Tables and fonts
A couple of table examples.
cellmethod
CellMethod 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.PdfEncodings;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
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.HorizontalAlignment;
import com.itextpdf.layout.properties.UnitValue;
import java.io.File;
public class CellMethod {
public static final String DEST = "./target/sandbox/tables/cell_method.pdf";
public static final String FONT = "./src/main/resources/font/FreeSans.ttf";
private static PdfFont czechFont;
private static PdfFont defaultFont;
private static PdfFont greekFont;
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CellMethod().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
czechFont = PdfFontFactory.createFont(FONT, "Cp1250", EmbeddingStrategy.PREFER_EMBEDDED);
greekFont = PdfFontFactory.createFont(FONT, "Cp1253", EmbeddingStrategy.PREFER_EMBEDDED);
defaultFont = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
// By default column width is calculated automatically for the best fit.
// useAllAvailableWidth() method makes table use the whole page's width while placing the content.
Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
table.addCell("Winansi");
table.addCell(getNormalCell("Test", null, 12));
table.addCell("Winansi");
table.addCell(getNormalCell("Test", null, -12));
table.addCell("Greek");
table.addCell(getNormalCell("\u039d\u03cd\u03c6\u03b5\u03c2", "greek", 12));
table.addCell("Czech");
table.addCell(getNormalCell("\u010c,\u0106,\u0160,\u017d,\u0110", "czech", 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", null, 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", "greek", 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", "czech", 12));
doc.add(table);
doc.close();
}
private static Cell getNormalCell(String string, String language, float size) {
if (string != null && "".equals(string)) {
return new Cell();
}
PdfFont f = getFontForThisLanguage(language);
Paragraph paragraph = new Paragraph(string).setFont(f);
Cell cell = new Cell().add(paragraph);
cell.setHorizontalAlignment(HorizontalAlignment.LEFT);
if (size < 0) {
size = -size;
cell.setFontSize(size);
cell.setFontColor(ColorConstants.RED);
}
return cell;
}
private static PdfFont getFontForThisLanguage(String language) {
if (language == null) {
return defaultFont;
}
switch (language) {
case "czech": {
return czechFont;
}
case "greek": {
return greekFont;
}
default: {
return defaultFont;
}
}
}
}
CellMethod C#
C#
using System;
using System.IO;
using iText.IO.Font;
using iText.Kernel.Colors;
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 CellMethod
{
public static readonly string DEST = "results/sandbox/tables/cell_method.pdf";
public static readonly string FONT = "../../../resources/font/FreeSans.ttf";
private static PdfFont czechFont;
private static PdfFont defaultFont;
private static PdfFont greekFont;
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new CellMethod().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
czechFont = PdfFontFactory.CreateFont(FONT, "Cp1250", PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
greekFont = PdfFontFactory.CreateFont(FONT, "Cp1253", PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
defaultFont = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
// By default column width is calculated automatically for the best fit.
// useAllAvailableWidth() method makes table use the whole page's width while placing the content.
Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
table.AddCell("Winansi");
table.AddCell(GetNormalCell("Test", null, 12));
table.AddCell("Winansi");
table.AddCell(GetNormalCell("Test", null, -12));
table.AddCell("Greek");
table.AddCell(GetNormalCell("\u039d\u03cd\u03c6\u03b5\u03c2", "greek", 12));
table.AddCell("Czech");
table.AddCell(GetNormalCell("\u010c,\u0106,\u0160,\u017d,\u0110", "czech", 12));
table.AddCell("Test");
table.AddCell(GetNormalCell(" ", null, 12));
table.AddCell("Test");
table.AddCell(GetNormalCell(" ", "greek", 12));
table.AddCell("Test");
table.AddCell(GetNormalCell(" ", "czech", 12));
doc.Add(table);
doc.Close();
}
private static Cell GetNormalCell(String line, String language, float size)
{
if (line != null && "".Equals(line))
{
return new Cell();
}
PdfFont f = GetFontForThisLanguage(language);
Paragraph paragraph = new Paragraph(line).SetFont(f);
Cell cell = new Cell().Add(paragraph);
cell.SetHorizontalAlignment(HorizontalAlignment.LEFT);
if (size < 0)
{
size = -size;
cell.SetFontSize(size);
cell.SetFontColor(ColorConstants.RED);
}
return cell;
}
private static PdfFont GetFontForThisLanguage(String language)
{
if (language == null)
{
return defaultFont;
}
switch (language)
{
case "czech":
return czechFont;
case "greek":
return greekFont;
default:
return defaultFont;
}
}
}
}
simpletable7
SimpleTable7 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.colors.ColorConstants;
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.HorizontalAlignment;
import com.itextpdf.layout.properties.UnitValue;
import java.io.File;
public class SimpleTable7 {
public static final String DEST = "./target/sandbox/tables/simple_table7.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SimpleTable7().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfFont titleFont = PdfFontFactory.createFont(StandardFonts.COURIER_BOLD);
Paragraph docTitle = new Paragraph("UCSC Direct - Direct Payment Form").setMarginRight(1);
docTitle.setFont(titleFont).setFontSize(11);
doc.add(docTitle);
PdfFont subtitleFont = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
Paragraph subTitle = new Paragraph("(not to be used for reimbursement of services)");
subTitle.setFont(subtitleFont).setFontSize(9);
doc.add(subTitle);
PdfFont importantNoticeFont = PdfFontFactory.createFont(StandardFonts.COURIER);
Paragraph importantNotice = new Paragraph("Important: Form must be filled out in Adobe Reader or Acrobat " +
"Professional 8.1 or above. To save completed forms, Acrobat Professional is required. " +
"For technical and accessibility assistance, contact the Campus Controller's Office.");
importantNotice.setFont(importantNoticeFont).setFontSize(9);
importantNotice.setFontColor(ColorConstants.RED);
doc.add(importantNotice);
Table table = new Table(UnitValue.createPercentArray(10)).setFixedLayout().
setWidth(UnitValue.createPercentValue(80));
Cell cell = new Cell(1, 3).add(docTitle);
cell.setBorder(Border.NO_BORDER);
cell.setHorizontalAlignment(HorizontalAlignment.LEFT);
table.addCell(cell);
Cell cellCaveat = new Cell(1, 2).add(subTitle);
cellCaveat.setBorder(Border.NO_BORDER);
table.addCell(cellCaveat);
Cell cellImportantNote = new Cell(1, 5).add(importantNotice);
cellImportantNote.setBorder(Border.NO_BORDER);
table.addCell(cellImportantNote);
doc.add(table.setHorizontalAlignment(HorizontalAlignment.CENTER));
doc.add(new Paragraph("This is the same table, created differently").
setFont(subtitleFont).setFontSize(9).setMarginBottom(10));
table = new Table(UnitValue.createPercentArray(new float[] {30, 20, 50})).setFixedLayout()
.setWidth(UnitValue.createPercentValue(80));
table.addCell(new Cell().add(docTitle).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(subTitle).setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(importantNotice).setBorder(Border.NO_BORDER));
doc.add(table.setHorizontalAlignment(HorizontalAlignment.CENTER));
doc.close();
}
}
SimpleTable7 C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
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 SimpleTable7
{
public static readonly string DEST = "results/sandbox/tables/simple_table7.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new SimpleTable7().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfFont titleFont = PdfFontFactory.CreateFont(StandardFonts.COURIER_BOLD);
Paragraph docTitle = new Paragraph("UCSC Direct - Direct Payment Form").SetMarginRight(1);
docTitle.SetFont(titleFont).SetFontSize(11);
doc.Add(docTitle);
PdfFont subtitleFont = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
Paragraph subTitle = new Paragraph("(not to be used for reimbursement of services)");
subTitle.SetFont(subtitleFont).SetFontSize(9);
doc.Add(subTitle);
PdfFont importantNoticeFont = PdfFontFactory.CreateFont(StandardFonts.COURIER);
Paragraph importantNotice = new Paragraph(
"Important: Form must be filled out in Adobe Reader or Acrobat Professional 8.1 or above. " +
"To save completed forms, Acrobat Professional is required. For technical and accessibility assistance, " +
"contact the Campus Controller's Office.");
importantNotice.SetFont(importantNoticeFont).SetFontSize(9);
importantNotice.SetFontColor(ColorConstants.RED);
doc.Add(importantNotice);
Table table = new Table(UnitValue.CreatePercentArray(10))
.SetFixedLayout().SetWidth(UnitValue.CreatePercentValue(80));
Cell cell = new Cell(1, 3).Add(docTitle);
cell.SetBorder(Border.NO_BORDER);
cell.SetHorizontalAlignment(HorizontalAlignment.LEFT);
table.AddCell(cell);
Cell cellCaveat = new Cell(1, 2).Add(subTitle);
cellCaveat.SetBorder(Border.NO_BORDER);
table.AddCell(cellCaveat);
Cell cellImportantNote = new Cell(1, 5).Add(importantNotice);
cellImportantNote.SetBorder(Border.NO_BORDER);
table.AddCell(cellImportantNote);
doc.Add(table.SetHorizontalAlignment(HorizontalAlignment.CENTER));
doc.Add(new Paragraph("This is the same table, created differently").SetFont(subtitleFont).SetFontSize(9)
.SetMarginBottom(10));
table = new Table(UnitValue.CreatePercentArray(new float[] {30, 20, 50})).SetFixedLayout()
.SetWidth(UnitValue.CreatePercentValue(80));
table.AddCell(new Cell().Add(docTitle).SetBorder(Border.NO_BORDER));
table.AddCell(new Cell().Add(subTitle).SetBorder(Border.NO_BORDER));
table.AddCell(new Cell().Add(importantNotice).SetBorder(Border.NO_BORDER));
doc.Add(table.SetHorizontalAlignment(HorizontalAlignment.CENTER));
doc.Close();
}
}
}
keyvaluetable
SimpleTable7 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;
}
}
}
SimpleTable7 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;
}
}
}
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; }
}
}
}
Resources