Link to home
Start Free TrialLog in
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
ASKER CERTIFIED SOLUTION
Avatar of onlyaymie
onlyaymie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gdkinney_2
gdkinney_2

ASKER

Is itextpdf free?  I believe we need something that is free.
it is free
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
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
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
you can save it, but you don't have to - you could just stream the updated bytes directly out.
Hi onlyaymie,

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

Thanks
I would advise taking a look at the extensive examples on the itext site.  http://itextpdf.com/book/examples.php
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.