Watermark created pdf in C#
Add the following code to your button click event
Here is the result
Steps for creating Pdf watermarked.
- creating new project window application
- download itextsharp from Here
- add reference of itextSharp.dll to your project
- Add a button to form
Add the following functions
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public Paragraph AddParagragh(string ParagraphText)
{
Paragraph p = new Paragraph();
p.SpacingBefore = 10f;
p.FirstLineIndent = 10f;
p.SpacingAfter = 15f;
iTextSharp.text.Font f = new iTextSharp.text.Font();
p.Font.SetFamily("Courier");
p.Alignment = Element.ALIGN_JUSTIFIED;
p.Font.Size = 13f;
p.Font.SetColor(0, 0, 0);
p.Add(ParagraphText);
return p;
}
public Chunk AddParagraphHeader(string headingText)
{
Chunk ch = new Chunk(headingText);
ch.Font.Size = 16f;
ch.Font.SetStyle("bold");
ch.Font.SetColor(0, 0, 0);
return ch;
}
Add the following code to your button click event
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
private void button2_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4);
try
{
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/WaterMark.pdf", FileMode.Create));
doc.Open();
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()."));
//water mark
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ITALIC, BaseFont.CP1252, false);
BaseColor bc = new BaseColor(0, 0, 0, 65);
iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 145.5F, iTextSharp.text.Font.ITALIC, bc);
// Dim wfont = New Font(BaseFont.TIMES_ROMAN, 1.0F, BaseFont.COURIER, BaseColor.LIGHT_GRAY)
ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase("Confidential", times), 245.5F, 480.0F, -55);
//End water mark
doc.Close();
System.Diagnostics.Process.Start(Environment.GetFolderPath(
Environment.SpecialFolder.Desktop) + "/WaterMark.pdf");
}
catch (Exception ae)
{
doc.Close();
}
}
Here is the result
6 Comments
This comment has been removed by a blog administrator.
ReplyDeletehow i make watermark transparent...??? i used your code but its not making watermark transparent. please help me sir.
ReplyDeleteThis watermark is already transparent. you can see in screenshot.
Deleteya sir i saw you screenshot but its not working......i modified your code to make it transparent.
ReplyDeleteitextsharp is perfect tool for open source library, however sometimes we are not allowed to use the open source component, so I search and find a cheaper and good use third part library to watermark pdf in c#
ReplyDeleteWatermark PDF in C# is not difficult, you can try CnetSDK.PDF.Library, direct reference, very convenient.
ReplyDeletePost a Comment