Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sun Oct 02, 2022 11:44 pm

Hi guys

I've noticed an issue with tables that have different cell widths per row for all versions after 10.4.5. I'm currently testing with 10.9.6

The example below creates a table, the first row has one column with 100% width, the second row has 2 columns with 50% width each

var row = table.AddRow(1);
row.Cells[0].SetCellWidth(100, Spire.Doc.CellWidthType.Percentage);

var row2 = table.AddRow(2);
row2.Cells[0].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);
row2.Cells[1].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);

Outputting this to DOCX or PDF worked fine with 10.4.5 (first screenshot attached)

After 10.4.5, DOCX output is fine but PDF output distorts the column widths for the second row (second screenshot attached) - it looks more like 70/30 instead of 50/50 width

I've found that I can resolve it by NOT setting column width to 100% for rows that only have a single cell, but thought I would mention it incase it's a bug

formsbyair
 
Posts: 9
Joined: Tue Mar 29, 2016 8:13 am

Mon Oct 03, 2022 10:49 am

Hello,

Thanks for your inquiry.
According to your description and code, I have reproduced your scenario when I used the Spire.Doc10.9.6 to test. To achieve your requirement, you need to add the following code to set the width of table.
--- table.PreferredWidth = new PreferredWidth(WidthType.Percentage, (short)100);----
And I put the complete code snippet below for your reference. If you have any issue, just feel free to contact us.

Code: Select all
Table table = new Table(doc);
            //Set the width of table
            table.PreferredWidth = new PreferredWidth(WidthType.Percentage, (short)100);

            table.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;
            var row = table.AddRow(1);
            row.Cells[0].SetCellWidth(100, Spire.Doc.CellWidthType.Percentage);

            //Add a paragraph
            Paragraph para1 = row.Cells[0].AddParagraph();
            //Append text in the paragraph
            para1.AppendText("Test");

           
           
            var row2 = table.AddRow(2);
           
            row2.Cells[0].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);
            row2.Cells[1].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);

            //Add a paragraph
            Paragraph row2Para1 = row2.Cells[0].AddParagraph();
            //Append text in the paragraph
            row2Para1.AppendText("First Name");

            //Add a paragraph
            Paragraph row2Para2 = row2.Cells[1].AddParagraph();
            //Append text in the paragraph
            row2Para2.AppendText("Shaun");


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Oct 03, 2022 8:36 pm

Thanks for your reply

I was already setting table.PreferredWidth to 100 but hadn't included that in my original sample code.

I've pasted in a full sample below.

With 10.4.5 I get the expected output for DOCX and PDF format

With 10.9.6 the DOCX output is fine, but the PDF output distorts the column widths (see attached image)

Code: Select all
var document = new Spire.Doc.Document();

var section = document.AddSection();

var table = section.AddTable(false);
//Set the width of table
table.PreferredWidth = new PreferredWidth(WidthType.Percentage, (short)100);

table.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;
var row = table.AddRow(1);
row.Cells[0].SetCellWidth(100, Spire.Doc.CellWidthType.Percentage);

//Add a paragraph
var para1 = row.Cells[0].AddParagraph();
//Append text in the paragraph
para1.AppendText("Test");

var row2 = table.AddRow(2);

row2.Cells[0].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);
row2.Cells[1].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);

//Add a paragraph
var row2Para1 = row2.Cells[0].AddParagraph();
//Append text in the paragraph
row2Para1.AppendText("First Name");

//Add a paragraph
var row2Para2 = row2.Cells[1].AddParagraph();
//Append text in the paragraph
row2Para2.AppendText("Shaun");

var docXStream = new MemoryStream();

document.SaveToStream(docXStream, Spire.Doc.FileFormat.PDF);

formsbyair
 
Posts: 9
Joined: Tue Mar 29, 2016 8:13 am

Tue Oct 04, 2022 10:03 am

Hello,

Thanks for your feedback.
After further investigation, I have reproduced your issue using your code, and I have logged it into our bug system with the number SPIREDOC-8540. Our development team will investigation and fix it. Once it is fixed, I’ll inform you in time.
In addition, I found the differences of code between us, I directly used the “saveToFile” method to save the result document as Pdf document, however, you used the “saveToStream” method to save the result document to stream. Therefore, I didn’t reproduce your issue yesterday. Before fixing this issue, you can use the “saveToFile” method to achieve your requirement. I put the complete code below for your reference.

Code: Select all
var document = new Spire.Doc.Document();

            var section = document.AddSection();

            var table = section.AddTable(false);
            //Set the width of table
            table.PreferredWidth = new PreferredWidth(WidthType.Percentage, (short)100);

            table.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;
            var row = table.AddRow(1);
            row.Cells[0].SetCellWidth(100, Spire.Doc.CellWidthType.Percentage);

            //Add a paragraph
            var para1 = row.Cells[0].AddParagraph();
            //Append text in the paragraph
            para1.AppendText("Test");

            var row2 = table.AddRow(2);

            row2.Cells[0].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);
            row2.Cells[1].SetCellWidth(50, Spire.Doc.CellWidthType.Percentage);

            //Add a paragraph
            var row2Para1 = row2.Cells[0].AddParagraph();
            //Append text in the paragraph
            row2Para1.AppendText("First Name");

            //Add a paragraph
            var row2Para2 = row2.Cells[1].AddParagraph();
            //Append text in the paragraph
            row2Para2.AppendText("Shaun");

            var docXStream = new MemoryStream();

            document.SaveToFile(@"../../data/result2_10.9.6.pdf", Spire.Doc.FileFormat.PDF);


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Tue Nov 08, 2022 9:26 am

Hello,

Thanks for your patience!
Glad to inform you that we just released Spire.Doc Pack(Hot Fix) Version:10.11.0 which fixes the issue with SPIREDOC-8540.
Please download the new version from the following links to test.

Website download link: https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html
Nuget download link: https://www.nuget.org/packages/Spire.Doc/10.11.0

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc