Create Graphics2D from PdfTemplate : Graphics2D « PDF « Java Tutorial






import java.awt.Graphics2D;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
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 = "\u0936\u093e\u0902\u0924\u093f";

    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 50);
    Graphics2D g2 = tp.createGraphicsShapes(100, 50);
    java.awt.Font font = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 12);
    g2.setFont(font);
    g2.drawString("Graphics2D: " + text, 0, 40);
    g2.dispose();
    cb.addTemplate(tp, 36, 750);

    document.close();
  }
}








29.42.Graphics2D
29.42.1.Text with an absolute position using java.awt.Graphics2D object
29.42.2.Use java.awt.Font to draw strings
29.42.3.Create Graphics2D from PdfTemplate
29.42.4.Draw with Graphics2D