Not able to set table column width in pdf file

Dear Aspose team,

Create the “output1.pdf” pdf (Aspose.pdf version 10.3) file, using the below code

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
Aspose.Pdf.Generator.Text header = new Aspose.Pdf.Generator.Text(“Table Of Contents (Module 4)”);
header.TextInfo.FontName = “Helvetica”;
header.TextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
header.TextWidth = 12;
sec1.Paragraphs.Add(header);

Aspose.Pdf.Generator.Text header2 = new Aspose.Pdf.Generator.Text(" ");
header2.TextInfo.FontName = “Helvetica”;
header2.TextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
header2.TextWidth = 12;
sec1.Paragraphs.Add(header2);

Aspose.Pdf.Generator.Text header3 = new Aspose.Pdf.Generator.Text(“Sections not appearing in the table of contents are not applicable”);
header3.TextInfo.FontName = “Helvetica”;
header3.TextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
header3.TextWidth = 12;
sec1.Paragraphs.Add(header3);

Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();
sec1.Paragraphs.Add(tab1);

tab1.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Center;
tab1.ColumnWidths = “400 100”;
tab1.DefaultColumnWidth = “500”;
tab1.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
tab1.DefaultCellTextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All);

Create the “output2.pdf” pdf (Aspose.pdf version 18.6) file, using the below code

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add();
Aspose.Pdf.Page page = (Aspose.Pdf.Page)doc.Pages[1];

Aspose.Pdf.Heading heading1 = new Aspose.Pdf.Heading(1);
Aspose.Pdf.Text.TextSegment segment1 = new Aspose.Pdf.Text.TextSegment();
segment1.Text = “Table Of Contents (Module 1)”;
segment1.TextState.FontSize = 12;
heading1.TextState.FontSize = 12;
heading1.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(“Helvetica”);
heading1.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
heading1.Segments.Add(segment1);
page.Paragraphs.Add(heading1);

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
Aspose.Pdf.Text.TextSegment segment2 = new Aspose.Pdf.Text.TextSegment();
segment2.Text = “”;
segment2.TextState.FontSize = 12;
heading2.TextState.FontSize = 12;
heading2.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(“Helvetica”);
heading2.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
heading2.Segments.Add(segment2);
page.Paragraphs.Add(heading2);

Aspose.Pdf.Heading heading3 = new Aspose.Pdf.Heading(1);
Aspose.Pdf.Text.TextSegment segment3 = new Aspose.Pdf.Text.TextSegment();
segment3.Text = “Sections not appearing in the table of contents are not applicable”;
segment3.TextState.FontSize = 12;
heading3.TextState.FontSize = 12;
heading3.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(“Helvetica”);
heading3.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
heading3.Segments.Add(segment3);
page.Paragraphs.Add(heading3);

Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
page.Paragraphs.Add(tab1);

tab1.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
tab1.ColumnWidths = “400 100”;
tab1.DefaultColumnWidth = “500”;

tab1.Alignment = Aspose.Pdf.HorizontalAlignment.Center;
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5f);
tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5f);

The problem is not able to set column width in “output2.pdf” file.

Thanks,
Naveen

output1.pdf (8.7 KB)
output2.pdf (7.6 KB)

@naveenkumar_navitas_net

Thanks for contacting support.

The new DOM (Document Object Model) approach, sets columns width according to page margins, which means table would adjust column width proportionality according to body/content area of PDF document excluding right/left margin values. If you set page margins like following, before adding table inside PDF, the column widths would be different, as you can also notice in attached PDF.

page.PageInfo.Margin = new MarginInfo(20, 20, 20, 20);

output2.pdf (2.1 KB)

You may set page margins as per your requirements and table will be rendered accordingly. In case you still face any issue, please share complete code snippet i.e. able to generate complete table. We will again test the scenario in our environment and address it accordingly.

Hi,

Now its able to sets columns width in table , thanks.

we are able create table using below code, but not able to set table border properly.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add();
Aspose.Pdf.Page page = (Aspose.Pdf.Page)doc.Pages[1];
page.PageInfo.Margin = new MarginInfo(20, 20, 20, 70);
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
page.Paragraphs.Add(tab1);

        tab1.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        tab1.ColumnWidths = "400 100";
        tab1.DefaultColumnWidth = "500";

        tab1.Alignment = Aspose.Pdf.HorizontalAlignment.Center;
        tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5f);
        tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5f);
        tab1.DefaultCellTextState.FontSize = 12;
        tab1.DefaultCellTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Times New Roman");


        Aspose.Pdf.Row row1 = tab1.Rows.Add();
        row1.DefaultCellTextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
        row1.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        row1.FixedRowHeight = 15;
        row1.Cells.Add("row1 column1");
        row1.Cells.Add("row1 column2");

        Aspose.Pdf.Row row2 = tab1.Rows.Add();
        row2.DefaultCellTextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
        row2.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        row2.FixedRowHeight = 15;
        row2.Cells.Add("row2 column1");
        row2.Cells.Add("row2 column2");

        Aspose.Pdf.Row row3 = tab1.Rows.Add();
        row3.DefaultCellTextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
        row3.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        row3.FixedRowHeight = 15;
        row3.Cells.Add("row3 column1");
        row3.Cells.Add("row3 column2");


        Aspose.Pdf.Row row4 = tab1.Rows.Add();
        row4.DefaultCellTextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
        row4.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        row4.FixedRowHeight = 15;
        row4.Cells.Add("row4 column1");
        row4.Cells.Add("row4 column2");

        Aspose.Pdf.Row row5 = tab1.Rows.Add();
        row5.DefaultCellTextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
        row5.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        row5.FixedRowHeight = 15;
        row5.Cells.Add("row5 column1");
        row5.Cells.Add("row5 column2");
        doc.Save(outFile);

Thanks,
Naveen.output.pdf (41.1 KB)

@naveenkumar_navitas_net

Thanks for getting back to us.

Would you please confirm if you are concerned about double border, appearing in the table? Please also check the screenshot which we have shared for your reference. We will log an investigation ticket accordingly.

Border.png (2.7 KB)