Sample text is added to every page on the same location
Inside the Page_Load()
string filepath = @"http://EXAMPLE.COM/TEST.pdf";
// open the reader
PdfReader reader = new PdfReader(filepath);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
//FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
MemoryStream fs = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = writer.DirectContent;
int pageCount = reader.NumberOfPages;
var rectangle = reader.GetPageSize(1);
for (int i = 1; i <= pageCount; i++)
{
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(Color.BLACK);
cb.SetFontAndSize(bf, 9);
cb.BeginText();
cb.ShowTextAligned(2, "This is the new Text " , 535, 33, 0);
cb.EndText();
PdfImportedPage page = writer.GetImportedPage(reader, i);
var widthFactor = document.PageSize.Width / page.Width;
var heightFactor = document.PageSize.Height / page.Height;
var factor = Math.Min(widthFactor, heightFactor);
var offsetX = (document.PageSize.Width - (page.Width * factor)) / 2;
var offsetY = (document.PageSize.Height - (page.Height * factor)) / 2;
cb.AddTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
document.NewPage();
}
document.Close();
fs.Close();
writer.Close();
reader.Close();
WebClient client = new WebClient();
Byte[] buffer = fs.GetBuffer();
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.Flush();
}