Colored text

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

31st May 2016
admin-marketing

Switch code for this example

ColoredLetters.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27080565/itext-make-a-single-letter-bold-within-a-word
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.layout.element.Text;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29.  
  30. @Category(SampleTest.class)
  31. public class ColoredLetters extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/objects/colored_letters.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new ColoredLetters().manipulatePdf(DEST);
  38.     }
  39.  
  40.     public void manipulatePdf(String dest) throws IOException {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  42.         Document doc = new Document(pdfDoc);
  43.  
  44.         Paragraph p = new Paragraph();
  45.         String s = "all text is written in red, except the letters b and g; they are written in blue and green.";
  46.         for (int i = 0; i < s.length(); i++) {
  47.             p.add(returnCorrectColor(s.charAt(i)));
  48.         }
  49.         doc.add(p);
  50.  
  51.         doc.close();
  52.     }
  53.  
  54.     private Text returnCorrectColor(char letter) throws IOException {
  55.         if (letter == 'b') {
  56.             return new Text("b")
  57.                     .setFontColor(Color.BLUE)
  58.                     .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD));
  59.         } else if (letter == 'g') {
  60.             return new Text("g")
  61.                     .setFontColor(Color.GREEN)
  62.                     .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA))
  63.                     .setItalic();
  64.         } else {
  65.             return new Text(String.valueOf(letter))
  66.                     .setFontColor(Color.RED)
  67.                     .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA));
  68.         }
  69.     }
  70. }
ColoredText.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27578497/applying-color-to-strings-in-paragraph-using-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.geom.Rectangle;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  21. import com.itextpdf.layout.Canvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.element.Text;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31. import java.io.IOException;
  32.  
  33. @Category(SampleTest.class)
  34. public class ColoredText extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/objects/colored_text.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new ColoredText().manipulatePdf(DEST);
  41.     }
  42.  
  43.     public void manipulatePdf(String dest) throws IOException {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc);
  46.  
  47.         Text redText = new Text("This text is red. ")
  48.                 .setFontColor(Color.RED)
  49.                 .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA));
  50.         Text blueText = new Text("This text is blue and bold. ")
  51.                 .setFontColor(Color.BLUE)
  52.                 .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD));
  53.         Text greenText = new Text("This text is green and italic. ")
  54.                 .setFontColor(Color.GREEN)
  55.                 .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_OBLIQUE));
  56.  
  57.         Paragraph p1 = new Paragraph(redText).setMargin(0);
  58.         doc.add(p1);
  59.         Paragraph p2 = new Paragraph().setMargin(0);
  60.         p2.add(blueText);
  61.         p2.add(greenText);
  62.         doc.add(p2);
  63.  
  64.         new Canvas(new PdfCanvas(pdfDoc.getLastPage()), pdfDoc, new Rectangle(36, 600, 108, 160))
  65.                 .add(p1)
  66.                 .add(p2);
  67.  
  68.         doc.close();
  69.     }
  70. }
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