Set Word Spacing : Text « PDF « Java Tutorial






import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfTextArray;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
      PdfWriter writer = PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      String text = "java2s";
      PdfContentByte cb = writer.getDirectContent();
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
          BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
      cb.beginText();
      cb.moveText(36, 806);
      cb.setFontAndSize(bf, 24);
      cb.moveTextWithLeading(0, -36);
      cb.showText(text);
      cb.newlineText();
      
      PdfTextArray array = new PdfTextArray("J");
      array.add(120);
      array.add("a");
      array.add(120);
      array.add("v");
      array.add(95);
      array.add("a2s");
      cb.showText(array);
      cb.setWordSpacing(50);
      cb.endText();
    document.close();
  }
}








29.12.Text
29.12.1.Add text to an existing Pdf document
29.12.2.Text is added at an absolute position and the stream with the content of a page is not compressed
29.12.3.Chunk text with underline
29.12.4.Text with line above
29.12.5.Text alignment
29.12.6.Text Matrix
29.12.7.Begin text and end text
29.12.8.Create and use PdfTextArray
29.12.9.Set Word Spacing
29.12.10.Set Text Rendering Mode
29.12.11.PdfContentByte.TEXT_RENDER_MODE_STROKE
29.12.12.Draw Unicode string