Java/PDF RTF/Pattern

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

Image Pattern: Shape

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
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 ImagePatternShapePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
          "ImagePatternShapePDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfPatternPainter p = cb.createPattern(30f, 30f, 30f, 30f);
      Image img = Image.getInstance("logo.png");
      p.addImage(img, img.scaledWidth(), 0f, 0f, img.scaledHeight(), 0f, 0f);
      p.setPatternMatrix(1f, 0f, 0f, 1f, 60f, 60f);
      cb.setPatternFill(p);
      cb.circle(150f, 400f, 150f);
      cb.fillStroke();
      
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
  }
}





Pattern by PdfContentByte

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
import com.lowagie.text.pdf.PdfSpotColor;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SpotColor;
public class PatternPdfContentByte {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PatternPdfContentByte.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      
      PdfPatternPainter pat = cb.createPattern(10, 10, null);
      pat.setLineWidth(2);
      pat.moveTo(0, -5);
      pat.lineTo(15, 10);
      pat.stroke();
      
      cb.setPatternFill(pat, Color.red);
      cb.rectangle(100, 400, 30, 210);
      cb.fillStroke();
      
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





PdfPatternPainter Demo

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
import com.lowagie.text.pdf.PdfSpotColor;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SpotColor;
public class Pattern1PDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Pattern1PDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate tp = cb.createTemplate(400, 300);
      
      PdfPatternPainter pat = cb.createPattern(10, 10, null);
      pat.setLineWidth(2);
      pat.moveTo(0, -5);
      pat.lineTo(15, 10);
      pat.stroke();
      
      PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
      SpotColor spot = new SpotColor(spc_cmyk);
      
      tp.setPatternFill(pat, spot, .9f);
      tp.rectangle(0, 0, 40, 30);
      tp.fill();
      cb.addTemplate(tp, 50, 50);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





PdfSpotColor for Pattern

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
import com.lowagie.text.pdf.PdfSpotColor;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SpotColor;
public class PatternPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PatternPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate tp = cb.createTemplate(400, 300);
      
      PdfPatternPainter pat = cb.createPattern(15, 15, null);
      pat.rectangle(5, 5, 5, 5);
      pat.fill();
      
      PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
      SpotColor spot = new SpotColor(spc_cmyk);
      
      tp.setPatternFill(pat, spot, .9f);
      tp.rectangle(0, 0, 40, 30);
      tp.fill();
      cb.addTemplate(tp, 50, 50);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Set Pattern color

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
import com.lowagie.text.pdf.PdfWriter;
public class PatternColorPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PatternColorPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      
      PdfPatternPainter pat = cb.createPattern(10, 10, null);
      pat.setLineWidth(2);
      pat.moveTo(0, -5);
      pat.lineTo(15, 10);
      pat.stroke();
      
      cb.setPatternFill(pat, Color.green);
      cb.rectangle(100, 400, 30, 210);
      cb.fillStroke();
      
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
}





Setting PdfContentByte for PdfPatternPainter

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
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 PatternPatternColorPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PatternPatternColorPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfPatternPainter p = cb.createPattern(60f, 60f, 60f, 60f);
      p.setColorFill(Color.red);
      p.circle(2,2,2);
      p.stroke();
      cb.setPatternFill(p);
      cb.setGrayStroke(0.0f);
      cb.rectangle(20, 20, 280, 120);
      cb.fillStroke();
      
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
  }
}





Shading Pattern

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfShading;
import com.lowagie.text.pdf.PdfShadingPattern;
import com.lowagie.text.pdf.PdfWriter;
public class ShadingPatternPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ShadingPatternPDF.pdf"));
      document.open();
      PdfShading shading = PdfShading.simpleAxial(writer, 100, 100, 400, 100, Color.red, Color.cyan);
      PdfShadingPattern shadingPattern = new PdfShadingPattern(shading);
      
      PdfContentByte cb = writer.getDirectContent();
      
      cb.setShadingFill(shadingPattern);
      cb.circle(50,50,50);
      cb.stroke();      
      PdfShading shadingR = PdfShading.simpleRadial(writer, 200, 500, 50, 300, 500, 100, new Color(
          255, 247, 148), new Color(247, 138, 107), false, false);
      cb.paintShading(shadingR);
      
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}





Shading Pattern Text

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfShading;
import com.lowagie.text.pdf.PdfShadingPattern;
import com.lowagie.text.pdf.PdfWriter;
public class ShadingPatternTextPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ShadingPatternTextPDF.pdf"));
      document.open();
      PdfShading shading = PdfShading.simpleAxial(writer, 100, 100, 400, 100, Color.red, Color.cyan);
      PdfShadingPattern shadingPattern = new PdfShadingPattern(shading);
      
      PdfContentByte cb = writer.getDirectContent();
      BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, false);
      
      cb.setShadingFill(shadingPattern);
      cb.beginText();
      cb.setTextMatrix(100, 100);
      cb.setFontAndSize(bf, 40);
      cb.showText("Look at this text!");
      cb.endText();
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}





Text Pattern Color

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
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 TextPatternColorPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
          "TextPatternColorPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfPatternPainter p = cb.createPattern(6f, 6f, 6f, 6f);
      p.setColorFill(Color.red);
      p.circle(2, 2, 2);
      p.stroke();
      document.add(new Paragraph("asdfasdf", FontFactory.getFont(FontFactory.HELVETICA, 60,
          Font.BOLD, new PatternColor(p))));
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
  }
}





Text Pattern Shape

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
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 TextPatternShapePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
          "TextPatternShapePDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      BaseFont bf = BaseFont.createFont("Times-Roman", "winansi", false);
      PdfPatternPainter p = cb.createPattern(30f, 30f, 30f, 30f);
      p.setGrayFill(0.3f);
      p.setFontAndSize(bf, 12);
      p.beginText();
      p.setTextMatrix(1f, 0f, 0f, 1f, 0f, 0f);
      p.showText("A B C D");
      p.endText();
      p.moveTo(0f, 0f);
      p.lineTo(30f, 30f);
      p.stroke();
      cb.setPatternFill(p);
      cb.circle(150f, 400f, 150f);
      cb.fillStroke();
      
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
  }
}