How to add TEXT as Watermark In Pdf in java


In this core java tutorial we will learn How to add text as Watermark In Pdf using iText library - core java tutorial in Java with program and examples.

Follow these steps to add text as Watermark In Pdf using iText library >

register the page event helper in main method
pdfWriter.setPageEvent(new MyPdfPageEventHelper());

Create class MyPdfPageEventHelper which extends PdfPageEventHelper class and override onEndPagemethod to create page event helper, onEndPage method gets called as soon as document is closed i.e. immediately after document.close(); is called.
Then create the waterMarkText,
create Phrase and pass waterMarkText in constructor to set the waterMarkText’s font name, size, style, colour.
Then set the phrase alignment to set the alignment, rotation angle of waterMarkText.


How to Set the Font Name, Size, Style and Colour In Pdf using itext in java

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

Convert Html To Pdf in java using iText - iText java tutorial

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial





Download jars required to execute program >

Or, you may download jars from here.


Program/Example How to add TEXT as Watermark In Pdf in java
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How to add text as Watermark In Pdf
*  Example Using iText library - core java tutorial
*
*/
public class AddTextAsWatermarkInPdfExample {
   public static void main(String[] args) {
          try {
                
                 String pdfFilePath = "E:/Add Text As Watermark text In Pdf.pdf";
                
                 Document document = new Document();
                 PdfWriter pdfWriter = PdfWriter
                              .getInstance(document, new FileOutputStream(
                                            pdfFilePath));
                 pdfWriter.setPageEvent(new MyPdfPageEventHelper()); // register the
                                                                                                      // page event helper
                 document.open();
                 System.out.println("Writing Paragraph to PDF");
                 for(int i=0; i<40; i++) //For loop -To show text is overlapping the waterMark text
                 document.add(new Paragraph(
                             "This document contains a Watermark text (Background text). "));
                 document.close();
                
                
                 System.out.println("PDF created in >> "+ pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/**
* Extend PdfPageEventHelper class and override onEndPagemethod
* to create page event helper.
*
* onEndPage method gets called as soon as document is closed i.e.
* immediately after document.close(); is called
*/
class MyPdfPageEventHelper extends PdfPageEventHelper {
   @Override
   public void onEndPage(PdfWriter pdfWriter, Document document) {
          System.out.println("Creating text as Waterwark in PDF");
         
          PdfContentByte pdfContentByte = pdfWriter.getDirectContentUnder();
          String waterMarkText = "JavaMadeSoEasy";
//create Phrase and pass waterMarkText in constructor,
//set the waterMarkText’s font name, size, style, colour.
          Phrase phrase = new Phrase(waterMarkText, new Font(
                       FontFamily.HELVETICA, //Select the Font name of waterMark Text
                       60, //Select the Font type of waterMark Text
                       Font.ITALIC, //Select the Font style of waterMark Text
                       BaseColor.LIGHT_GRAY)); //Select the Font colour of waterMark Text
                               //Keep colour as Light only, so that it appears
                               //Separately as watermark and  does not obscure text of the page
         
          // 300f is x axis,
          // 550f is y axis,
          ColumnText.showTextAligned(pdfContentByte,
                       Element.ALIGN_CENTER, //Keep waterMark center aligned
                       phrase, 300f, 500f,
                       45f); // 45f is the rotation angle
   }
}
/* OUTPUT of above program/Example -
Writing Paragraph to PDF
Creating text as Waterwark in PDF
PDF created in >> E:/Add Text As Watermark text In Pdf.pdf
*/


PDF formed after executing above java program(How to add TEXT as Watermark In Pdf in java) will look like this >


Summary -
So in this core java tutorial we learned How to add text as Watermark In Pdf using iText library in core java.
We registered the page event helper in main method

Created class MyPdfPageEventHelper which extends PdfPageEventHelper class and override onEndPagemethod to create page event helper, onEndPage method gots called as soon as document is closed.
Then created the waterMarkText,
created Phrase and passed waterMarkText in constructor to set the waterMarkText’s font name, size, style, colour.
Then we set the phrase alignment to set the alignment, rotation angle of waterMarkText.





Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java


Creating first pdf and setting attributes using itext in java - iText tutorial >

Create the first Pdf Example Using iText library in java - iText java tutorial example

Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java



Creating TABLES in pdf in java - iText tutorial >

How to Create the Table In Pdf in java - iText java tutorial

How to do the Cell Alignment In Pdf generated table in Java program

How to Set Cell Background Colour In Pdf Table in java example

How to Set Cell Background Colour In Pdf Table in java - iText java tutorial

How to Create Images In Pdf generated Table in java itext

How to Create Nested Tables in Pdf in java - iText java tutorial

How to Create the Table With Multiple Rows In Pdf in java program

Creating the Table and set the Column Width And Spacing In Pdf in java

Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example




Creating LIST in pdf in java - iText tutorial >

Create Greek List In pdf in java using iText - iText java tutorial

How to Create Roman List In pdf in java - iText java tutorial

Create ZapfDingbats List In pdf in java - iText java tutorial program

How to Create Ordered List In pdf in java using iText - iText java tutorial

Creating the UnOrdered List In pdf in java - iText java tutorial example

How to Create Lists And SubLists In Pdf in java - iText java tutorial



Creating PASSWORD PROTECTED PDF in java - iText tutorial >

How to Create Password Protected Pdf in java - iText Example





No comments:

Post a Comment