Package com.lowagie.examples.objects.images

Source Code of com.lowagie.examples.objects.images.ImageMasks

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.examples.objects.images;

import java.io.FileOutputStream;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.LwgImage;
import com.lowagie.text.LwgPageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

/**
* Applying a mask to an LwgImage.
*/
public class ImageMasks {
   
    /**
     * Applying masks to images.
     * @param args no arguments needed
     */
    public static void main(String[] args) {
        System.out.println("masked images");
       
        LwgDocument document = new LwgDocument(LwgPageSize.A4, 50, 50, 50, 50);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("maskedImages.pdf"));
           
            document.open();
            Paragraph p = new Paragraph("Some text behind a masked image.");
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
           
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            document.add(p);
            PdfContentByte cb = writer.getDirectContent();
            byte maskr[] = {(byte)0x3c, (byte)0x7e, (byte)0xe7, (byte)0xc3, (byte)0xc3, (byte)0xe7, (byte)0x7e, (byte)0x3c};
            LwgImage mask = LwgImage.getInstance(8, 8, 1, 1, maskr);
            mask.makeMask();
            mask.setInverted(true);
            LwgImage image = LwgImage.getInstance("otsoe.jpg");
            image.setImageMask(mask);
            image.setAbsolutePosition(60, 550);
            // explicit masking
            cb.addImage(image);
            // stencil masking
            cb.setRGBColorFill(255, 0, 0);
            cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 450);
            cb.setRGBColorFill(0, 255, 0);
            cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 400);
            cb.setRGBColorFill(0, 0, 255);
            cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 350);
            document.close();
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }

}
TOP

Related Classes of com.lowagie.examples.objects.images.ImageMasks

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.