Java/PDF RTF/Rectangle

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

Adding Rectangle to a Column

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class AddingRectangleToColumnPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddingRectangleToColumnPDF.pdf"));
      document.open();
      BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
      Font font = new Font(bf, 11, Font.NORMAL);
      Phrase col1 = new Phrase(15, "col1", font);
      Phrase col2 = new Phrase(15, "col2", font);
      Phrase col3 = new Phrase(15, "col3", font);
      for (int i = 0; i < 40; i++) {
        col1.add("col 1\n");
        col2.add("col 2\n");
        col3.add("col 3\n");
      }
      PdfContentByte cb = writer.getDirectContent();
      ColumnText ct = new ColumnText(cb);
      ct.setSimpleColumn(col1, 60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
      ct.go();
      ct.setSimpleColumn(col2, 105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
      ct.go();
      cb.rectangle(53, 295, 52, 8 + 28 * 15);
      cb.stroke();
      ct.setSimpleColumn(col3, 160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
      ct.go();
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Drawing Rectangle

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class DrawRectanglePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("DrawRectanglePDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate template = cb.createTemplate(500, 200);
      template.setLineWidth(2f);
      template.rectangle(2.5f, 2.5f, 495f, 95f);
      template.stroke();
      template.setLineWidth(12f);
      cb.addTemplate(template, 0, 1, -1, 0, 500, 200);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Shape: Rectangle

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class ShapeRectanglePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ShapeRectanglePDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      cb.setLineWidth(10f);
      cb.rectangle(100, 700, 100, 100);
      cb.stroke();
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}