Styles

Files: 
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 5 version of one of the examples.
 */
package javaone.edition16;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import sandbox.WrapToTest;
 
@WrapToTest
public class HelloWorldStyles {
 
    public static final String DEST
        = "results/javaone/edition16/hello_styles.pdf";
 
    public static void main(String[] args)
    	throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
    	new HelloWorldStyles().createPdf(DEST);
    }
 
    public void createPdf(String dest)
	throws DocumentException, IOException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Font code = new Font(FontFamily.COURIER, 12, Font.NORMAL, BaseColor.RED);
        Paragraph p = new Paragraph("In this example, named ");
        p.add(createBgChunk("HelloWorldStyles", code));
        p.add(", we experiment with some text in ");
        p.add(createBgChunk("code style", code));
        p.add(".");
        document.add(p);
        document.close();
    }
 
    public Chunk createBgChunk(String s, Font font) {
        Chunk chunk = new Chunk(s, font);
        chunk.setBackground(BaseColor.LIGHT_GRAY);
        return chunk;
    }
}
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 7 version of one of the examples.
 */
package com.itextpdf.sandbox.javaone16;
 
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.Style;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.File;
import java.io.IOException;
 
@WrapToTest
public class HelloWorldStyles {
    public static final String DEST
        = "results/javaone16/hello_styles.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
    	new HelloWorldStyles().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        PdfFont code = PdfFontFactory.createFont(FontConstants.COURIER);
        Style style = new Style()
            .setFont(code)
            .setFontSize(12)
            .setFontColor(Color.RED)
            .setBackgroundColor(Color.LIGHT_GRAY);
        try (Document document = new Document(pdf)) {
            document.add(
                new Paragraph()
                    .add("In this example, named ")
                    .add(new Text("HelloWorldStyles").addStyle(style))
                    .add(", we experiment with some text in ")
                    .add(new Text("code style").addStyle(style))
                    .add("."));
        }
    }
}
File nameRaw URLUpdated
HelloWorldStyles.javaHelloWorldStyles.java2016-09-01 5:16 pm
HelloWorldStyles.javaHelloWorldStyles.java2016-09-01 5:18 pm
Results: 
File nameRaw URLUpdated
cmp_hello_styles.pdfcmp_hello_styles.pdf2016-09-01 5:23 pm
cmp_hello_styles.pdfcmp_hello_styles.pdf2016-09-01 5:24 pm