Hi All,
Thanks for the input. Now i changed the code to consider crop box, hurray
now everything is fine. Posting the code for other's reference :).
Many many thanks to 1T3XT info team.
<code>
import java.io.FileOutputStream;
import java.io.*;
import java.util.HashMap;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.*;
public class StampingPDF {
public static void mainTemp(String[] args) {
stampPDF("ImageNotInCenter.
>
> pdf");
> }
>
> public static void stampPDF(String fileName) {
>
> String FILE_NAME = fileName;
>
> try {
> PdfReader reader = new PdfReader(FILE_NAME);
>
> Rectangle r,r1;
> int n = reader.getNumberOfPages();
> System.out.println("The no of pages "+n);
>
> Image img = Image.getInstance("TestLogo.gif");
> System.out.println("height "+img.getHeight()+" width
> "+img.getWidth());
>
> PdfStamper stamp = new PdfStamper(reader, new
> FileOutputStream("stamped"+FILE_NAME));
>
> int i = 0;
> PdfContentByte over;
>
> while (i < n) {
> i++;
> r=reader.getPageSize(i);
> //r=reader.getPageSizeWithRotation(i);
> Rectangle pagesize = reader.getPageSize(i);
> float pgHeight = r.getHeight();
> float pgWidth = r.getWidth();
> System.out.println("Page Number "+i);
> System.out.println("pgWidth "+pgWidth+" pgHeight
> "+pgHeight);
>
> Rectangle dim = reader.getBoxSize(i, "media");
> System.out.println(""+dim);
>
> System.out.println(" URX "+dim.getRight() +" URY
> "+dim.getTop() +" Page no "+i);
> System.out.println(" LLX "+dim.getLeft() +" LLY
> "+dim.getBottom());
>
> //dim.setRight(792);
> //dim.setLeft(0);
> //dim.setTop(612);
> //dim.setBottom(0);
> //System.out.println(" URX "+dim.getRight() +" URY
> "+dim.getTop());
> //System.out.println(" LLX "+dim.getLeft() +" LLY
> "+dim.getBottom());
>
> int pgRotation = reader.getPageRotation(i);
>
> System.out.println("pgRotation "+pgRotation);
>
> over = stamp.getOverContent(i);
> PdfGState gs1 = new PdfGState();
> gs1.setFillOpacity(0.25f);
> over.setGState(gs1);
>
> float X = (pagesize.getLeft() + pagesize.getRight() -
> img.getWidth()) / 2;
> float Y = (pagesize.getTop() + pagesize.getBottom() -
> img.getHeight())/ 2 ;
//New Change Start
> Rectangle dim = reader.getCropBox(i);
> if(dim != null) {
>
X = (dim.getLeft() + dim.getRight() - img.getWidth()) / 2;
> Y = (dim.getTop() + dim.getBottom() - img.getHeight())/ 2
> ;
> }
//New Change End
> System.out.println("X "+X+ " Y "+Y);
> if(pgRotation == 0 || pgRotation == 180) {
>
> over.addImage(img,img.getWidth(),0,0,img.getHeight(),X,Y);
> } else if(pgRotation == 90 || pgRotation == 270) {
>
> over.addImage(img,img.getWidth(),0,0,img.getHeight(),Y,X);
> }
>
>
> }
> stamp.close();
> }
> catch (Exception de) {
> de.printStackTrace();
> }
> }
> }
>
>
> </code>
2009/3/24 tadaka Pandu Rangam <panduj...@gmail.com>
> Hi All,
>
> I am not able to stamp the attached document in the middle. I have
> successfully stamped in the middle for other documents with a media boxes as
> shown
>
> media box pageWidth pageHeight Rotation
> [0,0,612,1008], 1008 612 90
> [0,7268,841,7070], 614 790 0
>
>
> but attached pdf is having mediabox [0,0,612,1224], I tried with changing
> mediabox size to [0,0,612,1008] etc but no luck
>
>
>
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php