Java Tutorial/PDF/Graphics2D

Материал из Java эксперт
Перейти к: навигация, поиск

Create Graphics2D from PdfTemplate

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();
  }
}





Draw with Graphics2D

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();
  }
}





Text with an absolute position using java.awt.Graphics2D object

import java.awt.Graphics2D;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "h.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    Graphics2D graphics2D = cb.createGraphics(PageSize.A4.width(), PageSize.A4
        .height());
    graphics2D.drawString("Hello World", 36, 54);
    graphics2D.dispose();
    document.close();
  }
}





Use java.awt.Font to draw strings

import java.awt.Graphics2D;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.AsianFontMapper;
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 = "\u5e73\u548C";
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 50);
    AsianFontMapper mapper = new AsianFontMapper(AsianFontMapper.JapaneseFont_Min,
        AsianFontMapper.JapaneseEncoding_H);
    Graphics2D g2 = tp.createGraphics(100, 50, mapper);
    java.awt.Font font = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 12);
    g2.setFont(font);
    g2.drawString(text, 0, 40);
    g2.dispose();
    cb.addTemplate(tp, 36, 780);
    document.close();
  }
}