• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

iText Watermark position issue

Explorer ,
Apr 10, 2008 Apr 10, 2008

Copy link to clipboard

Copied

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):


I hope this isn't confusing. Thanks in advance for everyone's assistance.
TOPICS
Advanced techniques

Views

5.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Apr 30, 2008 Apr 30, 2008
Lumpia wrote:
I must be missing/not understanding something as I have incorporated your code (Thanks)

Yes, I think so. The code was just a straight translation of your java snippet. As mentioned, you would need to make additional changes if you were to try using a PdfWriter (instead of a stamper) to add the watermarks. However, to just read the existing dimensions, you should not need a writer at all.

Obtain the media/crop box sizes from the reader (like in my original example). Then use the...

Votes

Translate

Translate
Explorer ,
Apr 22, 2008 Apr 22, 2008

Copy link to clipboard

Copied

Ok, it appears that what I need to do is implement the use of iText's mediabox and cropbox. Has anyone successfully used mediabox and cropbox within a cfscript tag? Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 23, 2008 Apr 23, 2008

Copy link to clipboard

Copied

What issue are you having with it? Using your code as a base, this simple example displays the cropbox information for each page.

<cfscript>
pdfReader = loader2.create("com.lowagie.text.pdf.PdfReader");
pdfReader.init(arguments.pdfFileIn);
n = pdfReader.getNumberOfPages();

for (ii = 1; ii LTE n; ii = ii + 1)
{
// display crop box for each page
box = pdfReader.getCropBox(ii);
WriteOutput(" PAGE "& ii &" (crop box): ");
WriteOutput(" Top="& box.getTop());
WriteOutput(" Right="& box.getRight());
WriteOutput(" Bottom="& box.getBottom());
WriteOutput(" Left="& box.getLeft());
WriteOutput(" Width="& box.getWidth());
WriteOutput(" Height="& box.getHeight() &"<br>");
}
</cfscript>


Having nothing to do with watermarks, I did notice two things that might cause other problems.

Lumpia wrote:
//create the loader
loader2 = createObject("component", "javaloader.JavaLoader").init(paths);


There is a bug in ColdFusion that can cause a memory leak when using java.net.URLClassLoader. So it is recommended that you store the javaLoader in the server scope rather than instantiating it each time it is used.
http://www.transfer-orm.com/?action=displayPost&ID=212


document = loader2.create("com.lowagie.text.Document");
streamOut.init(arguments.pdfFileOut);

...
pdfStamper.close();


Consider structuring your try/catch code so it always closes document, outputstream and stamper objects. Even if an error occurs. Sort of like a try/catch/finally clause. IMO it is a good practice that can prevent locked files and other io related problems.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 24, 2008 Apr 24, 2008

Copy link to clipboard

Copied

Cfsearching,

Thanks for your many advices. Regarding the memory leak issue, I am going to incorporate that now. I figure the way to add it to server scope with UUID would be like so:

server.uuid.loader = createObject("component", "javaloader.JavaLoader").init(paths);

However, for each referencing object, would I also add server.uuid on them, like so:

pdfCopy=server.uuid.loader.create("com.lowagie.text.pdf.PdfCopy");
pdfReader=server.uuid.loader.create("com.lowagie.text.pdf.PdfReader");
pageSize=server.uuid.loader.create("com.lowagie.text.PageSize").init();
bookMark=server.uuid.loader.create("com.lowagie.text.pdf.SimpleBookmark");
pdfDocument=server.uuid.loader.create("com.lowagie.text.Document");

In reference to media & crop box, I've been reading 'iText In Action' manual and it has this example:

Document document = new Document(new Rectangle(432, 792));
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("page_boundaries.pdf"));
writer.setCropBoxSize(new Rectangle(5, 5, 427, 787));

which is written in java, of course, so I'm having troubles converting it to use in cfscript and also knowing where to place it in my current code.

Thanks again for your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 24, 2008 Apr 24, 2008

Copy link to clipboard

Copied

Lumpia wrote:
I figure the way to add it to server scope with UUID would be like so: ...

Almost. Just make sure the server key for the javaloader is unique. I usually create a UUID with the CreateUUID function and hardcode that value into my Application.cfc file. The key is used when instantiating the javaLoader into the server scope. I then make the key avaliable to all pages by setting a variable in the OnRequest function. Though you could also use Request variables if you prefer. See the attached example.

In reference to media & crop box, I've been reading 'iText In Action' manual and it has this example:

Document document = new Document(new Rectangle(432, 792));
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("page_boundaries.pdf"));
writer.setCropBoxSize(new Rectangle(5, 5, 427, 787));


