My version of watermarking with iText


import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Image;

import com.itextpdf.text.Rectangle;

import com.itextpdf.text.pdf.PdfContentByte;

import com.itextpdf.text.pdf.PdfGState;

import com.itextpdf.text.pdf.PdfLayer;

import com.itextpdf.text.pdf.PdfReader;

import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;

import java.io.IOException;



/**

 *

 * @author Jaakov Jalink

 * Syneron Medical LTD.

 */

public class addWatermark {



    public static final String inFile = "d:\\ChapterSection.pdf";

    public static final String outFile = "d:\\test.pdf";

    public static final String IMAGE_PRINTED = "D:\\ active stamp.png";



    public static void main(String[] args) throws DocumentException, 
IOException {

        addWatermarkImg(inFile, outFile, IMAGE_PRINTED, 1, 100, true, true, 
true, 10, 10, 0.4f, true, 50, 100);

    }



/**

 * <p> independent function to add watermark to a PDF file </p>

 *

 * @param inFilefName

 * @param outFileName

 * @param imgFileName

 * @param startPage

 * @param endPage

 * @param onTop

 * @param onScreen

 * @param onPrint

 * @param posX

 * @param posY

 * @param Opacity

 * @param scaleOrAbsolute   - true if the sizes are scale, false for absolute 
size

 * @param sizeX

 * @param sizeY

 *

 * @throws IOException

 * @throws DocumentException

 */

    public static void addWatermarkImg(String inFilefName, String outFileName,

            String imgFileName, int startPage, int endPage,

            boolean onTop, boolean onScreen, boolean onPrint,

            float posX, float posY, float Opacity,

            boolean scaleOrAbsolute, float sizeX, float sizeY) throws 
IOException, DocumentException {

        //open the files

        PdfReader reader = new PdfReader(inFilefName);



        PdfStamper stamper = new PdfStamper(reader, new 
FileOutputStream(outFileName));



        Image image1 = Image.getInstance(imgFileName);

        if (scaleOrAbsolute)

            image1.scalePercent(sizeX, sizeY);

        else

            image1.scaleAbsolute(sizeX, sizeY);



        //add watermark layer

        PdfLayer wmLayer = new PdfLayer("WaterMark", stamper.getWriter());

        wmLayer.setOnPanel(true);



        //set layer parameters

        wmLayer.setPrint("print", true);

        wmLayer.setOn(onScreen);

        wmLayer.setView(onScreen);



        //Prepare transperancy

        PdfGState transparent = new PdfGState();

        transparent.setStrokeOpacity(Opacity);

        transparent.setFillOpacity(Opacity);



        PdfContentByte cb;

        int toPage = Math.min(endPage, stamper.getReader().getNumberOfPages());

        for (int i = startPage; i < toPage; i++) {



            if (onTop)

                cb = stamper.getOverContent(i);

            else

                cb = stamper.getUnderContent(i);



            Rectangle rectangle = 
stamper.getReader().getPageSizeWithRotation(i);

            cb.beginLayer(wmLayer);

            cb.setGState(transparent); //set block trasparency properties

            //position relative to top

            float AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - 
image1.getPlainHeight() - posY);



            image1.setAbsolutePosition(posX, AbsoluteY);

            cb.addImage(image1);

            cb.endLayer();

        }

        stamper.close();

    }

}



************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer 
viruses.
************************************************************************************



------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to