Java Tutorial/PDF/Stroke

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

close Path Fill Stroke

import java.io.FileOutputStream;
import com.lowagie.text.Document;
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.moveTo(470, 700);
    cb.lineTo(570, 700);
    cb.lineTo(570, 800);
    cb.lineTo(470, 800);
    cb.closePathFillStroke();
    document.close();
  }
}





close Path Stroke

import java.io.FileOutputStream;
import com.lowagie.text.Document;
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.moveTo(140, 700);
    cb.lineTo(240, 700);
    cb.lineTo(240, 800);
    cb.lineTo(140, 800);
    cb.closePathStroke();
    document.close();
  }
}





Color stroke and color fill

import java.io.FileOutputStream;
import com.lowagie.text.Document;
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.moveTo(250, 700);
    cb.lineTo(350, 700);
    cb.lineTo(350, 800);
    cb.lineTo(250, 800);
    cb.fill();
    document.close();
  }
}





eoFillStroke method

import java.io.FileOutputStream;
import com.lowagie.text.Document;
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();
    constructStar(cb, 30, 720);
    cb.eoFillStroke();
    document.close();
  }
  public static void constructStar(PdfContentByte cb, float x, float y) {
    cb.moveTo(x + 10, y);
    cb.lineTo(x + 80, y + 60);
    cb.lineTo(x, y + 60);
    cb.lineTo(x + 70, y);
    cb.lineTo(x + 40, y + 90);
    cb.closePath();
  }
}





fill Stroke

import java.io.FileOutputStream;
import com.lowagie.text.Document;
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.moveTo(360, 700);
    cb.lineTo(460, 700);
    cb.lineTo(460, 800);
    cb.lineTo(360, 800);
    cb.fillStroke();
    document.close();
  }
}





Finish a stroke

import java.io.FileOutputStream;
import com.lowagie.text.Document;
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.moveTo(30, 700);
    cb.lineTo(130, 700);
    cb.lineTo(130, 800);
    cb.lineTo(30, 800);
    cb.stroke();
    document.close();
  }
}





Use different colors to fill

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.pdf.CMYKColor;
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 document = new Document();
      PdfWriter writer = PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      cb.setColorFill(new GrayColor(0));
      cb.rectangle(36, 770, 36, 36);
      cb.fillStroke();
      cb.setGrayFill(1);
      cb.rectangle(470, 716, 36, 36);
      cb.fillStroke();
      cb.setColorFill(new Color(1f, 1f, 1f));
      cb.rectangle(416, 662, 36, 36);
      cb.fillStroke();
      cb.setRGBColorFillF(1f, 1f, 1f);
      cb.rectangle(416, 608, 36, 36);
      cb.fillStroke();
      cb.setColorFill(new CMYKColor(0f, 0f, 0f, 1f));
      cb.rectangle(416, 554, 36, 36);
      cb.fillStroke();
      cb.setCMYKColorFillF(0f, 0f, 0f, 1f);
      cb.rectangle(416, 500, 36, 36);
      cb.fillStroke();
    document.close();
  }
}