Well, I do not think you want to set the cropBox so much as use its dimensions to determine where to position the watermark. At least that is the impression I am getting. Do you have an example of one of the pdfs in which the watermark gets set to the middle of the page?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2008 Apr 29, 2008

Copy link to clipboard

Copied

cfSearching,

Thanks for the code for server scope. I have implemented it and it works great.

Well, I do not think you want to set the cropBox so much as use its dimensions to determine where to position the watermark. At least that is the impression I am getting. Do you have an example of one of the pdfs in which the watermark gets set to the middle of the page?

My understanding of the cropbox is that it sets the visable area, whereas the mediabox sets the print area; therefore, it would appear I need them both set. I didn't change the numbers in the example, as I don't quite know how to change the java code to work in Coldfusion.

Unfortunately, I tried to look for an example pdf file to show, but all of them have proprietary information on them I cannot share publically. I also attempted to create one in hopes to replicate the affected, but it did not work.

Would you be able to convert the java code to Coldfusion in my current code? It's the last brick needed to complete my house; otherwise, it comes tumbling down.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 29, 2008 Apr 29, 2008

Copy link to clipboard

Copied

Lumpia wrote:
My understanding of the cropbox is that it sets the visable area, whereas the mediabox sets the print area; therefore, it would appear I need them both set.

Well, you certainly could set both but I am still wondering whether you need to actually set the values or just read them. In other words, are you trying to read the existing values so you know where to position the image, or do you always want to overwrite the existing settings? If all of the pdf's are a known and fixed size overwriting should be okay. However, if the code just arbitrarily sets the media/cropbox sizes without knowledge of any existing settings, the results might be unexpected.

The conversion of the java snippet you posted is below. If you are going to use PdfWriter in your code, I suspect you will need to make some additional changes. Unfortunately I do not have time to work it into your example, but this should be enough to get you started.

(BTW, you should also update the function code to VAR scope the local function variables)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 30, 2008 Apr 30, 2008

Copy link to clipboard

Copied

Well, you certainly could set both but I am still wondering whether you need to actually set the values or just read them. In other words, are you trying to read the existing values so you know where to position the image, or do you always want to overwrite the existing settings? If all of the pdf's are a known and fixed size overwriting should be okay. However, if the code just arbitrarily sets the media/cropbox sizes without knowledge of any existing settings, the results might be unexpected.

I believe I am just trying to read the existing values so I my site will know where to position the image; as all the pdf's are not a fixed size. I must be missing/not understanding something as I have incorporated your code (Thanks), but it did not make a change. I've been over the iText documentation many times now, but can't seem to find my error. I also used re-modified my code so that the cropbox and mediabox adjust for a 8.5" X 11" page (per this site: http://itext.ugent.be/library/question.php?id=588) but the watermark still stays in the center. Perhaps this what you were referring to when you said results might be unexpected.

I've attached my (full) altered code so hopefully you or someone may find the flaws. Note that the first part of the code actually merges documents, and the 2nd part applies the watermark. Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 30, 2008 Apr 30, 2008

Copy link to clipboard

Copied

Lumpia wrote:
I must be missing/not understanding something as I have incorporated your code (Thanks)

Yes, I think so. The code was just a straight translation of your java snippet. As mentioned, you would need to make additional changes if you were to try using a PdfWriter (instead of a stamper) to add the watermarks. However, to just read the existing dimensions, you should not need a writer at all.

Obtain the media/crop box sizes from the reader (like in my original example). Then use the box values to calculate the desired x and y coordinates of the watermark.

for (ii = 1; ii LT n; ii = ii + 1) {
box = pdfReader.getCropBox( javacast("int", ii) );
under = pdfStamper.getUnderContent( javaCast("int", ii) );
// xOffset and yOffset being the desired left an top margin
// ie start drawing the image x points from the left of the crop box and y points from the top
xPos = box.getLeft() + xOffset;
yPos = box.getTop() - yOffset - img.getHeight();
img.setAbsolutePosition( xPos, yPos );
....
under.addImage(img);

}

However, IIRC those methods do not take into account rotation. So that is something you will have to look into yourself. Disclaimer: It has been a long day. The mind is tired. Take this all with a grain of salt ;-)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 01, 2008 May 01, 2008

Copy link to clipboard

Copied

cfSearching,

