Aspose.Pdf

Custom Positioning

When you add a Paragraph to the PDF document using Aspose.Pdf then the page renderer engine of Aspose.Pdf decides the position of the Paragraph on the page. But sometimes, you may want the Paragraph to be displayed on the page at some position specified by you. In this case, you can use custom positioning. This topic describes how to use custom positioning in Aspose.Pdf .

 

When using custom positioning, you can use the Top and Left property of Paragraph to specify its position on the page.

 

There are some positioning types defined by Aspose.Pdf in PositioningType enumeration. These pre-defined positioning types are listed below with their brief descriptions:

 

Positioning Types

Description

Auto

Paragraph is positioned automatically by page renderer engine

PageRelative

Paragraph is positioned relative to the page

ColumnRelative

Paragraph is positioned relative to the column

ParagraphRelative

Paragraph is positioned relative to the paragraph

 

The above positioning types are explained in the figures below for your better understanding.

 

PageRelative

 

Figure: PageRelative positioning

 

ColumnRelative

 

Figure: ColumnRelative positioning

 

ParagraphRelative

 

Figure: ParagraphRelative positioning

 

It is important to know that custom positioning doesn't affect the default automatic positioning. It means that the automatically positioned paragraphs are rendered as if the custom positioning paragraphs don't exist. If a custom positioning paragraph is too large, it will not be rendered into a new page but will simply span the page edge. Moreover, when using ParagraphRelative positioning as shown in above figure, the referenced Paragraph should be a Paragraph "ahead of" the current Paragraph in the document object model. That will make sure the referenced Paragraph to be rendered first.

 

The example below shows how to use custom positioning for the paragraphs. In this example, a Graph (which is a specializtion of the Paragraph class) is added at the top of the page and a note annotation is added besides a Text Paragraph .

 

Example:

 

[C#]

 

 

//Instantiate the Pdf object

Pdf pdf1 = new Pdf();

 

 

//Add a section to the Pdf object

Section sec1 = pdf1.Sections.Add();

 

 

//Create a text paragraph

Text text1 = new Text("This is a text paragraph.");

 

 

//Set the id of the paragraph to "text1" so that it can referenced uniquely

text1.ID = "text1";

 

 

//Add the paragraph to the section

sec1.Paragraphs.Add(text1);

 

 

//Create a graph with specified left and top position settings. Set its

//poition relative to the page. Add a rectangle to its shapes collection and

//then add the graph to the paragraph collection of the section

Graph graph1 = new Graph(200,50);

graph1.Left = 200;

graph1.Top = 10;

graph1.PositioningType = PositioningType.PageRelative;

graph1.Shapes.Add(new Rectangle(0,0,200,50));

sec1.Paragraphs.Add(graph1);

 

 

//Create an attachment as note annotation and add it to the section as a

//pragraph. Set the content and heading for the note. Set its position relative

//to the paragraph. Assign a unique id to this note annotation for the

//reference purposes and then customize its left and top position

Attachment noteAttachment = new Attachment();

sec1.Paragraphs.Add(noteAttachment);

noteAttachment.AttachmentType = AttachmentType.Note;

noteAttachment.NoteContent = "This is a test for note";

noteAttachment.NoteHeading = "this is a Note";

noteAttachment.PositioningType = PositioningType.ParagraphRelative;

noteAttachment.ReferenceParagraphID = "text1";

noteAttachment.Left = 200;

noteAttachment.Top = 0;

 

 

//Save the Pdf

pdf1.Save("e:/temp/test.pdf");

 

 

[VB.NET]

 

 

'Instantiate the Pdf object

Dim pdf1 As Pdf = New Pdf

 

 

'Add a section to the Pdf object

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

 

 

'Create a text paragraph

Dim text1 As Text = New Text("This is a text paragraph.")

 

 

'Set the id of the paragraph to "text1" so that it can referenced uniquely

text1.ID = "text1"

 

 

'Add the paragraph to the section

sec1.Paragraphs.Add(text1)

 

 

'Create a graph with specified left and top position settings. Set its

'poition relative to the page. Add a rectangle to its shapes collection and

'then add the graph to the paragraph collection of the section

Dim graph1 As Graph = New Graph(200, 50)

graph1.Left = 200

graph1.Top = 10

graph1.PositioningType = PositioningType.PageRelative

graph1.Shapes.Add(New Rectangle(0, 0, 200, 50))

sec1.Paragraphs.Add(graph1)

 

 

'Create an attachment as note annotation and add it to the section as a

'pragraph. Set the content and heading for the note. Set its position relative

'to the paragraph. Assign a unique id to this note annotation for the

'reference purposes and then customize its left and top position

Dim noteAttachment As Attachment = New Attachment

sec1.Paragraphs.Add(noteAttachment)

noteAttachment.AttachmentType = AttachmentType.Note

noteAttachment.NoteContent = "This is a test for note"

noteAttachment.NoteHeading = "this is a Note"

noteAttachment.PositioningType = PositioningType.ParagraphRelative

noteAttachment.ReferenceParagraphID = "text1"

noteAttachment.Left = 200

noteAttachment.Top = 0

 

 

'Save the Pdf

pdf1.Save("e:/temp/test.pdf")

 

[XML]

 

 

<Pdf xmlns="Aspose.Pdf">

     <Section>

         <Text ID="text1">

                 <Segment>This is a text paragraph.

                 </Segment>

         </Text>

         <Graph Width="200" Height="50" Left="200"

                 Top="10" PositioningType= "PageRelative">

             <Rectangle Position="0 0 200 50"></Rectangle>

         </Graph>

         <Attachment AttachmentType="Note"

                 NoteHeading="this is a Note"

                 NoteContent="This is a test for note"

                 Left="200" Top="0"

                 PositioningType="ParagraphRelative"

                 ReferenceParagraphID="text1">

         </Attachment>

     </Section>

</Pdf>