Hi Sapien, recently I fought against a similar problem during my work and got it straight. Some facts to notice:
1. The mainstream pdf readers only render the cropbox area on screen. Only when you hit the "print" button can you view your document in the mediabox, in your "print preview" screen. This could cause confusion when the cropbox is really smaller than the mediabox. So, make sure you get the coordinates of corners of your cropbox and mediabox and be clear where your watermark should appear. Relative to mediabox or cropbox? Sometimes you may really place your watermark right but only cannot see it on screen. They can be seen when you print your doc off. 2. There are methods likes top(), bottom(), left(), right() you can apply to a PdfRectangle object in the iText API to get the coordinates of your cropbox and mediabox. Hope that helps. Best Regards, Ellen N. Zhao Sapien schrieb: > Thanks for the tip Paulo, this appears to be what I need, and I've done some > investigating into both mediabox and cropbox. I've tried implementing some > examples in my code, but can't seem to get it right. Has anyone > successfully used mediabox and cropbox in Coldfusion within a cfscript tag? > Many thanks. > > > Paulo Soares wrote: > >> You need to take into account the mediabox and the cropbox to place the >> text, placing it always at the same height won't work. >> >> Paulo >> >> ----- Original Message ----- >> From: "Sapien" <[EMAIL PROTECTED]> >> To: <[email protected]> >> Sent: Thursday, April 10, 2008 6:02 PM >> Subject: [iText-questions] iText Watermark position issue >> >> >> >>> Coders, >>> >>> Please forgive me in advance as my java kung foo is not that strong. I >>> have >>> a site which allows users to merge multiple pdf documents they choose via >>> a >>> checklist, and then it will apply a small watermark to the top left >>> (xposition 158, yposition 743). The problem is that there are some pdfs >>> in >>> which the watermark gets set in the middle of the page instead of the top >>> left. I believe I have read in some forums where it may be a size issue >>> of >>> sorts, but I'm still confused. I do know that I have found a weird work >>> around, which involves saving the pdf documents that don't work right, >>> then >>> rescanning via copy machine and having my copy machine send it back to me >>> via email. It will then apply the watermark to the upper left corner. I >>> will attach my code below (note: code is written in coldfusion and java): >>> >>> >>> <!--- BEGIN WATERMARK SCRIPT ---> >>> >>> <cfscript> >>> >>> paths = arrayNew(1); >>> >>> /* >>> This points to the jar we want to load. >>> Could also load a directory of .class files >>> */ >>> paths[1] = expandPath("iText.jar"); >>> >>> //create the loader >>> loader2 = createObject("component", "javaloader.JavaLoader").init(paths); >>> >>> function insertWatermarkPDF(pdfFileIn, imageFile, pdfFileOut, xPos, yPos, >>> zIndex) >>> { >>> // zindex refers to placing the image over or under the content, default >>> is >>> under >>> // but if your content has a background, it may obscure the watermark >>> if (NOT structKeyExists(arguments, "zIndex")) >>> arguments.zIndex = 0; >>> >>> //try >>> //{ >>> document = loader2.create("com.lowagie.text.Document"); >>> document.init(); >>> >>> pdfReader = loader2.create("com.lowagie.text.pdf.PdfReader"); >>> pdfReader.init(arguments.pdfFileIn); >>> >>> // page countING >>> n = pdfReader.getNumberOfPages(); >>> >>> // creae an outputstream >>> streamOut = loader2.create("java.io.FileOutputStream"); >>> streamOut.init(arguments.pdfFileOut); >>> >>> // give it to the pdfstamper >>> pdfStamper = loader2.create("com.lowagie.text.pdf.PdfStamper"); >>> pdfStamper.init(pdfReader, streamOut); >>> >>> // contentbyte is a static object, no constructor >>> under = loader2.create("com.lowagie.text.pdf.PdfContentByte"); >>> >>> // create the Image handler, static object, no constructor >>> Image = loader2.create("com.lowagie.text.Image"); >>> >>> // now create an instance with this file (report_watermark.jpg) >>> img = Image.getInstance(JavaCast("string", arguments.imageFile)); >>> img.setAbsolutePosition(arguments.xPos, arguments.yPos); >>> >>> for (ii = 1; ii LT n; ii = ii + 1) >>> { >>> // pdfStamper supports getUnderContent() and getOverContent() >>> if (arguments.zIndex) >>> under = pdfStamper.getOverContent(JavaCast("int", ii)); >>> else >>> under = pdfStamper.getUnderContent(JavaCast("int", ii)); >>> >>> under.addImage(img); >>> } >>> >>> pdfStamper.close(); >>> >>> return true; >>> //} >>> //catch (any e) >>> //{ >>> // return false; >>> //} >>> } >>> >>> </cfscript> >>> >>> <cfoutput>#insertWatermarkPDF("<PATH REMOVED>\concatenation.pdf" >>> ,"<PATH REMOVED>\n#form.tail#up.gif" >>> ,"<PATH REMOVED>\concatenation2.pdf" >>> ,158 >>> ,743 >>> ,1)# >>> </cfoutput> >>> >>> I hope this isn't confusing. Thanks in advance for everyone's >>> assistance. >>> >> ------------------------------------------------------------------------- >> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference >> Don't miss this year's exciting event. There's still time to save $100. >> Use priority code J8TL2D2. >> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone >> _______________________________________________ >> iText-questions mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/itext-questions >> >> Do you like iText? >> Buy the iText book: http://www.1t3xt.com/docs/book.php >> Or leave a tip: https://tipit.to/itexttipjar >> >> >> > > -- HaCon Ingenieurgesellschaft mbH Lister Str. 15 30163 Hannover Germany/Deutschland Tel. +49 511 33699-363 Fax. +49 511 33699-99 Email: [EMAIL PROTECTED] http://www.hacon.de Registry Court/Amtsgericht Hannover HRB 1712 Managing Directors/Geschäftsführer: Michael Frankenberg, Dr.-Ing. Marian Gaidzik, Dr.-Ing. Werner Kretschmer, Werner Sommerfeld, Dr.-Ing. Volker Sustrate, Peter Talke ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Do you like iText? Buy the iText book: http://www.1t3xt.com/docs/book.php Or leave a tip: https://tipit.to/itexttipjar