Thanks! After modifying your code slightly, it now applies the watermark in the correct position based on the size of the pdf page. I do have one problem however. The way my watermark function works, is that it takes the following arguments (pdfFileIn, imageFile, pdfFileOut, xPos, yPos, zIndex) and assigns values based on my input. The way my site works is that the user can put checkmarks in all the pdfs they want merged and/or have watermark(s) applied. There are 2 dropdown menus to select watermarks; in which, the user can choose 1 or both to have applied to the top of the page.

With your newly added code, it appears that it now disregards my xPos and yPos (shown below in the function) and uses the newly added cropbox code for the coordinates.

<cfscript>
function insertWatermarkPDF(pdfFileIn, imageFile, pdfFileOut, xPos, yPos, zIndex)
{
...
}
</cfscript>

<!--- IMAGE1 WAS CHOSEN --->
<cfif form.image1 neq "">
<cfoutput>#insertWatermarkPDF("concatenation.pdf"
,"#form.image1#.gif"
,"concatenation2.pdf"
,158
,743
,1)#
</cfoutput>
</cfif>

<!--- IMAGE2 WAS CHOSEN --->
<cfif form.image2 neq "">
<cfoutput>#insertWatermarkPDF("concatenation2.pdf"
,"#form.image2#.gif"
,"concatenation3.pdf"
,158
,700
,1)#
</cfoutput>
</cfif>

<!--- OPEN ALTERED PDF FILE --->
<cfif IsDefined("form.image2") and form.image2 neq ""> <!--- USER CHOSE IMAGE2 FROM DROPDOWN --->
<cflocation url="concatenation3.pdf">
<cfelse> <!--- IMAGE1 WAS CHOSEN --->
<cflocation url="concatenation2.pdf">
</cfif>


So what ends up occuring is that both images are applied in the exact same postion (upper left), but overlapping each other. It makes sense why it is using the newly added codes xPos and yPos, but I am wondering now how to let it account for 2 images in 2 different yPos coordinates.

Thanks again! This is working out great and the circle is almost complete!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 30, 2008 Apr 30, 2008

Copy link to clipboard

Copied

Lumpia, if you're on CF8 you can use the cfpdf tag to merge PDFs in one line of code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 01, 2008 May 01, 2008

Copy link to clipboard

Copied

BKBK,

True. Unfortunately, my company is still using CFMX7 until next year. If only...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2008 May 01, 2008

Copy link to clipboard

Copied

I see you're using the original version of PaulH's PDF concatenation code. There has since been at least one important modification to it. For example, the master array becomes a vector. See the contribution from -==cfSearching==- on the 4th page of the thread.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 01, 2008 May 01, 2008

Copy link to clipboard

Copied

BKBK,

Thanks for your tip and for looking at my code. I have incorporated the change from cfSearchings post into my code and it works great. This forum really is a handy tool to get assistance.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 01, 2008 May 01, 2008

Copy link to clipboard

Copied

Lumpia wrote:

<cfif form.image1 neq "">
<cfoutput>#insertWatermarkPDF("concatenation.pdf"
,"#form.image1#.gif"
,"concatenation2.pdf"
,158
,743
,1)#
</cfoutput>
</cfif>


Can you post your latest function code for adding the watermark? Because I do not think my previous code snippet would work with those xPos, yPos values (158, 743). They look like absolute positions.

The idea with my previous example was to pass in the desired offsets and let the function determine what the x and y coordinates should be. In other words, you tell the function the watermark should be positioned 1/2 inch from the left (xOffset) and 3/4 of an inch from the top (yOffset) of the crop box. The function then uses that information to calculate the x and y coordinates needed to achieve this.

By using offset values, instead of coordinates, you should be able to position the images correctly simply by changing the x/yOffset parameters. But I would have to see the actual code to know why it is not behaving as you expect.




Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2008 May 03, 2008

Copy link to clipboard

Copied

cfSearching,

You are correct. The way I originally coding this site was with absolute positions, but obviously, my method did not take into account the mediabox and cropbox sizes and adjust accordingly. You are definitely on the right track, and I'm not opposed to taking the absolute postions out and just using the java offsets instead, I really just need it to work with 2 images/coordinates at this point (instead of overlapping each other). Thanks again for your help.

Here is the entire Watermark function code:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 03, 2008 May 03, 2008

Copy link to clipboard

Copied

Lumpia wrote:
xPos = box.getLeft() + 160;
yPos = box.getTop() - 20 - img.getHeight();


The code is drawing both images in the same place, because that is what you are telling it do 😉 The values are hard coded.to always draw the image 160pt from the left and 20pt from the top. Every time. So when you call the function multiple times, the images overlap because the offsets, and consequently the x and y positions, never change. The xOffset and yOffset must be dynamic, so you can adjust the image positions. Make sense?

Just change the function to accept xOffset/yOffset instead of xPos/yPos. Then use xOffset/yOffset inside your loop, instead of the hard coded values 160 and 20. When you call the function, pass in the desired offsets. Just remember to adjust the values to take into account the width or height of first watermark. Otherwise the images will still overlap.

But again, do not forget to VAR your local function variables to avoid thread/scope problems. Also be sure to fix the code to always close the FileOutputStream or you may have problems accessing the final files.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2008 May 06, 2008

Copy link to clipboard

Copied

cfSearching,

Something appears to be off as it is still not adjusting for 2 different images at different yOffset positions. I'm not getting an error, just a previous pdf merge instead. You stated that this code:

<cfset variables.img = javaLoader.create("com.lowagie.text.Image").getInstance( variables.fullPathToImage1 )>
<cfset variables.yOffset = variables.yOffset + variables.img.getHeight()>

takes into account the size of the first image and readjusts the yOffset accordingly. Essentially, could I not just 2 different yOffsets like so:

<cfset variables.xOffset = 160>
<cfset variables.yOffset = 20>

<cfif len(trim(form.image1)) >
<!--- NOTE - these should be absolute paths --->
<cfset insertWatermarkPDF( fullPathToInputFile
, fullPathToImage1
, fullPathToOutputFile1
, variables.xOffset
, variables.yOffset
, 1
)>
</cfif>

<cfset variables.xOffset = 160>
<cfset variables.yOffset = 30>

<cfif len(trim(form.image2)) >
<!--- NOTE - these should be absolute paths --->
<cfset insertWatermarkPDF( fullPathToOutputFile1
, fullPathToImage2
, fullPathToOutputFile2
, variables.xOffset
, variables.yOffset
, 1
)>
</cfif>

Regardless of how I modify the code, it still wants to overlap the images. Correct me if I'm wrong, but it seems like the code within the function (img.setAbsolutePosition(xPos, yPos)) needs to be adjusted to account if there are 2 images.

for ( ii = 1; ii LTE totalPages; ii = ii + 1)
{
...
box = reader.getCropBox( javacast("int", ii) );
// calculate the x and y postions based on the given offsets
xPos = box.getLeft() + arguments.xOffset;
yPos = box.getTop() - arguments.yOffset - img.getHeight();
img.setAbsolutePosition( xPos, yPos );
..
}

I don't know, it just seems like the problem lies somewhere in there, perhaps an if statement or something. I may be completely off, just a thought. Again, your assistance is ALWAYS appreciated. Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 06, 2008 May 06, 2008

Copy link to clipboard

Copied

LATEST
Lumpia wrote:
Essentially, could I not just 2 different yOffsets like so:

Yes. It should work as long as the values correctly account for the size of the first image. Using
<cfset variables.yOffset = variables.yOffset + variables.img.getHeight()>

Just makes it more dynamic. If you ever changed the watermark images, the code would still work without having to change hard coded values.

Regardless of how I modify the code, it still wants to overlap the images. Correct me if I'm wrong, but it seems like the code within the function (img.setAbsolutePosition(xPos, yPos)) needs to be adjusted to account if there are 2 images.

Yes, you are right in thinking that. However, it is already taken care of by <cfset variables.yOffset = variables.yOffset + variables.img.getHeight()>. That code adjusts the yOffset, right before the second call, so it takes into account the height of the first watermark. Let us say your first watermark has a height of 40. The result should be the same as if you were to adjusted the positions manually like this:

<cfset variables.xOffset = 160>
<!--- Draw the first watermark 20pt from the top of the crop box--->
<cfset variables.yOffset = 20>
<cfset insertWatermarkPDF( ..., variables.xOffset, variables.yOffset, ...)>

<cfset variables.xOffset = 160>
<!--- Draw the second watermark 60pt from the top of the crop box--->
<!--- ie yOffset = 20 + 40 (image height) --->
<cfset variables.yOffset = 60>
<cfset insertWatermarkPDF( ..., variables.xOffset, variables.yOffset, ...)>


Try the attached example. Download these (2) sample files from the iText site. Save them to the same directory as your CFM script. You could use different files, but this way we know you will get the same results.

http://itext.ugent.be/library/com/lowagie/examples/general/copystamp/ChapterSection.pdf
http://itext.ugent.be/library/com/lowagie/examples/general/copystamp/watermark.jpg

Run the example then open the file named "ChapterSectionFinal.pdf". You should see that the second watermark is placed below the first one.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation