public void AddPageNumber(string str, string displaytext)
{
byte[] bytes = File.ReadAllBytes(str);
iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 6, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
using (var stream = new MemoryStream())
{
stream.Flush();
System.Windows.Forms.Application.DoEvents();
PdfReader reader = new PdfReader(bytes);
PdfReader.unethicalreading = true;
Paragraph p = new Paragraph();
Document doc = new Document();
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
System.Windows.Forms.Application.DoEvents();
//PdfContentByte canvas = stamper.GetOverContent(1);
//iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
int pages = reader.NumberOfPages;
for (int i = 1; i <= pages; i++)
{
iTextSharp.text.Rectangle mediabox = reader.GetPageSize(i);
float x, y;
x = mediabox.Height;
y = mediabox.Width;
iTextSharp.text.Rectangle cropBox = reader.GetCropBox(i);
cropBox.GetRectangle(y - 1000, x - 50);
iTextSharp.text.Rectangle bottomRight = new iTextSharp.text.Rectangle(cropBox.GetRight(199), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(18));
//TextField tf = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(0, 0, 300, 100), displaytext);
////Change the orientation of the text
//tf.Rotation = 0;
EmptyTextBoxSimple(stamper, i, bottomRight, BaseColor.WHITE);
ColumnText columnText = GenerateTextBox(stamper, i, bottomRight);
columnText.AddText(new Phrase(displaytext.ToString(), blackFont));
columnText.Go();
}
}
bytes = stream.ToArray();
//stream.Flush();
}
System.Threading.Thread.Sleep(200);
File.WriteAllBytes(str, bytes);
}