Java PDF: Créer une image en utilisant un template

Author:

Java PDF: Créer une image en utilisant un template
{filelink=10659}

/***** Code de MesExemples.com *******/
/**
 * @(#)TemplateImagesPDF.java
 *
 *
 * @author 
 *sakoba(java.mesexemples.com) @version 1.00 2013/5/29
 */



import java.io.FileOutputStream;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class TemplateImagesPDF {
  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:/TemplateImagesPDF.pdf"));
      document.open();

      PdfTemplate template = pdf_writer.getDirectContent().createTemplate(20, 
    20);
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, 
    BaseFont.NOT_EMBEDDED);
      String text = "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);
      template.setHeight(size + 2);
      
      Image img = Image.getInstance(template);
      img.setRotationDegrees(90);
      document.add(new Phrase(new Chunk(img, 0, 0)));

      document.close();
    } catch (Exception de) {
      System.err.println(de.getMessage());
    }
  }
}

Leave a Reply

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