Hi I have a pdf I created using itextsharp.
Using pdfreader I am reading the created pdf into a pdfstamper.
Now I am trying to use the pdfstamper to add a black rectangle the size of the page on all pages. How do i do this?
Also I cannot use document to add the rectangle because the stream is close!
MemoryStream stream = new MemoryStream();
PdfReader pdfReader = new PdfReader(output.ToArray());
PdfStamper stamper = new PdfStamper(pdfReader, stream);
for (int x = 0; x < stamper.Reader.NumberOfPages; x++)
{
Rectangle rectangle = document.PageSize;
rectangle.BackgroundColor = new BaseColor(0, 0, 0);
//stamper.Writer.AcroForm.
//document.Add(rectangle);
}
output.Close();
pdfReader.Close();
stamper.Close();
If you want to draw using the
PdfStamperthen you need to use thePdfContentBytewhich you can get by callingstamper.GetOverContent(pageNum). There’s a specific command on that object calledRectanglewhich does exactly what you want it to do. Also, remember that pages within a PDF start numbering at one and not zero.Below is a full working C# 2010 WinForm app targeting iTextSharp 5.1.1.0 that should do what you’re looking for, I think. You’ll need to modify it to support the
MemoryStreambut that should be pretty easy.