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

Free Support Forum - aspose.com

How can I position new table on page?

Hello,

How does table positioning works?

I’ve used code snippet from your documentation https://docs.aspose.com/pdf/net/create-form/

I want to position table somehow using relative or absolute coordinates, it doesn’t matter for me. If I use table.Left and table.Top properties, my table does move but it still has some strange offset (please check the screenshoot attached). Additionally my table.ColumnWidths doesn’t work too. I expect each column to be exactly 100, but the result is defferent.

Can you please help me with this questions? Here is code example and files I’ve used.

using (Aspose.Pdf.Document document = new Aspose.Pdf.Document(@"C://sample.pdf"))
        {
            Aspose.Pdf.Page page = document.Pages[1];
            Aspose.Pdf.Table table = new Aspose.Pdf.Table();

            table.ColumnWidths = "100 100 100";
            page.Paragraphs.Add(table);

            table.DefaultColumnWidth = "100";

            table.Left = 200;
            table.Top = 400;

            Row r1 = table.Rows.Add();
            Cell c1 = r1.Cells.Add();
            Cell c2 = r1.Cells.Add();
            Cell c3 = r1.Cells.Add();

            table.Border = new BorderInfo(BorderSide.All, Color.Green);
            table.DefaultCellBorder = new BorderInfo(BorderSide.All, Color.Blue);

            RadioButtonField rf = new RadioButtonField(page);
            rf.PartialName = "radio";
            document.Form.Add(rf, 1);

            RadioButtonOptionField opt1 = new RadioButtonOptionField();
            RadioButtonOptionField opt2 = new RadioButtonOptionField();
            RadioButtonOptionField opt3 = new RadioButtonOptionField();

            opt1.OptionName = "Item1";
            opt2.OptionName = "Item2";
            opt3.OptionName = "Item3";

            opt1.Width = 15;
            opt1.Height = 15;
            opt2.Width = 15;
            opt2.Height = 15;
            opt3.Width = 15;
            opt3.Height = 15;

            rf.Add(opt1);
            rf.Add(opt2);
            rf.Add(opt3);

            opt1.Border = new Border(opt1);
            opt1.Border.Width = 1;
            opt1.Border.Style = BorderStyle.Solid;
            opt1.Characteristics.Border = System.Drawing.Color.Black;
            opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
            opt1.Caption = new TextFragment("Item1");

            opt2.Border = new Border(opt1);
            opt2.Border.Width = 1;
            opt2.Border.Style = BorderStyle.Solid;
            opt2.Characteristics.Border = System.Drawing.Color.Black;
            opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
            opt2.Caption = new TextFragment("Item2");

            opt3.Border = new Border(opt1);
            opt3.Border.Width = 1;
            opt3.Border.Style = BorderStyle.Solid;
            opt3.Characteristics.Border = System.Drawing.Color.Black;
            opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
            opt3.Caption = new TextFragment("Item3");

            c1.Paragraphs.Add(opt1);
            c2.Paragraphs.Add(opt2);
            c3.Paragraphs.Add(opt3);

            document.Save(@"C://result.pdf");
        }

result.png (21.2 KB)
sample.zip (7.6 KB)

@AlexBilakovskyi

You can try two different ways to change the table position as per your needs. You can either add the table in FloatingBox and position it as per your desire OR you can change top/left margins of table as you are doing already. For FloatingBox functionality, the code sample is below:

Document doc = new Document();
Page page = doc.Pages.Add();
FloatingBox floatingBox = new FloatingBox();
floatingBox.Left = 200;
floatingBox.Top = 400;

// Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
// Add the table in paragraphs collection of the desired section
floatingBox.Paragraphs.Add(tab1);
// Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);
tab1.HorizontalAlignment = HorizontalAlignment.Center;
// Set with column widths of the table
tab1.ColumnWidths = "100%";

// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();

row1.Cells.Add("Table in Footer Section");
row1.BackgroundColor = Color.Gray;
// Set the row span value for first row as 2
tab1.Rows[0].Cells[0].Alignment = HorizontalAlignment.Center;
tab1.Rows[0].Cells[0].DefaultCellTextState.ForegroundColor = Color.Cyan;
tab1.Rows[0].Cells[0].DefaultCellTextState.Font = FontRepository.FindFont("Helvetica");

page.Paragraphs.Add(floatingBox);
doc.Save(dataDir + "TableinFloatingBox.pdf");

Furthermore, please note that API also honors the values of page margins while positioning any element inside it. In case you want to add table at 200 points from left, you need to consider the left margin value as well. If left margin value is 72, you can do that as below:

 table.Left = 200 - page.PageInfo.Margin.Left;

Please also note that measurement unit in Aspose.PDF is point not pixel where 1 inch = 72 points. Please feel free to let us know in case you need more information.

@asad.ali

Thanks for quick response,

measurement unit in Aspose.PDF is point not pixel

That information really helped a lot.

Thank you!

1 Like