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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it is free
ASKER
We are using Struts. We have a wrapper class called DownloadDocument that implements org.apache.struts.actions. DownloadAc tion. 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("applicat ion/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
Here is the call to the constructor:
downlloadDocument downloadDocument = new DownloadDocument("applicat
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
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
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.
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
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
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(BaseFo nt.HELVETI CA,
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(Elem ent.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(Elem ent.ALIGN_ LEFT, "DO NOT DISTRIBUTE", 200, 800, 0);
under.endText();
//Mark bottom of each page of document - Center text
under.beginText();
under.showTextAligned(Elem ent.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.
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
BaseFont bf = BaseFont.createFont(BaseFo
BaseFont.WINANSI, BaseFont.EMBEDDED);
while (i <= n) {
under = pdfStamper.getUnderContent
//Mark only the first page of document
if (i == 1) {
under.beginText();
under.setFontAndSize(bf, 30);
under.showTextAligned(Elem
under.endText();
}
//Mark top of each page of document - Center text
under.beginText();
under.setFontAndSize(bf, 14);
under.showTextAligned(Elem
under.endText();
//Mark bottom of each page of document - Center text
under.beginText();
under.showTextAligned(Elem
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.
ASKER