Java PDF: Créer un texte vertical

Author:

Java PDF: Créer un texte vertical
{filelink=10368}

/***** Code de MesExemples.com *******/
/**
 * @(#)TexteVerticalPDF.java
 *
 *
 * @author 
 *sakoba(java.mesexemples.com) @version 1.00 2013/4/26
 */



import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class TexteVerticalPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter pdf_writer = PdfWriter.getInstance(document,
      new FileOutputStream("c:/TexteVerticalPDF.pdf"));
      pdf_writer.setPdfVersion(pdf_writer.PDF_VERSION_1_7);
      document.open();
      PdfTemplate template = pdf_writer.getDirectContent().createTemplate(20, 
    20);
      BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);
      String text = "Texte Vertical";
      float size = 16;
      float width = bf.getWidthPoint(text, size);
      template.beginText();
      template.setFontAndSize(bf, size);
      template.setTextMatrix(0, 2);
      template.showText(text);
      template.endText();
      template.setWidth(width);
       
      Image img = Image.getInstance(template);
      img.setRotationDegrees(90);

      PdfPTable table = new PdfPTable(1);
      table.addCell(new PdfPCell(img));
      document.add(table);
    } catch (Exception err) {
      err.printStackTrace();
    }
    document.close();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *