Avatar of gdkinney_2
gdkinney_2
 asked on

Need a Java Tool to watermark PDF files when they are downloaded by users

Need a Java Tool to watermark PDF files when they are downloaded by users.

I think this should be a free tool that I can implement to watermark pdf files that users download.

I need to do the following:

1) Display a watermark on the first page of the document.
2) I will need to display a static message on the top and bottom of each page of the document.
3) I will need to display a date and time stamp on the top and bottom of each page of the document for indicating when the document was downloaded.

Thanks in advance
JavaJava EE

Avatar of undefined
Last Comment
gdkinney_2

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
onlyaymie

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
gdkinney_2

ASKER
Is itextpdf free?  I believe we need something that is free.
onlyaymie

it is free
gdkinney_2

ASKER
We are using Struts.  We have a wrapper class called DownloadDocument that implements org.apache.struts.actions.DownloadAction.  I send this class a contentType (a String) and a inputStream (an InputStream) in its constructor.

Here is the call to the constructor:
downlloadDocument downloadDocument = new DownloadDocument("application/pdf", inputStream);

parameter inputStream is a java.io.InputStream;

How would I then use itextpfd or some other Java watermarking tool to watermark downloadDocument the way I described above?

Thanks
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
onlyaymie

You would add logic in your DownloadDocument or a helper class to add your additions to the original pdf.  Here is an example from the itext site of superimposing image and text.  http://itextpdf.com/examples/iia.php?id=113 Your needs seem to be much simpler - just adding a few pieces of text.  There are many other examples of using the library at http://itextpdf.com/book/examples.php
gdkinney_2

ASKER
What I really need to do is make a copy of the original pdf file, then watermark that file and return that copy of the original once it has been watermarked.  I can save the watermarked file as maybe temp.pdf but I do not want to overwrite the original file with the watermarks.

Is there a way I can do this in itextpdf?  Is there a way I can make a copy of a pdf then watermark the copied file and then save this copied watermarked file under another filename like maybe temp.pdf?

Thanks
onlyaymie

you can save it, but you don't have to - you could just stream the updated bytes directly out.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gdkinney_2

ASKER
Hi onlyaymie,

I am brand new to itextpdf so I have no idea how to do that.  I really need some concrete examples.

Thanks
onlyaymie

I would advise taking a look at the extensive examples on the itext site.  http://itextpdf.com/book/examples.php
gdkinney_2

ASKER
I am using the following to watermark a file:

private void getNewFile(String oldFile) {

      try   {
             PdfReader pdfReader = new PdfReader(oldFile);
             int n = pdfReader.getNumberOfPages();
             int i = 1;
             PdfContentByte under;
             PdfContentByte over;
         
             // Get the PdfStamper object        
             PdfStamper pdfStamper = new PdfStamper(pdfReader, new  FileOutputStream(newFile));

             // Get the PdfContentByte type by pdfStamper.      
             //PdfContentByte underContent = pdfStamper.getUnderContent(1);
             BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
             BaseFont.WINANSI, BaseFont.EMBEDDED);

             while (i <= n)  {
                   under = pdfStamper.getUnderContent(i);
                   //Mark only the first page of document
                   if (i == 1)  {
                        under.beginText();
                        under.setFontAndSize(bf, 30);
                        under.showTextAligned(Element.ALIGN_LEFT, "SERRA/EPA", 200, 400, 45);
                        under.endText();
                   }                            
           
                   //Mark top of each page of document - Center text
                   under.beginText();
                   under.setFontAndSize(bf, 14);
                   under.showTextAligned(Element.ALIGN_LEFT, "DO NOT DISTRIBUTE", 200, 800, 0);
                   under.endText();

                   //Mark bottom of each page of document - Center text
                   under.beginText();
                   under.showTextAligned(Element.ALIGN_LEFT, "DO NOT DISTRIBUTE", 200, 0, 0);
                   under.endText();                                    

                   i++;

              }
              pdfStamper.close();
      } catch (Exception de) { }
 }

Is there a better to do it than using the showTextAligned method?  It uses absolute positioning.  I'd like a method that would put the text at the top and bottom of each page centered automatically without using absolute positioning.  Also I need a large text across the first page of the document centered at a 45 degree angle going up across the page.  I am using iText for PDF document marking in Java.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23