2.2 Add Chunk, Phrase, Paragraph and List objects
Chunk: a string, a font, some attributes
Space between two lines: Leading
setInitialLeading(16);
setTextRise()–Set the rising height
import java.io.FileOutputStream; import java.io.IOException; import java.sql.SQLException; 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.pdf.PdfWriter; /** * Writes a list of countries to a PDF file. */ public class CountryChunks { /** The resulting PDF file. */ public static final String RESULT = "D:/data/iText/inAction/chapter02/country_chunks.pdf"; /** * Main method. * * @param args no arguments needed * @throws DocumentException * @throwsIOException * @throws SQLException */ public static void main(String[] args) throws IOException, DocumentException, SQLException { new CountryChunks().createPdf(RESULT); } /** * Creates a PDF document. * @param filename the path to the new PDF document * @throws DocumentException * @throwsIOException * @throws SQLException */ public void createPdf(String filename) throws IOException, DocumentException, SQLException{ // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)) .setInitialLeading(16); // step 3 document.open(); // step 4 document.add(new Chunk("Argentina ")); Font font = new Font(FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE); Chunk id = new Chunk("AR", font); // with a background color id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f); // and a text rise id.setTextRise(6); document.add(id); document.add(Chunk.NEWLINE); document.add(new Chunk("Australia ")); id = new Chunk("AU", font); // with a background color id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f); // and a text rise id.setTextRise(6); document.add(id); document.add(Chunk.NEWLINE); document.add(new Chunk("Belgium ")); id = new Chunk("BE", font); // with a background color id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f); // and a text rise id.setTextRise(6); document.add(id); document.add(Chunk.NEWLINE); // step 5 document.close(); } }
Phrase: a series of Chunks
import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.sql.SQLException; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Phrase; import com.itextpdf.text.Font.FontFamily; import com.itextpdf.text.pdf.PdfWriter; /** * Writes a list of directors to a PDF file. */ public class DirectorPhrases1 { /** The resulting PDF file. */ public static final String RESULT = "D:/data/iText/inAction/chapter02/director_phrases_1.pdf"; /** A font that will be used in our PDF. */ public static final Font BOLD_UNDERLINED = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD | Font.UNDERLINE); /** A font that will be used in our PDF. */ public static final Font NORMAL = new Font(FontFamily.TIMES_ROMAN, 12); /** * Creates a Phrase with the name and given name of a director using different fonts. * @param rs the ResultSet containing director records. */ public Phrase createDirectorPhrase(final String name, final String givenName) throws UnsupportedEncodingException, SQLException { Phrase director = new Phrase(); director.add( new Chunk(name, BOLD_UNDERLINED)); director.add(new Chunk(",", BOLD_UNDERLINED)); director.add(new Chunk(" ", NORMAL)); director.add(new Chunk(givenName, NORMAL)); return director; } /** * Creates a PDF file with director names. * @param filename the name of the PDF file that needs to be created. * @throws DocumentException * @throwsIOException * @throws SQLException */ public void createPdf(String filename) throws IOException, DocumentException, SQLException { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 document.add(createDirectorPhrase("Affleck", "Ben")); document.add(Chunk.NEWLINE); document.add(createDirectorPhrase("Aronofsky", "Darren")); document.add(Chunk.NEWLINE); document.add(createDirectorPhrase("Becker", "Wolfgang")); document.add(Chunk.NEWLINE); document.add(createDirectorPhrase("Beineix", "Jean-Jacques")); document.add(Chunk.NEWLINE); // step 5 document.close(); } /** * Main method. * * @param args no arguments needed * @throws DocumentException * @throwsIOException * @throws SQLException */ public static void main(String[] args) throws IOException, DocumentException, SQLException { new DirectorPhrases1().createPdf(RESULT); } }
import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.sql.SQLException; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; /** * Writes a list of directors to a PDF file. */ public class DirectorPhrases2 { /** The resulting PDF file. */ public static final String RESULT = "D:/data/iText/inAction/chapter02/director_phrases_2.pdf"; /** A font that will be used in our PDF. */ public static final Font BOLD; /** A font that will be used in our PDF. */ public static final Font NORMAL; static { BaseFont timesbd = null; BaseFont times = null; try { // create a font that will be embedded timesbd = BaseFont.createFont( "c:/windows/fonts/timesbd.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED); // create a font that will be embedded times = BaseFont.createFont( "c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { e.printStackTrace(); System.exit(1); } BOLD = new Font(timesbd, 12); NORMAL = new Font(times, 12); } /** * Creates a Phrase with the name and given name of a director using different fonts. * @param rs the ResultSet containing director records. */ public Phrase createDirectorPhrase(final String nameStr, final String givenName) throws UnsupportedEncodingException, SQLException { Phrase director = new Phrase(); Chunk name = new Chunk(nameStr, BOLD); name.setUnderline(0.2f, -2f); director.add(name); director.add(new Chunk(",", BOLD)); director.add(new Chunk(" ", NORMAL)); director.add(new Chunk(givenName, NORMAL)); director.setLeading(24); return director; } /** * Creates a PDF file with director names. * @param filename the name of the PDF file that needs to be created. * @throws DocumentException * @throwsIOException * @throws SQLException */ public void createPdf(String filename) throws IOException, DocumentException, SQLException { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 document.add(createDirectorPhrase("Berri", "Claude")); document.add(Chunk.NEWLINE); document.add(createDirectorPhrase("Bielinsky", "Fab")); document.add(Chunk.NEWLINE); document.add(createDirectorPhrase("Cameron", "Gaston")); document.add(Chunk.NEWLINE); document.add(createDirectorPhrase("Beineix", "Jean-Jacques")); document.add(Chunk.NEWLINE); // step 5 document.close(); } /** * Main method. * * @param args no arguments needed * @throws DocumentException * @throwsIOException * @throws SQLException */ public static void main(String[] args) throws IOException, DocumentException, SQLException { new DirectorPhrases2().createPdf(RESULT); } }
Paragraph: a Phrase with extra attributes and new lines
Distribute text on different lines
List: a series of ListItem
DrawInterface: vertical position markers, separators and tabs
2.3 Add Anchor, Image, Chapter and Section objects
Anchor: internal and external links
Chapter and Section: Free Bookmarks
Image: Add raster format illustrations