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.
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.
clericns:
1/ When using only absolute positioning, how can I add elements to 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.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?
Hi Neil,
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")