Highlighting text

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

27th November 2015
admin-marketing

Switch code for this example

HighLightByAddingContent.java
  1. /*
  2.  * This example was written in answer to the question:
  3.  * http://stackoverflow.com/questions/33952183
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfContentByte;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16. /**
  17.  *
  18.  * @author Bruno Lowagie (iText Software)
  19.  */
  20. public class HighLightByAddingContent {
  21.    
  22.     public static final String SRC = "resources/pdfs/hello.pdf";
  23.     public static final String DEST = "results/stamper/hello_highlighted.pdf";
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new HighLightByAddingContent().manipulatePdf(SRC, DEST);
  28.     }
  29.  
  30.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  31.         PdfReader reader = new PdfReader(src);
  32.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  33.         PdfContentByte canvas = stamper.getUnderContent(1);
  34.         canvas.saveState();
  35.         canvas.setColorFill(BaseColor.YELLOW);
  36.         canvas.rectangle(36, 786, 66, 16);
  37.         canvas.fill();
  38.         canvas.restoreState();
  39.         stamper.close();
  40.         reader.close();
  41.     }
  42. }
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