public static void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText)
{
iTextSharp.text.pdf.BaseFont tWatermarkFont = null;
float tWatermarkFontSize = 48F;
iTextSharp.text.Color tWatermarkFontColor = null;
float tWatermarkFontOpacity = 0.3F;
float tWatermarkRotation = 45.0F;
tWatermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
tWatermarkFontColor = iTextSharp.text.Color.BLUE;
AddWatermarkTextC(sourceFile, outputFile, watermarkText, tWatermarkFont, tWatermarkFontSize, tWatermarkFontColor, tWatermarkFontOpacity, tWatermarkRotation);
}//void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText )
public static void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.Color watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)
{
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;
float currentY = 0.0F;
float offset = 0.0F;
int pageCount = 0;
try
{
reader = new iTextSharp.text.pdf.PdfReader(sourceFile);
rect = reader.GetPageSizeWithRotation(1);
stamper = new iTextSharp.text.pdf.PdfStamper(reader, new System.IO.FileStream(outputFile, System.IO.FileMode.Create));
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 (watermarkFont == null)
if (watermarkFontColor == null)
{
watermarkFontColor = iTextSharp.text.Color.BLUE;
}//if (watermarkFontColor == null)
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);
underContent.SaveState();
underContent.SetGState(gstate);
underContent.SetColorFill(watermarkFontColor);
underContent.BeginText();
underContent.SetFontAndSize(watermarkFont, watermarkFontSize);
underContent.SetTextMatrix(30, 30);
if (watermarkText.Length > 1)
{
currentY = (rect.Height / 2) + ((watermarkFontSize * watermarkText.Length) / 2);
}//if (watermarkText.Length > 1)
else
{
currentY = (rect.Height / 2);
}//else if (watermarkText.Length > 1)
for (int j = 0; j < watermarkText.Length; j++)
{
if (j > 0)
{
offset = (j * watermarkFontSize) + (watermarkFontSize / 4) * j;
}//if (j > 0)
else
{
offset = 0.0F;
}//else if (j > 0)
underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText[j], rect.Width / 2, currentY - offset, watermarkRotation);
}//for (int j = 0; j < watermarkText.Length; j++)
underContent.EndText();
underContent.RestoreState();
}//for (int i = 1; i <= pageCount; i++)
stamper.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}//void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.Color watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)