Pages

Friday, February 25, 2011

Watermark on a Exisiting PDF using iTextsharp Library

iText Download
Creating a Template For Text Watermark
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing;
namespace ConsoleApplication1
{
    public static class GenrateStampingTemplate
    {

        public static void createTemplate(string StampSignautre, string stampFilename)
        {
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, 
                new FileStream(stampFilename, FileMode.Create));
            document.Open();
            iTextSharp.text.Font font =
                new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 22, 
                    iTextSharp.text.Font.NORMAL,
                    new BaseColor(System.Drawing.Color.Blue));
            ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, 
                new Phrase(StampSignautre, font), 297.5f, 421, 45);
            document.Close();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;

namespace ConsoleApplication1
{
    /// 
    /// This class is used to Put Watermark Text and Image on PDF Document
    /// 
   public static class StampDocument
   {
       
      public static void PutTextWateMark(string path,string waterMarkTemplate)
      {
            byte[] _byte = File.ReadAllBytes(path);
            byte[] _templateBye = File.ReadAllBytes(waterMarkTemplate);
            PdfReader reader = new PdfReader(_byte);
            PdfStamper pdfStamper = new PdfStamper(reader, 
                new FileStream(path, FileMode.Create));
            PdfContentByte content;
            PdfReader templateReader = new PdfReader(_templateBye);
            PdfImportedPage page = pdfStamper.GetImportedPage(templateReader, 1);
            for (int i = 0; i < reader.NumberOfPages; i++)
            {
                content = pdfStamper.GetUnderContent(i+1);
                content.AddTemplate(page, 0, 0);
            }
            pdfStamper.Close();
            templateReader.Close();
            
     }
     public static void PutImageWaterMark(string path, string imagePath)
     {
            byte[] _byte = File.ReadAllBytes(path);
            
            PdfReader reader = new PdfReader(_byte);
            PdfStamper pdfStamper = new PdfStamper(reader,
                new FileStream(path, FileMode.Create));
            Image img = Image.GetInstance(imagePath);
            PdfContentByte content;
            
            img.SetAbsolutePosition(200, 400);
            
            for (int i = 0; i < reader.NumberOfPages; i++)
            {
                content = pdfStamper.GetUnderContent(i+1);
                content.AddImage(img);
            }
            pdfStamper.Close();
            

     }
  }
}
//using custom Text
GenrateStampingTemplate.createTemplate("Watermark", "d:\\CompanyStamp.pdf");
StampDocument.PutTextWateMark("d:\\test.pdf", "d:\\CompanyStamp.pdf");
//using image
StampDocument.PutImageWaterMark("d:\\test.pdf", "d:\\test.img");

10 comments:

  1. excellent post thats what i needed man :)

    ReplyDelete
  2. Hi Ravi,

    I tried your code, it didn't give me any error messages, but the watermark didn't appear on the pdf file. Any idea why it could be?

    Thanks in advance,

    Alex

    ReplyDelete
  3. hi.
    By Using this code, watermark is appearing in the last page of the pdf .i want to show in all the pages of the pdf...

    ReplyDelete
  4. Its not supporting for Arabic text as water mark..

    ReplyDelete
  5. Watermark on a Exisiting PDF using iDiTect.PDF Library, is another choice for c# developers, it's cheap and easy to use, private font and text color can be modified by yourself.

    ReplyDelete