Chunk examples

This is a code example of iText PDF, discover more.

25th October 2015
admin-marketing

Switch code for this example

ChunkBackground.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19976343/how-to-set-the-paragraph-of-itext-pdf-file-as-rectangle-with-background-color-in
  4.  *
  5.  * We create a table with two columns and two cells.
  6.  * This way, we can add two images next to each other.
  7.  */
  8. package sandbox.objects;
  9.  
  10. import com.itextpdf.text.BaseColor;
  11. import com.itextpdf.text.Chunk;
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Font;
  15. import com.itextpdf.text.Font.FontFamily;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22.  
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class ChunkBackground {
  27.  
  28.     public static final String DEST = "results/objects/chunk_background.pdf";
  29.    
  30.     public static void main(String[] args) throws IOException,
  31.             DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new ChunkBackground().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         Font f = new Font(FontFamily.TIMES_ROMAN, 25.0f, Font.BOLD, BaseColor.WHITE);
  42.         Chunk c = new Chunk("White text on red background", f);
  43.         c.setBackground(BaseColor.RED);
  44.         Paragraph p = new Paragraph(c);
  45.         document.add(p);
  46.         document.close();
  47.     }
  48. }
SubSuperScript.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24772212/itext-pdf-writer-is-there-any-way-to-allow-unicode-subscript-symbol-in-the-pdf
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.BaseFont;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class SubSuperScript {
  21.  
  22.     public static final String DEST = "results/objects/sub_superscript.pdf";
  23.     public static final String FONT = "resources/fonts/Cardo-Regular.ttf";
  24.    
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new SubSuperScript().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  37.         Font f = new Font(bf, 10);
  38.         Paragraph p = new Paragraph("H\u2082SO\u2074", f);
  39.         document.add(p);
  40.         document.close();
  41.     }
  42. }
OrdinalNumbers.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/30322665/how-to-access-the-sup-tag-in-itextsharp
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class OrdinalNumbers {
  25.     public static final String DEST = "results/objects/ordinal_numbers.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new OrdinalNumbers().createPdf(DEST);
  31.     }
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         Font small = new Font(FontFamily.HELVETICA, 6);
  37.         Chunk st = new Chunk("st", small);
  38.         st.setTextRise(7);
  39.         Chunk nd = new Chunk("nd", small);
  40.         nd.setTextRise(7);
  41.         Chunk rd = new Chunk("rd", small);
  42.         rd.setTextRise(7);
  43.         Chunk th = new Chunk("th", small);
  44.         th.setTextRise(7);
  45.         Paragraph first = new Paragraph();
  46.         first.add("The 1");
  47.         first.add(st);
  48.         first.add(" of May");
  49.         document.add(first);
  50.         Paragraph second = new Paragraph();
  51.         second.add("The 2");
  52.         second.add(nd);
  53.         second.add(" and the 3");
  54.         second.add(rd);
  55.         second.add(" of June");
  56.         document.add(second);
  57.         Paragraph fourth = new Paragraph();
  58.         fourth.add("The 4");
  59.         fourth.add(rd);
  60.         fourth.add(" of July");
  61.         document.add(fourth);
  62.         document.close();
  63.     }
  64. }
StandardDeviation.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/30876926/itext-std-deviation-symbol-%CF%83-not-printing
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class StandardDeviation {
  25.  
  26.     public static final String DEST = "results/objects/standard_deviation.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new StandardDeviation().createPdf(DEST);
  32.     }
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         document.add(new Paragraph("The standard deviation symbol doesn't exist in Helvetica."));
  38.         Font symbol = new Font(FontFamily.SYMBOL);
  39.         Paragraph p = new Paragraph("So we use the Symbol font: ");
  40.         p.add(new Chunk("s", symbol));
  41.         document.add(p);
  42.         document.close();
  43.     }
  44.    
  45. }
Bullets.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/30369587/how-to-write-bulleted-list-in-pdf-using-itext-jar-but-item-should-not-in-next-li
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. /**
  23.  * @author iText
  24.  */
  25. @WrapToTest
  26. public class Bullets {
  27.    
  28.     public static final String DEST = "results/objects/bullets.pdf";
  29.     public static final String[] ITEMS = {
  30.         "Insurance system", "Agent", "Agency", "Agent Enrollment", "Agent Settings",
  31.         "Appointment", "Continuing Education", "Hierarchy", "Recruiting", "Contract",
  32.         "Message", "Correspondence", "Licensing", "Party"
  33.     };
  34.  
  35.     public static void main(String[] args) throws IOException, DocumentException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new Bullets().createPdf(DEST);
  39.     }
  40.    
  41.    
  42.     public void createPdf(String dest) throws DocumentException, IOException {
  43.         Document document = new Document();
  44.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  45.         document.open();
  46.         Font zapfdingbats = new Font(FontFamily.ZAPFDINGBATS, 8);
  47.         Font font = new Font();
  48.         Chunk bullet = new Chunk(String.valueOf((char) 108), zapfdingbats);
  49.        
  50.         Paragraph p = new Paragraph("Items can be split if they don't fit at the end: ", font);
  51.         for (String item: ITEMS) {
  52.             p.add(bullet);
  53.             p.add(new Phrase(" " + item + " ", font));
  54.         }
  55.         document.add(p);
  56.         document.add(Chunk.NEWLINE);
  57.        
  58.         p = new Paragraph("Items can't be split if they don't fit at the end: ", font);
  59.         for (String item: ITEMS) {
  60.             p.add(bullet);
  61.             p.add(new Phrase("\u00a0" + item.replace(' ', '\u00a0') + " ", font));
  62.         }
  63.         document.add(p);
  64.         document.add(Chunk.NEWLINE);
  65.        
  66.         BaseFont bf = BaseFont.createFont("resources/fonts/FreeSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  67.         Font f = new Font(bf, 12);
  68.         p = new Paragraph("Items can't be split if they don't fit at the end: ", f);
  69.         for (String item: ITEMS) {
  70.             p.add(new Phrase("\u2022\u00a0" + item.replace(' ', '\u00a0') + " ", f));
  71.         }
  72.         document.add(p);
  73.        
  74.         document.close();
  75.     }
  76. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now