Java Tutorial/PDF/Circle

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

Draw a circle with PdfContentByte

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.GrayColor;
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.setColorStroke(new GrayColor(0.2f));
    cb.setColorFill(new GrayColor(0.9f));
    cb.circle(70, 770, 40);
    cb.fillStroke();

    document.close();
  }
}





Draw more circles

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfTransparencyGroup;
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();
      PdfContentByte cb = writer.getDirectContent();
      float gap = (document.getPageSize().width() - 400) / 3;
      pictureCircles(gap, 500, cb);
      cb.saveState();
      PdfGState gs1 = new PdfGState();
      gs1.setFillOpacity(0.5f);
      cb.setGState(gs1);
      
      cb.resetRGBColorFill();
      ColumnText ct = new ColumnText(cb);
      Phrase ph = new Phrase("Ungrouped objects\nObject opacity = 1.0");
      ct.setSimpleColumn(ph, gap, 0, gap + 200, 500, 18,
          Element.ALIGN_CENTER);
      ct.go();
      document.close();
  }

  public static void pictureCircles(float x, float y, PdfContentByte cb) {
    cb.setColorFill(Color.red);
    cb.circle(x + 70, y + 70, 50);
    cb.fill();
    cb.setColorFill(Color.yellow);
    cb.circle(x + 100, y + 130, 50);
    cb.fill();
    cb.setColorFill(Color.blue);
    cb.circle(x + 130, y + 70, 50);
    cb.fill();
  }
}