Saturday, 7 November 2015

set WaterMark Text in PDF using itextsharp in C#

watermark text in background PDF


Description:-

In this example we can explain that How can we create a PDF file with watermark text as a Background of the pdf  using itextsharp dll in asp.net using C#.

Generally we create simple pdf directally by exporting gridview control data to pdf but sometime we have requirement like watermark text display as background of pdf in each and every pages in our application.


Suppose if we have created one pdf report for any Honda showroom then Honda company have normally requirement like its HONDA name display as watermark text in pdf background in every pages of the pdf. Here we demonstrate set the watermark text in pdf using itextsharp in C#.



Code:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PDFWaterMark
{
    public partial class Form1 : Form
    {
        string watermarkText = "WaterMark Testing - Pandian";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string OutLocation = "D:\\WaterMark.pdf";
            PdfReader reader1 = new PdfReader(textBox1.Text);
            using (FileStream fs = new FileStream(OutLocation, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (PdfStamper stamper = new PdfStamper(reader1, fs))
                {
                    int pageCount1 = reader1.NumberOfPages;
                    //Create a new layer
                    PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
                    for (int i = 1; i <= pageCount1; i++)
                    {
                        iTextSharp.text.Rectangle rect = reader1.GetPageSize(i);
                        //Get the ContentByte object
                        PdfContentByte cb = stamper.GetUnderContent(i);
                        //Tell the CB that the next commands should be "bound" to this new layer
                        cb.BeginLayer(layer);
                        cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
                        PdfGState gState = new PdfGState();
                        gState.FillOpacity = 0.25f;
                        cb.SetGState(gState);
                        cb.SetColorFill(BaseColor.BLACK);
                        cb.BeginText();
                        cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
                        cb.EndText();
                        //"Close" the layer
                        cb.EndLayer();
                    }
                }
            }
            MessageBox.Show("Water Mark Completed");
        }
    }
}

1 comments: