Draw with Graphics2D : Graphics2D « PDF « Java Tutorial






import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {
  public static void main(String[] args) throws Exception {
    int w, h;
    w = 150;
    h = 150;
    Ellipse2D.Double circle = new Ellipse2D.Double(12, 12, 12, 12);

    Document document = new Document(new Rectangle(w, h));

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("sun_tutorial.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    Graphics2D g2 = cb.createGraphics(w, h);

    g2.setColor(Color.green);

    g2.fill(circle);

    g2.dispose();

    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