Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java PDF 2D: Dessiner un string dans le document PDF

Java PDF 2D:  Dessiner un string dans le document PDF
{filelink=10440}

/***** Code de MesExemples.com *******/
/**
 * @(#)G2DDessinerStringPDF.java
 *
 *
 * @author 
 *sakoba(java.mesexemples.com) @version 1.00 2013/4/30
 */
 
 
import java.awt.Graphics2D;
import java.io.FileOutputStream;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.FontFactory;
import com.itextpdf.awt.DefaultFontMapper;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
 
public class G2DDessinerStringPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, 
      	new FileOutputStream("c:/G2DDessinerStringPDF.pdf"));
      document.open();
      DefaultFontMapper mapper = new DefaultFontMapper();
      FontFactory.registerDirectories();
      mapper.insertDirectory("c:windowsfonts");
 
      int w = 150;
      int h = 150;
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate tp = cb.createTemplate(w, h);
      Graphics2D g2 = tp.createGraphics(w, h, mapper);
 
      g2.drawString("Bienvenu", 20, 20);
 
      g2.dispose();
      cb.addTemplate(tp, 50, 400);
 
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
 
}