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();
}

Arul RRank #2,478 · 566 reputationPosted Jan 7, 2016, 6:51 AM EST
Lisa White141 reputationPosted Jan 7, 2016, 4:21 PM EST
Usha Talakanti37 reputationPosted Jan 7, 2016, 8:23 PM EST
sudhanshu Gupta64 reputationPosted Jun 29, 2017, 6:11 AM EDT
{
Document doc = new Document(PageSize.A4, 20f, 20f, 20f, 20f);
PdfGState _state;
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/WaterMark.pdf", FileMode.Create));
doc.Open();
var pagenumber = writer.PageNumber;
int n = pagenumber;
//string imageURL = "D:\sudhanshu/opr/OprNew17-06-2017/opr/images/login-logo.png";
//iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
//doc.Add(jpg);
doc.Add(this.AddParagraphHeader("Getting ready"));
doc.Add(this.AddParagragh(@"At the top level, there is a Figure instance containing all that we see and some more (that we don't see). The figure contains, among other things, instances of the Axes class as a Figure.axes field. The Axes instances contain almost everything we care about: all the lines, points, ticks, and labels. So, when we call plot(), we are adding a line (matplotlib.lines.Line2D) to the Axes.lines list. If we plot a histogram (hist()), we are adding rectangles to the list of Axes.patches ('patches' is the term inherited from MATLAB®, and it represents the 'patch of color' concept)."));
doc.Add(this.AddParagragh(@"An instance of Axes also holds references to the XAxis and YAxis instances, which in turn refer to the x axis and y axis, respectively. The XAxis and YAxis instances manage the drawing of the axis, labels, ticks, tick labels, locators, and formatters. We can reference these through Axes.xaxis and Axes.yaxis, respectively. We don't have to go all the way down to XAxis or YAxis instances to get to the labels as matplotlib gives us a helper method (practically a shortcut) that enables iterations via these labels: matplotlib.pyplot.xlabel() and matplotlib.pyplot.ylabel()."));
doc.Add(this.AddParagragh(@"At the top level, there is a Figure instance containing all that we see and some more (that we don't see). The figure contains, among other things, instances of the Axes class as a Figure.axes field. The Axes instances contain almost everything we care about: all the lines, points, ticks, and labels. So, when we call plot(), we are adding a line (matplotlib.lines.Line2D) to the Axes.lines list. If we plot a histogram (hist()), we are adding rectangles to the list of Axes.patches ('patches' is the term inherited from MATLAB®, and it represents the 'patch of color' concept)."));
doc.Add(this.AddParagragh(@"An instance of Axes also holds references to the XAxis and YAxis instances, which in turn refer to the x axis and y axis, respectively. The XAxis and YAxis instances manage the drawing of the axis, labels, ticks, tick labels, locators, and formatters. We can reference these through Axes.xaxis and Axes.yaxis, respectively. We don't have to go all the way down to XAxis or YAxis instances to get to the labels as matplotlib gives us a helper method (practically a shortcut) that enables iterations via these labels: matplotlib.pyplot.xlabel() and matplotlib.pyplot.ylabel()."));
doc.Add(this.AddParagragh(@"At the top level, there is a Figure instance containing all that we see and some more (that we don't see). The figure contains, among other things, instances of the Axes class as a Figure.axes field. The Axes instances contain almost everything we care about: all the lines, points, ticks, and labels. So, when we call plot(), we are adding a line (matplotlib.lines.Line2D) to the Axes.lines list. If we plot a histogram (hist()), we are adding rectangles to the list of Axes.patches ('patches' is the term inherited from MATLAB®, and it represents the 'patch of color' concept)."));
doc.Add(this.AddParagragh(@"An instance of Axes also holds references to the XAxis and YAxis instances, which in turn refer to the x axis and y axis, respectively. The XAxis and YAxis instances manage the drawing of the axis, labels, ticks, tick labels, locators, and formatters. We can reference these through Axes.xaxis and Axes.yaxis, respectively. We don't have to go all the way down to XAxis or YAxis instances to get to the labels as matplotlib gives us a helper method (practically a shortcut) that enables iterations via these labels: matplotlib.pyplot.xlabel() and matplotlib.pyplot.ylabel()."));
//water mark
PdfContentByte cb = writer.DirectContent;
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font times = new Font(bfTimes, 120F, Font.TIMES_ROMAN, Color.LIGHT_GRAY);
_state = new PdfGState()
{
FillOpacity = 0.2F,
StrokeOpacity = 0.2F
};
cb.SetGState(_state);
//ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase("www.giftalove.com", times), 310F, 400F, 55);
ColumnText.ShowTextAligned(writer.DirectContentUnder, Element.ALIGN_CENTER, new Phrase("www.jobsncr.in", times), 310, 400, writer.PageNumber % 2 == 1 ? 60 : 55);
pdfReader(writer, times);
doc.Close();
System.Diagnostics.Process.Start(Environment.GetFolderPath(
Environment.SpecialFolder.Desktop) + "/WaterMark.pdf");
//End water mark
}
Gajendra JangidRank #157 · 14.9K reputationPosted Jun 20, 2019, 7:46 PM EDT
Kip HackmanRank #2,595 · 536 reputationPosted Dec 10, 2020, 11:28 AM EST