How do you set column widths in a PDF?

Hello, I’m attempting to format a PDF document that I’m creating in memory, but so far I am unable to figure out how to do this.

Here is my code:

Aspose.Pdf.License asposeLicense = SetAsposePdfLicense();

var document = new Aspose.Pdf.Document();
document.DisplayDocTitle = true;

// Create document and page
document.Pages.Add();
PageCollection pages = document.Pages;
Page page = pages[1];

TextStamp textStamp = new TextStamp("Comments");
textStamp.TopMargin = 10;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;

// Create table to display data
Table table = new Aspose.Pdf.Table
{
    ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,
    ColumnWidths = "60 20 20",
    Border = new BorderInfo(BorderSide.All, .5f, Color.FromRgb(System.Drawing.Color.Black)),
    DefaultCellBorder = new BorderInfo(BorderSide.All, .5f, Color.FromRgb(System.Drawing.Color.LightGray))
};

foreach (Page docPage in document.Pages)
{
    docPage.AddStamp(textStamp);
}

foreach (Comment comment in viewModel.Comments)
{
    // Add row to table
    Aspose.Pdf.Row row = table.Rows.Add();

    // Add table cells
    row.Cells.Add("col1");
    row.Cells.Add("col2");
    row.Cells.Add("col3");
}

// Add table to page
document.Pages[1].Paragraphs.Add(table);

var memoryStream = new System.IO.MemoryStream();
document.Save(memoryStream);

As you can see, I’m trying to display a list of “Comment” strings. There should be three columns, the first is 60% wide, the second is 20% wide, the third is 20% wide. But it’s not working.

Am I missing something?

@CoffeeNerd

Please try to specify it like below and let us know if it is still not working.

ColumnWidths = "60 20 20"

@asad.ali Thank you for replying. Unfortunately, that does not work: the columns are still equally sized. I have updated my original post with my current code for your review.

@CoffeeNerd

Looks like the culprit is ColumnAdjustment property. Please set it to Customized e.g.

Table tocTable = new Table()
{
    ColumnAdjustment = ColumnAdjustment.Customized,
    ColumnWidths = "90 10"
};

@asad.ali

Thank you very much, that certainly fixed my problem!

1 Like