MakingMasksFromByteArrayPDF.java Source code

Java tutorial

Introduction

Here is the source code for MakingMasksFromByteArrayPDF.java

Source

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