Java/PDF RTF/Image Mask

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

Image Masks Demo

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class ImageMasksPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ImageMasksPDF.pdf"));
      document.open();
      Paragraph p = new Paragraph("Some text behind a masked image.");
      for(int i=0;i<50;i++){
        document.add(p);  
      }
      
      PdfContentByte cb = writer.getDirectContent();
      byte maskr[] = { (byte) 0x77, (byte) 0x77, (byte) 0x00, (byte) 0x00, (byte) 0x00,
          (byte) 0x00, (byte) 0x77, (byte) 0x77 };
      
      Image mask = Image.getInstance(8, 8, 1, 1, maskr);
      mask.makeMask();
      mask.setInvertMask(true);
      
      Image image = Image.getInstance("logo.png");
      image.setImageMask(mask);
      image.setAlignment(Image.TEXTWRAP);
      image.setAbsolutePosition(60, 550);
      cb.addImage(image);
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}





Making Image Masks From Byte Array

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MakingMasksFromByteArrayPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("MakingMasksFromByteArrayPDF.pdf"));
      document.open();
      Paragraph p = new Paragraph("Some text behind a masked image.");
      for(int i=0;i<10;i++){
        document.add(p);  
      }
      
      PdfContentByte cb = writer.getDirectContent();
      byte maskr[] = { (byte) 0x77, (byte) 0x77, (byte) 0x00, (byte) 0x00, (byte) 0x00,
          (byte) 0x00, (byte) 0x77, (byte) 0x77 };
      
      Image mask = Image.getInstance(8, 8, 1, 1, maskr);
      mask.makeMask();
      mask.setInvertMask(true);
      
      cb.setRGBColorFill(255, 0, 0);
      cb.addImage(mask, mask.scaledWidth() * 8, 0, 0, mask.scaledHeight() * 8, 100, 450);
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}