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 RPosted Jan 7, 2016, 11:51 AM
Kip HackmanPosted Dec 10, 2020, 4:28 PM
Gajendra JangidPosted Jun 20, 2019, 11:46 PM
sudhanshu GuptaPosted Jun 29, 2017, 10:11 AM
{
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
}
Usha TalakantiPosted Jan 8, 2016, 1:23 AM
Lisa WhitePosted Jan 7, 2016, 9:21 PM