Aspose.Pdf

Set Paragraph Margins

Aspose.Pdf has a large collection of built-in classes. Paragraph class is one of them. It is an abstract class that has some shared properties which are commonly used by other Paragraph specializations like:

 

 

All above specialized classes share a property named Margin from their super class, Paragraph . We can use Margin property to set the margins of any kind of paragraph. But first of all, it's better to understand the paragraph margins.

 

The following figure provides a better understanding of paragraph margins:

 

 

Using Aspose.Pdf , we can assign a positive or negative value to the Margin property of a Paragraph .

 

The basic approach to set the margins of any kind of Paragraph is to use their Margin property. Infact, Margin property is an object of MarginInfo class and it can be manipulated by setting its further four properties as follows:

 

 

Using the above properties of Margin we can have full control over the margins of any Paragraph.

 

In the example given below, we will take a look that how can we align two Paragraphs side by side using the negative margin value.

 

Code Snippet

 

[C#]

 

//Instantiate Pdf object by calling its empty constructor

Pdf pdf1 = new Pdf();

 

//Add a new section to the Pdf object

Section sec1 = pdf1.Sections.Add();

 

//Instantiate a graph object, associate it with a section and pass the height

// & width of the graph

Graph g1 = new Graph(sec1,100,100);

 

//Add a graph object to the paragraphs collection of the section

sec1.Paragraphs.Add(g1);

 

//Add a circle object to the shapes collection of graph object

g1.Shapes.Add(new Circle(g1,50,50,30));

 

//Instantiate another Graph object, associate it with a section and pass the height

// & width of the graph

Graph g2 = new Graph(sec1,100,100);

 

//Set the value of left margin

g2.Margin.Left = 150;

 

//Set the value of top margin and assign a negative value to it

g2.Margin.Top = -100;

 

//Add the paragraph object "g2" to paragraphs collection of the section

sec1.Paragraphs.Add(g2);

 

//Add a rectangle to the graph object (g2) in its shapes collection

g2.Shapes.Add(new Aspose.Pdf.Rectangle(g2,20,20,60,60)); 

 

[VB.NET]

 

'Instantiate Pdf object by calling its empty constructor

Dim pdf1 As Pdf = New Pdf()

 

'Add a new section to the Pdf object

Dim sec1 As Section = pdf1.Sections.Add()

 

'Instantiate a graph object, associate it with a section and pass the height

' & width of the graph

Dim g1 As Graph = New Graph(sec1, 100, 100)

 

'Add a graph object to the paragraphs collection of the section

sec1.Paragraphs.Add(g1)

 

'Add a circle object to the shapes collection of graph object

g1.Shapes.Add(New Circle(g1, 50, 50, 30))

 

'Instantiate another Graph object, associate it with a section and pass the height

' & width of the graph

Dim g2 As Graph = New Graph(sec1, 100, 100)

 

'Set the value of left margin

g2.Margin.Left = 150

 

'Set the value of top margin and assign a negative value to it

g2.Margin.Top = -100

 

'Add the paragraph object "g2" to paragraphs collection of the section

sec1.Paragraphs.Add(g2)

 

'Add a rectangle to the graph object (g2) in its shapes collection

g2.Shapes.Add(New Aspose.Pdf.Rectangle(g2, 20, 20, 60, 60))

 

[JAVA]

 

//Instantiate Pdf object by calling its empty constructor

Pdf pdf1 = new Pdf();

 

//Add a new section to the Pdf object

Section sec1 = pdf1.getSections().add();

 

//Add a Text

Text text = new Text(sec1, "hello");

sec1.getParagraphs().add(text);

 

//Set the value of left margin

text.getMargin().setLeft(150);

 

//Set the value of top margin and assign a negative value to it

text.getMargin().setTop(-100);

 

[XML]

 

<Pdf xmlns="Aspose.Pdf">

     <Section>

         <Graph Width="100" Height="100">

             <Circle CenterPosition="50 50" Radius="30"/>

         </Graph>

         <Graph Width="100" Height="100" MarginLeft="150"

                 MarginTop="-100">

             <Rectangle Position="20 20 60 60"/>

         </Graph>

     </Section>

</Pdf>