Java Tutorial/PDF/Pattern

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create and use PdfGState

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfShading;
import com.lowagie.text.pdf.PdfShadingPattern;
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;
    pictureBackdrop(gap, 500, cb, writer);
    PdfTemplate tp;
    PdfTransparencyGroup group;
    tp = cb.createTemplate(200, 200);
    pictureCircles(0, 0, tp);
    group = new PdfTransparencyGroup();
    group.setIsolated(true);
    group.setKnockout(true);
    tp.setGroup(group);
    cb.addTemplate(tp, gap, 500);
    document.close();
  }
  public static void pictureBackdrop(float x, float y, PdfContentByte cb, PdfWriter writer) {
    PdfShading axial = PdfShading.simpleAxial(writer, x, y, x + 200, y, Color.yellow, Color.red);
    PdfShadingPattern axialPattern = new PdfShadingPattern(axial);
    cb.setShadingFill(axialPattern);
    cb.setColorStroke(Color.black);
    cb.setLineWidth(2);
    cb.rectangle(x, y, 200, 200);
    cb.fillStroke();
  }
  public static void pictureCircles(float x, float y, PdfContentByte cb) {
    PdfGState gs = new PdfGState();
    gs.setBlendMode(PdfGState.BM_MULTIPLY);
    gs.setFillOpacity(1f);
    cb.setGState(gs);
    cb.setColorFill(new CMYKColor(0f, 0f, 0f, 0.15f));
    cb.circle(x + 125, y + 75, 70);
    cb.fill();
    cb.circle(x + 125, y + 125, 70);
    cb.fill();
  }
}





Line pattern

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PatternColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
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();
    
    
    PdfPatternPainter line = cb.createPattern(5, 10, null);
    line.setLineWidth(1);
    line.moveTo(3, -1);
    line.lineTo(3, 11);
    line.stroke();
    PatternColor lines = new PatternColor(line);
    cb.setColorFill(lines);
    cb.rectangle(360, 716, 72, 72);
    cb.fillStroke();
    
    document.close();
  }
}





Pattern of ellipses

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PatternColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
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();
    
    
    PdfPatternPainter ellipse = cb.createPattern(15, 10, 20, 25);
    ellipse.setColorFill(new Color(0xFF, 0xFF, 0x00));
    ellipse.setColorStroke(new Color(0xFF, 0x00, 0x00));
    ellipse.ellipse(2f, 2f, 13f, 8f);
    ellipse.fillStroke();
    PatternColor ellipses = new PatternColor(ellipse);
    cb.setColorFill(ellipses);
    cb.rectangle(144, 716, 72, 72);
    cb.fillStroke();
    
    document.close();
  }
}





Pdf Pattern Painter

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PatternColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
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();
    
    
    PdfPatternPainter circle = cb.createPattern(15, 15, 10, 20, Color.blue);
    circle.circle(7.5f, 7.5f, 2.5f);
    circle.fill();
    PatternColor circles = new PatternColor(circle);
    cb.setColorFill(circles);
    cb.rectangle(252, 716, 72, 72);
    cb.fillStroke();
    
    document.close();
  }
}





Square pattern

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PatternColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
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();
    
    
    PdfPatternPainter square = cb.createPattern(15, 15);
    square.setColorFill(new Color(0xFF, 0xFF, 0x00));
    square.setColorStroke(new Color(0xFF, 0x00, 0x00));
    square.rectangle(5, 5, 5, 5);
    square.fillStroke();
    PatternColor squares = new PatternColor(square);
    cb.setColorFill(squares);
    cb.rectangle(36, 716, 72, 72);
    cb.fillStroke();
    
    document.close();
  }
}





Use SpotColor

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.PdfSpotColor;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SpotColor;
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();
      PdfSpotColor psc_g = new PdfSpotColor("iTextSpotColorGray", 0.5f,
          new GrayColor(0.9f));
      PdfSpotColor psc_rgb = new PdfSpotColor("iTextSpotColorRGB", 0.9f,
          new Color(0x64, 0x95, 0xed));
      PdfSpotColor psc_cmyk = new PdfSpotColor("iTextSpotColorCMYK",
          0.25f, new CMYKColor(0.3f, .9f, .3f, .1f));
      SpotColor sc_g = new SpotColor(psc_g);
      SpotColor sc_rgb1 = new SpotColor(psc_rgb, 0.1f);
      SpotColor sc_cmyk = new SpotColor(psc_cmyk);
      cb.setColorFill(sc_g);
      cb.rectangle(36, 770, 36, 36);
      cb.fillStroke();
      cb.setColorFill(psc_g, psc_g.getTint());
      cb.rectangle(90, 770, 36, 36);
      cb.fillStroke();
      cb.setColorFill(psc_g, 1);
      cb.rectangle(252, 770, 36, 36);
      cb.fillStroke();
      cb.setColorFill(sc_rgb1);
      cb.rectangle(36, 716, 36, 36);
      cb.fillStroke();
      cb.setColorFill(psc_rgb, 0.9f);
      cb.rectangle(36, 662, 36, 36);
      cb.fillStroke();
      cb.setColorFill(sc_cmyk);
      cb.rectangle(36, 608, 36, 36);
      cb.fillStroke();
      cb.setColorFill(psc_cmyk, psc_cmyk.getTint());
      cb.rectangle(90, 608, 36, 36);
      cb.fillStroke();
    document.close();
  }
}