Image Masks Demo : Image Mask « PDF RTF « Java






Image Masks Demo

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

           
       








itext.zip( 1,748 k)

Related examples in the same category

1.Making Image Masks From Byte ArrayMaking Image Masks From Byte Array