Colored text

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

25th October 2015
admin-marketing

Switch code for this example

ColoredLetters.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27080565/itext-make-a-single-letter-bold-within-a-word
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.Font.FontFamily;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class ColoredLetters {
  23.  
  24.     public static final String DEST = "results/objects/colored_letters.pdf";
  25.    
  26.     public static final Font RED_NORMAL = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, BaseColor.RED);
  27.     public static final Font BLUE_BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
  28.     public static final Font GREEN_ITALIC = new Font(FontFamily.HELVETICA, 12, Font.ITALIC, BaseColor.GREEN);
  29.     public static final Chunk B = new Chunk("b", BLUE_BOLD);
  30.     public static final Chunk G = new Chunk("g", GREEN_ITALIC);
  31.    
  32.     public static void main(String[] args) throws IOException,
  33.             DocumentException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new ColoredLetters().createPdf(DEST);
  37.     }
  38.    
  39.     public void createPdf(String dest) throws IOException, DocumentException {
  40.         Document document = new Document();
  41.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  42.         document.open();
  43.         Paragraph p = new Paragraph();
  44.         String s = "all text is written in red, except the letters b and g; they are written in blue and green.";
  45.         for (int i = 0; i < s.length(); i++) {
  46.             p.add(returnCorrectColor(s.charAt(i)));
  47.         }
  48.         document.add(p);
  49.         document.close();
  50.     }
  51.    
  52.     private Chunk returnCorrectColor(char letter) {
  53.         if (letter == 'b'){
  54.             return B;
  55.         }
  56.         else if(letter == 'g'){
  57.             return G;
  58.         }
  59.         return new Chunk(String.valueOf(letter), RED_NORMAL);
  60.     }
  61. }
ColoredText.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27578497/applying-color-to-strings-in-paragraph-using-itext
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.Font.FontFamily;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.ColumnText;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class ColoredText {
  25.  
  26.     public static final String DEST = "results/objects/colored_text.pdf";
  27.    
  28.    
  29.     public static void main(String[] args) throws IOException,
  30.             DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new ColoredText().createPdf(DEST);
  34.     }
  35.    
  36.     public void createPdf(String dest) throws IOException, DocumentException {
  37.         Document document = new Document();
  38.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  39.         document.open();
  40.         Font red = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, BaseColor.RED);
  41.         Chunk redText = new Chunk("This text is red. ", red);
  42.         Font blue = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
  43.         Chunk blueText = new Chunk("This text is blue and bold. ", blue);
  44.         Font green = new Font(FontFamily.HELVETICA, 12, Font.ITALIC, BaseColor.GREEN);
  45.         Chunk greenText = new Chunk("This text is green and italic. ", green);
  46.        
  47.         Paragraph p1 = new Paragraph(redText);
  48.         document.add(p1);
  49.         Paragraph p2 = new Paragraph();
  50.         p2.add(blueText);
  51.         p2.add(greenText);
  52.         document.add(p2);
  53.        
  54.         ColumnText ct = new ColumnText(writer.getDirectContent());
  55.         ct.setSimpleColumn(new Rectangle(36, 600, 144, 760));
  56.         ct.addElement(p1);
  57.         ct.addElement(p2);
  58.         ct.go();
  59.        
  60.         document.close();
  61.     }
  62. }
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