Drawing rectangele automatically generates a new page in .NET

Drawing rectangele automatically generates a new page
I want to draw on the same page
Is it possible to do this?
How do I disable the generation of new pages?
PDFCreateRectangleTest.zip (27.2 KB)

@linbaidu,

You had several issues in your code sample. I fixed them and added some extra stuff. Hopefully, this will help you understand better how to draw on a page and how the positioning works.

private void Logic()
{
    Document doc = new Document();

    Page page = doc.Pages.Add();

    MarginInfo marginInfo = new MarginInfo();
    marginInfo.Left = 0;
    marginInfo.Right = 0;
    marginInfo.Top = 0;
    marginInfo.Bottom = 0;

    page.PageInfo.Margin = marginInfo;


    DrawRectangle(page, 0, 0, 200, 200, Aspose.Pdf.Color.Blue, 0);

    DrawRectangle(page, 200, 200, 100, 300, Aspose.Pdf.Color.Brown, 0);

    DrawRectangle(page, 300, 0, 250, 200, Aspose.Pdf.Color.Green, 0);
    
    DrawRectangle(page, 100, 500, 300, 50, Aspose.Pdf.Color.BurlyWood, 0);

    // Error Rectangle

    DrawRectangle(page, 500, 500, 300, 300, Aspose.Pdf.Color.DarkCyan, 0);


    doc.Save($"{PartialPath}_output.pdf");
}

private static void DrawRectangle(Page page, float x, float y, float width, float height, Aspose.Pdf.Color color, int zindex)
{
    // Create graph object with dimensions same as Page, where we will be drawing our rectangles
    Aspose.Pdf.Drawing.Graph graph = new Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height)
    {
        // Can we change the position of graph instance
        IsChangePosition = true,
        // Set Left coordinate position for Graph instance
        Left = 0,
        // Set Top coordinate position for Graph object
        Top = 0,

    };           

    // Add a rectangle inside the "graph"
    var rect = new Aspose.Pdf.Drawing.Rectangle(x, y, width, height);
    // Set rectangle fill color
    rect.GraphInfo.FillColor = color;
    // Color of graph object
    rect.GraphInfo.Color = color;
    // Add rectangle to shapes collection of graph instance
    graph.Shapes.Add(rect);
    // Set Z-Index for rectangle object
    graph.ZIndex = zindex;
    // Add graph to paragraphs collection of page object
    page.Paragraphs.Add(graph);

    if(x + width > (float)page.PageInfo.Width || y + height > (float)page.PageInfo.Height)
    {
        // To show error because you drawed out of bounds
        rect.GraphInfo.FillColor = Color.Black;
    }
}

Here is the output:
DrawingRectanglesAddsPage_output.pdf (1.6 KB)

This is very useful for me!
Thank you very much for your reply!!