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
ASKER CERTIFIED SOLUTION
Avatar of onlyaymie
onlyaymie

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gdkinney_2
gdkinney_2

ASKER

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

it is free
Avatar of gdkinney_2
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
Avatar of onlyaymie
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
Avatar of gdkinney_2
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
Avatar of onlyaymie
onlyaymie

you can save it, but you don't have to - you could just stream the updated bytes directly out.
Avatar of gdkinney_2
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
Avatar of onlyaymie
onlyaymie

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

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo