Create and use PdfGState : Pattern « PDF « Java Tutorial






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();
  }
}








29.73.Pattern
29.73.1.Square pattern
29.73.2.Pattern of ellipses
29.73.3.Pdf Pattern Painter
29.73.4.Line pattern
29.73.5.Create and use PdfGState
29.73.6.Use SpotColor