Check shape bounds in Shapes collection
Introduction
This document provides a detailed guide on using the bounds-checking feature in the Shapes collection. This feature ensures that elements fit within their parent container and can be configured to throw an exception if the component does not fit.
Prerequisites
You will need the following:
Visual Studio 2019 or later
Aspose.PDF for .NET 25.3 or later
A sample PDF file that contains some pages
You can download the Aspose.PDF for .NET library from the official website or install it using the NuGet Package Manager in Visual Studio.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CheckShapeBounds ()
{
// Create PDF document
using ( var document = new Aspose . Pdf . Document ())
{
// Add page
var page = document . Pages . Add ();
// Create a Graph object with specified dimensions
var graph = new Aspose . Pdf . Drawing . Graph ( 100d , 100d )
{
Top = 10 ,
Left = 15 ,
Border = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . Box , 1F , Aspose . Pdf . Color . Black )
};
page . Paragraphs . Add ( graph );
// Create a Shape object (for example, Rectangle) with specified dimensions
var rect = new Aspose . Pdf . Drawing . Rectangle (- 1 , 0 , 50 , 50 )
{
GraphInfo =
{
FillColor = Aspose . Pdf . Color . Tomato
}
};
// Set the BoundsCheckMode to ThrowExceptionIfDoesNotFit
graph . Shapes . UpdateBoundsCheckMode ( Aspose . Pdf . BoundsCheckMode . ThrowExceptionIfDoesNotFit );
// Add the rectangle to the graph
graph . Shapes . Add ( rect );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CheckShapeBounds ()
{
// Create PDF document
using var document = new Aspose . Pdf . Document ();
// Add page
var page = document . Pages . Add ();
// Create a Graph object with specified dimensions
var graph = new Aspose . Pdf . Drawing . Graph ( 100d , 100d )
{
Top = 10 ,
Left = 15 ,
Border = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . Box , 1F , Aspose . Pdf . Color . Black )
};
page . Paragraphs . Add ( graph );
// Create a Aspose.Pdf.Drawing.Shape object (for example, Aspose.Pdf.Drawing.Rectangle) with specified dimensions
var rect = new Aspose . Pdf . Drawing . Rectangle (- 1 , 0 , 50 , 50 )
{
GraphInfo =
{
FillColor = Aspose . Pdf . Color . Tomato
}
};
// Set the BoundsCheckMode to ThrowExceptionIfDoesNotFit
graph . Shapes . UpdateBoundsCheckMode ( Aspose . Pdf . BoundsCheckMode . ThrowExceptionIfDoesNotFit );
// Add the rectangle to the graph
graph . Shapes . Add ( rect );
}