Java Tutorial/PDF/Shape — различия между версиями

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

Текущая версия на 05:21, 1 июня 2010

Draw shape

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.Type3Font;
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();
    Type3Font t3 = new Type3Font(writer, new char[] { " ", "1", "2", "3", "4", "5" }, false);
    PdfContentByte g;
    g = t3.defineGlyph(" ", 300, 0, 0, 600, 1200);
    g = t3.defineGlyph("1", 600, 0, 0, 600, 1200);
    g.moveTo(250, 1200);
    g.lineTo(350, 100);
    g.lineTo(400, 1100);
    g.closePathFillStroke();
    g = t3.defineGlyph("2", 800, 0, 0, 800, 1200);
    g = t3.defineGlyph("3", 1000, 0, 0, 500, 600);
    g.moveTo(250, 1200);
    g.lineTo(350, 100);
    g.lineTo(400, 1100);
    g.closePathFillStroke();
    g.moveTo(650, 1200);
    g.lineTo(750, 100);
    g.lineTo(800, 1100);
    g.closePathFillStroke();
    g = t3.defineGlyph("4", 1200, 0, 0, 600, 600);
    g.closePathFillStroke();
    g = t3.defineGlyph("5", 1200, 0, 0, 1200, 1200);
    g.closePathFillStroke();
    Font font = new Font(t3, 24);
    document.add(new Paragraph("1 2 3 4 5\n5 5 5 5 5 5 5 1", font));
    document.close();
  }
}





Fill shape

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document.rupress = false;
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    cb.circle(26.0f, 50.0f, 25.0f);
    cb.fill();
    cb.saveState();
    cb.setColorFill(Color.yellow);
    cb.circle(26.0f, 50.0f, 20.0f);
    cb.fill();
    cb.saveState();
    cb.setColorFill(Color.red);
    cb.circle(26.0f, 50.0f, 15.0f);
    cb.fill();
    cb.restoreState();
    cb.circle(26.0f, 50.0f, 10.0f);
    cb.fill();
    cb.restoreState();
    cb.circle(26.0f, 50.0f, 5.0f);
    cb.fill();
    document.close();
  }
}





Set shape color

import java.io.FileOutputStream;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("HelloWorldRead.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("HelloWorldStamper.pdf"));
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfContentByte over;
    int total = reader.getNumberOfPages() + 1;
    for (int i = 1; i < total; i++) {
      over = stamper.getOverContent(i);
      over.beginText();
      over.setFontAndSize(bf, 18);
      over.setTextMatrix(30, 30);
      over.showText("page " + i);
      over.endText();
      over.setRGBColorStroke(0xFF, 0x00, 0x00);
      over.setLineWidth(5f);
      over.ellipse(250, 450, 350, 550);
      over.stroke();
    }
    stamper.close();
  }
}