Usha Talakanti

Usha Talakanti

  • NA
  • 31
  • 24.8k

watermark text in all the pdf pages of existing pdf

Jan 7 2016 3:57 AM
how to add watermark (text or image) in existing pdf in c#.I want the watermark in all the pages of the pdf .

I tired by itextsharp but the watermark is displaying in only last page of the pdf.
I want watermark in all pages

    public void CreateTemplate(string watermarkText, string targetFileName)
        {
            var document = new Document();
            var pdfWriter = PdfWriter.GetInstance(document, new FileStream(targetFileName, FileMode.Create));
            var font = new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 60, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.LIGHT_GRAY);
            document.Open();
            ColumnText.ShowTextAligned(pdfWriter.DirectContent, Element.ALIGN_CENTER, new Phrase(watermarkText, font), 300, 400, 45);
            document.Close();
        }
        public void AddTextWatermark(string sourceFilePath, string watermarkTemplatePath, string targetFilePath)
        {
            var pdfReaderSource = new PdfReader(sourceFilePath);
            var pdfStamper = new PdfStamper(pdfReaderSource, new FileStream(targetFilePath, FileMode.Create));
            var pdfReaderTemplate = new PdfReader(watermarkTemplatePath);
            var page = pdfStamper.GetImportedPage(pdfReaderTemplate, 1);

            for (var i = 0; i < pdfReaderSource.NumberOfPages; i++)
            {
                var content = pdfStamper.GetUnderContent(i + 1);
                content.AddTemplate(page, 0, 0);
            }

            pdfStamper.Close();
            pdfReaderTemplate.Close();
        }

Answers (6)