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");
excellnt
ReplyDeletethanks :)
Deleteexcellent post thats what i needed man :)
ReplyDeletethanks :)
DeleteHi Ravi,
ReplyDeleteI 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
Sorry for soo much late reply. Is it working now ?
Deletehi.
ReplyDeleteBy Using this code, watermark is appearing in the last page of the pdf .i want to show in all the pages of the pdf...
Can u please share the code snippet for the same?
DeleteIts not supporting for Arabic text as water mark..
ReplyDeleteWatermark 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.
ReplyDeleteHi,
ReplyDeletehow we can remove or replace the added watermark? Considering that Text was added in watermark not image.