Underline a form field value

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

31st May 2016
iText PDF

Switch code for this example

FillWithUnderline.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 the following question:
  10.  * http://stackoverflow.com/questions/28579382/underline-portion-of-text-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfReader;
  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. import org.xml.sax.Attributes;
  27. import org.xml.sax.InputSource;
  28. import org.xml.sax.SAXException;
  29. import org.xml.sax.helpers.DefaultHandler;
  30.  
  31. import javax.xml.parsers.SAXParser;
  32. import javax.xml.parsers.SAXParserFactory;
  33. import java.io.File;
  34. import java.io.StringReader;
  35.  
  36. @Category(SampleTest.class)
  37. public class FillWithUnderline extends GenericTest {
  38.     public static final String DEST = "./target/test/resources/sandbox/acroforms/fill_with_underline.pdf";
  39.     public static final String SRC = "./src/test/resources/pdfs/form.pdf";
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new FillWithUnderline().manipulatePdf(DEST);
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  50.         Document doc = new Document(pdfDoc);
  51.  
  52.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  53.         form.flattenFields();
  54.  
  55.         Rectangle pos = form.getField("Name").getWidgets().get(0).getRectangle().toRectangle();
  56.  
  57.         SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  58.         InputSource is = new InputSource(new StringReader("Bruno Lowagie"));
  59.         parser.parse(is, new CustomHandler(doc, pos));
  60.  
  61.         pdfDoc.close();
  62.     }
  63.  
  64.  
  65.     private static class CustomHandler extends DefaultHandler {
  66.         protected Document document;
  67.         protected Rectangle position;
  68.         protected Paragraph paragraph;
  69.         protected boolean isUnderlined;
  70.  
  71.         public CustomHandler(Document document, Rectangle position) {
  72.             this.document = document;
  73.             this.position = position;
  74.             paragraph = new Paragraph();
  75.             isUnderlined = false;
  76.         }
  77.  
  78.         /**
  79.          * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
  80.          * java.lang.String, java.lang.String, org.xml.sax.Attributes)
  81.          */
  82.         public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  83.             if ("u".equals(qName)) {
  84.                 isUnderlined = true;
  85.             }
  86.         }
  87.  
  88.         /**
  89.          * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
  90.          * java.lang.String, java.lang.String)
  91.          */
  92.         public void endElement(String uri, String localName, String qName)
  93.                 throws SAXException {
  94.             if ("div".equals(qName)) {
  95.                 document.add(paragraph.setFixedPosition(position.getLeft(), position.getBottom(), position.getWidth()));
  96.                 position.moveDown(20);
  97.                 paragraph = new Paragraph();
  98.             } else if ("u".equals(qName)) {
  99.                 isUnderlined = false;
  100.             }
  101.         }
  102.  
  103.         /**
  104.          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
  105.          */
  106.         public void characters(char[] ch, int start, int length)
  107.                 throws SAXException {
  108.             Text text = new Text(strip(new StringBuffer().append(ch, start, length)));
  109.             if (isUnderlined) {
  110.                 text.setUnderline();
  111.             }
  112.             if (0 != text.getText().length()) {
  113.                 paragraph.add(text);
  114.             }
  115.         }
  116.  
  117.         /**
  118.          * Replaces all the newline characters by a space.
  119.          *
  120.          * @param buf the original StringBuffer
  121.          * @return a String without newlines
  122.          */
  123.         protected String strip(StringBuffer buf) {
  124.             while (buf.length() != 0 && (buf.charAt(0) == '\n' || buf.charAt(0) == '\t')) {
  125.                 buf.deleteCharAt(0);
  126.             }
  127.             while (buf.length() != 0 && (buf.charAt(0) == '\n' || buf.charAt(0) == '\t'))
  128.                 buf.deleteCharAt(buf.length() - 1);
  129.             return buf.toString();
  130.         }
  131.     }
  132. }
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