We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Positoning of elements

When creating a pdf document, is there a way to specify the exact position of each element. i.e I would like to position a table at x=10, y = 20, a piece of text at x=40, y = 20 and an image at x=40,y = 100.


thanks.

Hi Neil,

Thanks for your inquiry. You can use [FloatingBox class] as a container for any object e.g table image, text etc. and position it as per your requirements . Please check following sample code snippet Text, Image and Table objects are grouped together. Hopefully it will help you to accomplish your requirements.

Document doc = new Document();

Page page = doc.Pages.Add();

Aspose.Pdf.FloatingBox fb = new Aspose.Pdf.FloatingBox();

fb.Top = 10.0f;

fb.Left = 10.0f;

//Add the FloatingBox into paragraphs collection of Page

page.Paragraphs.Add(fb);

fb.Width = 100.0f;

//Add the Text into paragraphs collection of FloatingBox

fb.Paragraphs.Add(

new TextFragment("This is test Text added in FloatingBox using Aspose.Pdf"));

//Add the image in FloatingBox

FileStream fs = new FileStream(myDir + "aspose_logo.png", FileMode.Open, FileAccess.Read);

//Create an image object

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

//Set the ImageStream to a MemoryStream object

image1.ImageStream = fs;

//Add the image into paragraphs collection of FloatingBox

fb.Paragraphs.Add(image1);

//add a table in FloatingBox

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

// Set the table border color as LightGray

table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

// set the border for table cells

table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

// create a loop to add 10 rows

for (int row_count = 1; row_count < 10; row_count++)

{

// add row to table

Aspose.Pdf.Row row = table.Rows.Add();

// add table cells

row.Cells.Add("Column (" + row_count + ", 1)");

row.Cells.Add("Column (" + row_count + ", 2)");

row.Cells.Add("Column (" + row_count + ", 3)");

}

//Add the table into paragraphs collection of FloatingBox

fb.Paragraphs.Add(table);

doc.Save(myDir+"FoatingBox_out.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for the response.


This seems to be what I am looking for although I have a couple of questions.

1/ When using only absolute positioning, how can I add elements to a second page?
2/ If I add a table in a floating box and this table is very large, how can I get it to split onto a second page?

Thanks
clericns:
1/ When using only absolute positioning, how can I add elements to a second page?
Hi Neil,

In order to add elements to second/subsequent page, add a page to pages collection of PDF file and then add the element to paragraphs collection of that page.
clericns:
2/ If I add a table in a floating box and this table is very large, how can I get it to split onto a second page?
The tables are added in flow layout i.e. if the table height is greater than page height, the contents will automatically flow to subsequent pages. In case you encounter any issue, please share the code snippet so that we can test the scenario at our end.
Hi

If you are positioning the table by putting it in a floating box, when the table gets to the bottom of the page it truncates rather than continuing on second page.


Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
Dim sec2 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()

strAddress(0) = "aaaaaaaaaaaaaa"
strAddress(1) = "bbbbbbbbbbbbbbbb"
strAddress(2) = "cccccccccccccccc"
strAddress(3) = "ddddddddddddddd"
strAddress(4) = "eeeeeeeeeeeeeeeee"
strAddress(5) = "fffffffffffffffffff"

box1 = New Aspose.Pdf.Generator.FloatingBox()
sec1.Paragraphs.Add(box1)
box1.Left = 0
box1.Top = 730

tablex.ColumnWidths = "400"

For i As Integer = 1 To strAddress.Count - 1
row = tablex.Rows.Add()

cell2 = row.Cells.Add()
text = New Aspose.Pdf.Generator.Text(sec1, strAddress(i))
cell2.Paragraphs.Add(text)
Next

box1.Paragraphs.Add(tablex)

Hi Neil,


Thanks for sharing the code snippet.

I have updated the code according to new DOM and have observed that table inserted inside the FloatingBox drops/cuts over first page and is not flown to subsequent page. Please note that FloatingBox has obsolete positioning and can have position specific to particular page region. When page ends, the floatingbox is also truncated. However when contents/elements are added inside paragraphs collection of PDF page (sec1.Paragraphs.Add(tablex)), they automatically flow to subsequent pages.

Following is the code snippet which I have used to notice the behavior.

[VB.NET]

Dim pdf1 As Aspose.Pdf.Document = New Aspose.Pdf.Document()<o:p></o:p>

Dim sec1 As Aspose.Pdf.Page = pdf1.Pages.Add()

Dim sec2 As Aspose.Pdf.Page = pdf1.Pages.Add()

' strAddress(0) = "aaaaaaaaaaaaaa"

' strAddress(1) = "bbbbbbbbbbbbbbbb"

' strAddress(2) = "cccccccccccccccc"

'strAddress(3) = "ddddddddddddddd"

'strAddress(4) = "eeeeeeeeeeeeeeeee"

'strAddress(5) = "fffffffffffffffffff"

Dim box1 As Aspose.Pdf.FloatingBox = New Aspose.Pdf.FloatingBox()

sec1.Paragraphs.Add(box1)

box1.Left = 0

box1.Top = 530

Dim tablex As Aspose.Pdf.Table = New Aspose.Pdf.Table()

tablex.ColumnWidths = "400"

tablex.Border = New Aspose.Pdf.BorderInfo(BorderSide.All, Aspose.Pdf.Color.Black)

tablex.DefaultCellBorder = New Aspose.Pdf.BorderInfo(BorderSide.All, Aspose.Pdf.Color.Red)

For i As Integer = 1 To 100 - 1

Dim row As Aspose.Pdf.Row = tablex.Rows.Add()

Dim cell2 As Aspose.Pdf.Cell = row.Cells.Add()

Dim text As Aspose.Pdf.Text.TextFragment = New Aspose.Pdf.Text.TextFragment("" & i)

cell2.Paragraphs.Add(text)

Next

box1.Paragraphs.Add(tablex)

pdf1.Save("c:/pdftest/PositioningOfElements.pdf")