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");