Add 3-inch red rectangle to the top of the page

Hello,

My use case involves adding a 3-inch tall red rectangle to the top of the first page of the PDF.

The rectangle needs to have a height of 3-inches. And the width of the rectangle should run the entire width of the first page, page edge to page edge. I want this rectangle to increase the overall height of the first page of the PDF file. It shouldn’t just sit on top of content.

Any code examples that people can provide that would help me push this out the door quickly would be much appreciated.

@tdilbertcuc

We are preparing a code example to achieve your requirements and will get back to you shortly.

Thanks, Asad.ali. Very much appreciated.

Awaiting your response.

@tdilbertcuc

Please try below code snippet to add a 3-inch rectangle at the top of the PDF Page as per your requirements. We have also attached the generated output by the code snippet for your reference:

Document doc = new Document();
            
Page page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
page.PageInfo.Height = PageSize.A4.Height + (72 * 3); // 72 points = 1 inch


var canvas = new Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
page.Paragraphs.Add(canvas);

Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(0, (float)page.PageInfo.Height- (72 * 3), (float)page.PageInfo.Width,  72*3); 
var c = ColorTranslator.FromHtml("#FFFF00");
rect.Text = new TextFragment("test address , test address esttest address , test address testtest address , test address test");
rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
canvas.Shapes.Add(rect);
doc.Save(dataDir + "rectangle.pdf");

rectangle.pdf (2.0 KB)