Chunk examples
Examples written in answer to questions such as:
chunkbackground
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/19976343/how-to-set-the-paragraph-of-itext-pdf-file-as-rectangle-with-background-color-in
*
* We create a table with two columns and two cells.
* This way, we can add two images next to each other.
*/
package sandbox.objects;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class ChunkBackground {
public static final String DEST = "results/objects/chunk_background.pdf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ChunkBackground().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font f = new Font(FontFamily.TIMES_ROMAN, 25.0f, Font.BOLD, BaseColor.WHITE);
Chunk c = new Chunk("White text on red background", f);
c.setBackground(BaseColor.RED);
Paragraph p = new Paragraph(c);
document.add(p);
document.close();
}
}
subsuperscript
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/24772212/itext-pdf-writer-is-there-any-way-to-allow-unicode-subscript-symbol-in-the-pdf
*/
package sandbox.objects;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class SubSuperScript {
public static final String DEST = "results/objects/sub_superscript.pdf";
public static final String FONT = "resources/fonts/Cardo-Regular.ttf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SubSuperScript().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 10);
Paragraph p = new Paragraph("H\u2082SO\u2074", f);
document.add(p);
document.close();
}
}
ordinalnumbers
JAVA
JAVA
$body
standarddeviation
JAVA
JAVA
/**
* This code sample was written by Bruno Lowagie in answer to this question:
* http://stackoverflow.com/questions/30876926/itext-std-deviation-symbol-%CF%83-not-printing
*/
package sandbox.objects;
import com.itextpdf.text.Chunk;
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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
* @author Bruno Lowagie (iText Software)
*/
@WrapToTest
public class StandardDeviation {
public static final String DEST = "results/objects/standard_deviation.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new StandardDeviation().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("The standard deviation symbol doesn't exist in Helvetica."));
Font symbol = new Font(FontFamily.SYMBOL);
Paragraph p = new Paragraph("So we use the Symbol font: ");
p.add(new Chunk("s", symbol));
document.add(p);
document.close();
}
}
bullets
JAVA
JAVA
/*
* This example was written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/30369587/how-to-write-bulleted-list-in-pdf-using-itext-jar-but-item-should-not-in-next-li
*/
package sandbox.objects;
import com.itextpdf.text.Chunk;
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.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
* @author iText
*/
@WrapToTest
public class Bullets {
public static final String DEST = "results/objects/bullets.pdf";
public static final String[] ITEMS = {
"Insurance system", "Agent", "Agency", "Agent Enrollment", "Agent Settings",
"Appointment", "Continuing Education", "Hierarchy", "Recruiting", "Contract",
"Message", "Correspondence", "Licensing", "Party"
};
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Bullets().createPdf(DEST);
}
public void createPdf(String dest) throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font zapfdingbats = new Font(FontFamily.ZAPFDINGBATS, 8);
Font font = new Font();
Chunk bullet = new Chunk(String.valueOf((char) 108), zapfdingbats);
Paragraph p = new Paragraph("Items can be split if they don't fit at the end: ", font);
for (String item: ITEMS) {
p.add(bullet);
p.add(new Phrase(" " + item + " ", font));
}
document.add(p);
document.add(Chunk.NEWLINE);
p = new Paragraph("Items can't be split if they don't fit at the end: ", font);
for (String item: ITEMS) {
p.add(bullet);
p.add(new Phrase("\u00a0" + item.replace(' ', '\u00a0') + " ", font));
}
document.add(p);
document.add(Chunk.NEWLINE);
BaseFont bf = BaseFont.createFont("resources/fonts/FreeSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
p = new Paragraph("Items can't be split if they don't fit at the end: ", f);
for (String item: ITEMS) {
p.add(new Phrase("\u2022\u00a0" + item.replace(' ', '\u00a0') + " ", f));
}
document.add(p);
document.close();
}
}