Java Tutorial/PDF/Shading

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

PdfShading.simpleAxial

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfShading;
import com.lowagie.text.pdf.PdfShadingPattern;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.ShadingColor;
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();
    PdfShading axial = PdfShading.simpleAxial(writer, 36, 716, 396, 788, Color.orange, Color.blue);

    PdfShadingPattern axialPattern = new PdfShadingPattern(axial);
    ShadingColor axialColor = new ShadingColor(axialPattern);
    cb.setColorFill(axialColor);
    cb.rectangle(36, 608, 72, 72);
    cb.rectangle(144, 608, 72, 72);
    cb.rectangle(252, 608, 72, 72);
    cb.rectangle(360, 608, 72, 72);
    cb.fillStroke();
    document.close();
  }
}





PdfShading.simpleRadial

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfShading;
import com.lowagie.text.pdf.PdfShadingPattern;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.ShadingColor;
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();
    PdfShading radial = PdfShading.simpleRadial(writer, 200, 500, 50, 300, 500, 100, new Color(255,
        247, 148), new Color(247, 138, 107), false, false);
    cb.paintShading(radial);

    document.close();
  }
}





Spot Color

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
import com.lowagie.text.pdf.PdfShading;
import com.lowagie.text.pdf.PdfShadingPattern;
import com.lowagie.text.pdf.PdfSpotColor;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.ShadingColor;
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_cmyk = new PdfSpotColor("iTextSpotColorCMYK", 0.25f, new CMYKColor(0.3f, .9f,
        .3f, .1f));
    SpotColor sc_cmyk = new SpotColor(psc_cmyk);
    Image img = Image.getInstance("dog.jpg");
    PdfPatternPainter img_pattern = cb.createPattern(img.scaledWidth(), img.scaledHeight(), img
        .scaledWidth(), img.scaledHeight());
    img_pattern.addImage(img, img.scaledWidth(), 0f, 0f, img.scaledHeight(), 0f, 0f);
    img_pattern.setPatternMatrix(1f, 0f, 0f, 1f, 60f, 60f);
    PdfShading axial = PdfShading.simpleAxial(writer, 36, 716, 396, 788, Color.orange, Color.blue);
    PdfShadingPattern axialPattern = new PdfShadingPattern(axial);
    ShadingColor axialColor = new ShadingColor(axialPattern);
    document.add(new Paragraph("This is a paragraph painted using a shading pattern", new Font(
        Font.HELVETICA, 24, Font.BOLD, axialColor)));
    document.close();
  }
}