import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class SoftMaskTest_01 {
    public static void main(String[] args) {
        Document document = new Document(new Rectangle(0, 0, 400, 300));
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("SoftMaskTest_01.pdf"));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            
            //Prepare photo
            Image myPhoto = Image.getInstance("myPhoto.jpg");
            myPhoto.setAbsolutePosition(0, 0);
            cb.addImage(myPhoto);

            //Prepare gradation list
            int gradationStep = 40;
            float darkEndRatio = 1.0f;
            float lightEndRatio = 0.0f;
            float incrementAngle = 90.0f / gradationStep;
            float[] gradationRatioList = new float[gradationStep];
            double currentBaseRatio;
            double currentGrayRatio;
            for(int i=0; i<gradationStep; i++) {
                currentBaseRatio = Math.sin(Math.toRadians(incrementAngle * (i+1)));
                currentGrayRatio = currentBaseRatio * (darkEndRatio - lightEndRatio);
                gradationRatioList[i] = (float)(currentGrayRatio);
            }
            
            //Create template
            PdfTemplate template = cb.createTemplate(400, 300);
            
            //Prepare transparent group
            PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
            transGroup.put( PdfName.CS, PdfName.DEVICEGRAY);
            transGroup.setIsolated(true);
            transGroup.setKnockout(false);
            template.setGroup(transGroup);
            
            //Prepare graphic state
            PdfGState gState = new PdfGState();
            PdfDictionary maskDict = new PdfDictionary();
            maskDict.put( PdfName.TYPE, PdfName.MASK );
            maskDict.put( PdfName.S, new PdfName( "Luminosity" ) );
            maskDict.put( new PdfName("G"), template.getIndirectReference() );
            gState.put( PdfName.SMASK, maskDict );
            cb.setGState(gState);
            
            //Clip outside of the ellipse
            float ellipseStartPosX = 50.0f;
            float ellipseStartPosY = 0.0f;
            float ellipseWidth = 300.0f;
            float ellipseHeight = 300.0f;
            template.ellipse(ellipseStartPosX, ellipseStartPosY, ellipseStartPosX + ellipseWidth, ellipseStartPosY + ellipseHeight);
            template.clip();
            template.newPath();
            
            //Fill ellipse
            template.setGrayFill(1.0f);
            template.ellipse(ellipseStartPosX, ellipseStartPosY, ellipseStartPosX + ellipseWidth, ellipseStartPosY + ellipseHeight);
            template.fill();
            
            //Create gradation for mask
            float firstLineWidth = 1.0f;
            for(int i=1; i<gradationStep + 1; i++) {
                template.setLineWidth(firstLineWidth * (gradationStep + 1 - i));
                template.setGrayStroke(gradationRatioList[gradationStep - i]);
                template.ellipse(ellipseStartPosX, ellipseStartPosY, ellipseStartPosX + ellipseWidth, ellipseStartPosY + ellipseHeight);
                template.stroke();
            }
            
            //Place template
            cb.addTemplate( template, 0, 0);
            
            
            //PDF全体表示の設定
            PdfAction action = PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FITV), writer);
            writer.setOpenAction(action);
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        
        document.close();
    }
}
