public static void AddWatermarkText(string comm_id, iTextSharp.text.pdf.BaseFont watermarkFont = null, float watermarkFontSize = 48, iTextSharp.text.Color watermarkFontColor = null, float watermarkFontOpacity = 0.3f, float watermarkRotation = 45f)
{
iTextSharp.text.pdf.PdfReader reader = null;
iTextSharp.text.pdf.PdfStamper stamper = null;
iTextSharp.text.pdf.PdfGState gstate = null;
iTextSharp.text.pdf.PdfContentByte underContent = null;
iTextSharp.text.Rectangle rect = null;
string[] watermarkText ={ SessionManager.v_stno, System.DateTime.Now.ToString() };
float currentY = 0f;
float offset = 0f;
int pageCount = 0;
try
{
string PdfName = SessionManager.v_stno + "_" + comm_id + ".pdf";
string pdfpath = "C:\\Communication\\Circular\\" + PdfName;
reader = new iTextSharp.text.pdf.PdfReader(pdfpath);
rect = reader.GetPageSizeWithRotation(1);
stamper = new iTextSharp.text.pdf.PdfStamper(reader, new System.IO.FileStream(pdfpath,FileMode.Create, FileAccess.Write, FileShare.None));
if (watermarkFont == null)
{
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
}
if (watermarkFontColor == null)
{
watermarkFontColor = iTextSharp.text.Color.BLUE;
}
gstate = new iTextSharp.text.pdf.PdfGState();
gstate.FillOpacity = watermarkFontOpacity;
gstate.StrokeOpacity = watermarkFontOpacity;
pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
underContent = stamper.GetOverContent(i);
var _with1 = underContent;
_with1.SaveState();
_with1.SetGState(gstate);
_with1.SetColorFill(watermarkFontColor);
_with1.BeginText();
_with1.SetFontAndSize(watermarkFont, watermarkFontSize);
_with1.SetTextMatrix(30, 30);
if (watermarkText.Length > 1)
{
currentY = (rect.Height / 2) + ((watermarkFontSize * watermarkText.Length) / 2);
}
else
{
currentY = (rect.Height / 2);
}
for (int j = 0; j <= watermarkText.Length - 1; j++)
{
if (j > 0)
{
offset = (j * watermarkFontSize) + (watermarkFontSize / 4) * j;
}
else
{
offset = 0f;
}
_with1.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText[j], rect.Width / 2, currentY - offset, watermarkRotation);
}
_with1.EndText();
_with1.RestoreState();
}
stamper.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}