Tables and fonts

Tags: fontstablescelliText 7

A couple of table examples.

Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.color.Color;
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.property.HorizontalAlignment;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class CellMethod extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/cell_method.pdf";
    public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
 
    private static PdfFont czechFont = null;
    private static PdfFont defaultFont = null;
    private static PdfFont greekFont = null;
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CellMethod().manipulatePdf(DEST);
    }
 
    public static Cell getNormalCell(String string, String language, float size) throws IOException {
        if (string != null && "".equals(string)) {
            return new Cell();
        }
        PdfFont f = getFontForThisLanguage(language);
        Cell cell = new Cell().add(new Paragraph(string).setFont(f));
        cell.setHorizontalAlignment(HorizontalAlignment.LEFT);
        if (size < 0) {
            size = -size;
            cell.setFontSize(size);
            cell.setFontColor(Color.RED);
        }
        return cell;
    }
 
    public static PdfFont getFontForThisLanguage(String language) throws IOException {
        if ("czech".equals(language)) {
            return czechFont;
        }
        if ("greek".equals(language)) {
            return greekFont;
        }
        return defaultFont;
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        setCompareRenders(true);
 
        czechFont = PdfFontFactory.createFont(FONT, "Cp1250", true);
        greekFont = PdfFontFactory.createFont(FONT, "Cp1253", true);
        defaultFont = PdfFontFactory.createFont(FONT, null, true);
 
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(2);
        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();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.color.Color;
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.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class SimpleTable7 extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        PdfFont titleFont = PdfFontFactory.createFont(FontConstants.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(FontConstants.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(FontConstants.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(Color.RED);
        doc.add(importantNotice);
 
        Table table = new Table(10).
                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(null);
        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(new float[]{3, 2, 5}).
                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();
    }
}
/*
 * This question was written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/39190026
 */
package com.itextpdf.sandbox.tables;
 
import com.itextpdf.io.font.FontConstants;
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.Table;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;
 
/**
 * @author bruno
 */
@WrapToTest
public class KeyValueTable {
 
    public static final String DEST = "results/tables/key_value_table.pdf";
 
    public final PdfFont regular;
    public final PdfFont bold;
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new KeyValueTable().createPdf(DEST);
    }
 
    public KeyValueTable() throws IOException {
        this.regular = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
        this.bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
    }
 
    public void createPdf(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);
        document.add(createTable(rohit));
        document.add(createTable(bruno));
        document.close();
    }
 
    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;
    }
 
    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;
        }
    }
}
/*
 * This question was written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/39190026
 */
package com.itextpdf.sandbox.tables;
 
import com.itextpdf.io.font.FontConstants;
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.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;
 
/**
 * @author bruno
 */
@WrapToTest
public class KeyValueTable2 {
 
    public static final String DEST = "results/tables/key_value_table2.pdf";
 
    public final PdfFont regular;
    public final PdfFont bold;
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new KeyValueTable2().createPdf(DEST);
    }
 
    public KeyValueTable2() throws IOException {
        this.regular = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
        this.bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
    }
 
    public void createPdf(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);
        document.add(createTable(rohit, bruno));
        document.close();
    }
 
    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;
    }
 
    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;
        }
    }
}
File nameRaw URLUpdated
CellMethod.javaCellMethod.java2016-08-09 10:33 am
SimpleTable7.javaSimpleTable7.java2016-08-09 10:33 am
KeyValueTable.javaKeyValueTable.java2016-08-30 2:42 pm
KeyValueTable2.javaKeyValueTable2.java2016-08-30 2:42 pm
Resources: 
File nameRaw URLUpdated
FreeSans.ttfFreeSans.ttf2016-08-04 3:45 pm