//Creates an instance of WordDocument class (Empty Word Document) WordDocument document = new WordDocument(); //Opens an existing Word document into DocIO instance document.Open("Table.docx", FormatType.Docx); //Accesses the instance of the first section in the Word document WSection section = document.Sections[0]; //Accesses the instance of the first table in the section WTable table = section.Tables[0] as WTable; //Specifies the auto resize of table to automatically resize all cell width based on its content table.TableFormat.IsAutoResized = true; //Saves and closes the document instance document.Save("TableFormatting.docx", FormatType.Docx); document.Close(); |
I am copying the above example (or at least I think I am!):
var table = section.AddTable();
table.TableFormat.IsAutoResized = true;
I am then saving as a PDF:
var render = new DocIORenderer();
var pdfDocument = render.ConvertToPDF(document);
I am using the latest versions of the Nuget packages:
I am seeing two issues. Firstly the auto size doesn't seem to be working. Secondly, the table format doesn't seem to be respecting the right margin (the right margin is actually set to the same size as the left):
I am probably doing something wrong and would welcome any pointers.
Thanks,
Nick
Hi Nick,
We have tried to reproduce the reported problem (auto size doesn't work
and table format doesn't seem to be respecting the right margin using
DocIO) using the given details, but it works properly at our end. We suspect
that the reported problem might be due to the contents inside the input Word
document and code snippets which used at your end. So, to analyze further on
the reported problem with your requirement, could you please provide us the
following things from your end,
1. Input Word document.
2. Complete code snippets of adding table and its format in the
Word document.
3. Output PDF document generated at
your end.
Note: If you have any confidential data in your input Word document, please
replace with some dummy data and provide us the same. We just need your
document to recreate the problem you face.
Based on the above details, we will analyze further at our end and provide you
the appropriate solution at the earliest.
Regards,
Anto Nihil S
Thanks for the response, and my apologies for the delay in my reply. I don't have an input document, I am creating it from the ground up. I have a single C# class which drives the process. I declare some member variables:
private WordDocument document;
private IWSection section;
private int standardIndent = 40;
private string standardFont = "Arial";
private int standardFontSize = 12;
Then in the class constructor, I initialise the document:
// Assign the licence
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("R E D A C T E D");
// Create a new Word Document
document = new WordDocument();
//Add a new section to the document
section = document.AddSection();
// Set the margins
section.PageSetup.Margins = new MarginsF(50, 50, 50, 50);
// Tweak the built-in body text style
var bodyStyle = (Style)Style.CreateBuiltinStyle(BuiltinStyle.BodyText, document);
bodyStyle.CharacterFormat.TextColor = Color.Black;
bodyStyle.CharacterFormat.FontName = standardFont;
bodyStyle.CharacterFormat.FontSize = standardFontSize;
document.Styles.Add(bodyStyle);
// And the heading style
var headStyle = (Style)Style.CreateBuiltinStyle(BuiltinStyle.Heading1, document);
headStyle.CharacterFormat.TextColor = Color.Black;
headStyle.CharacterFormat.FontName = standardFont;
headStyle.CharacterFormat.FontSize = standardFontSize;
headStyle.CharacterFormat.Bold = true;
document.Styles.Add(headStyle);
Paragraphs are added like this:
public void AddParagraph(string text)
{
// Add the paragraph
var paragraph = section.AddParagraph();
//Applies built-in style and the standard indent for the paragraph
paragraph.ApplyStyle(BuiltinStyle.BodyText);
paragraph.ParagraphFormat.LeftIndent = standardIndent;
//Adds new text to the paragraph
paragraph.AppendText(text);
}
Tables are added like this:
public void AddTable(List<List<string>> rows)
{
var table = section.AddTable();
table.TableFormat.IsAutoResized = true;
table.IndentFromLeft = standardIndent; // THOUGHT - IS THIS PUSHING THE TABLE RIGHT?
table.TableFormat.Paddings.Top = 5;
table.TableFormat.Paddings.Bottom = 5;
table.TableFormat.Paddings.Left = 5;
table.TableFormat.Paddings.Right = 5;
var currentRow = 0;
foreach(var row in rows)
{
var thisRow = table.AddRow(false, false);
var currentColumn = 0;
foreach (var column in row)
{
var thisCell = thisRow.AddCell();
var newText = thisCell.AddParagraph().AppendText(column);
newText.CharacterFormat.FontName = standardFont;
newText.CharacterFormat.FontSize = standardFontSize;
newText.CharacterFormat.Bold = currentRow == 0; // make the first row bold
currentColumn++;
}
currentRow++;
}
table.AutoFit(AutoFitType.FitToContent); // TODO Why is this not working?
section.AddParagraph(); // To get space after the table TODO find a better way
}
Then, finally, the PDF is created:
//Create instance for DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
//Converts Word document to PDF.
PdfDocument pdfDocument = render.ConvertToPDF(document);
//Release the resources used by the Word document and DocIO Renderer objects.
render.Dispose();
document.Dispose();
//Saves the PDF file.
FileStream outputStream = new FileStream(@"c:\\Logs\\xxxx.pdf", FileMode.Create, FileAccess.Write);
pdfDocument.Save(outputStream);
//Closes the instance of PDF document object.
pdfDocument.Close();
//Dispose the instance of FileStream.
outputStream.Dispose();
I have attached a sample output file.
I hope that all makes sense - and look forward to hearing what stupid mistake I have made!
Thanks,
Nick
Hi Nick,
Regarding auto-size doesn't
seem to be working
We can reproduce the reported
issue with “Table
not resize properly while converting Word document to PDF” in our end,
and we suspect it to be a defect. We will validate this issue and update you
with more details on 20th May 2022.
Regarding table format doesn't seem to be respecting the right margin
We set the IndentFromLeft value for the table, so table moves towards the right. To fit
the table in the given margin, we suggest you skip to set the IndentFromLeft
value for the table.
Regarding
AutoFit Options
You can resize the table to fit the contents respect to the given contents
using AutoFit Option. Please
refer the UG documentation to know how to set the AutoFit Option for the table.
https://help.syncfusion.com/file-formats/docio/working-with-tables?cs-save-lang=1&cs-lang=csharp#resize-table
Please let us know if you have any other questions.
Regards,
Anto Nihil S
Hi Nick,
Still, we are validating the reported issue with “Table not resize properly
while converting Word document to PDF” on our end. We will validate this
issue and update you with more details on 24th May 2022.
Regards,
Lokesh B
Hi and thanks for the update. Looking back at the previous response.....
Regarding table format doesn't seem to be respecting the right margin
We set the IndentFromLeft value for the table, so table moves towards the right. To fit the table in the given margin, we suggest you skip to set the IndentFromLeft value for the table.
I can see what is happening here but, in my example output, you will see that I have indented the table from the left so it aligns with the other text, and doesn't "obscure" the list of heading numbers. So, simply not indenting the table (which is what I think you are suggesting) will not produce the desired result. There doesn't seem to be an IndentFromRight function, and the Width setting is read-only - so I am not sure whether it is possible to make the table narrower. I guess once the column widths can be set based on content, the problem will go away as the table will no longer be the full width of the page.
Regarding AutoFit Options
You can resize the table to fit the contents respect to the given contents using AutoFit Option. Please refer the UG documentation to know how to set the AutoFit Option for the table.
I might be missing something, but I think I am already doing this as shown in my code sample.
Thanks,
Nick
Hi Nick,
Please find the below answers for your queries,
I think I am already doing this as shown in my code sample.
Yes, you have done this properly in your code sample. As mentioned earlier,
we are validating the reported issue with “Table not resize properly while
converting Word document to PDF” on our end. We will validate this issue
and update you with more details on 24th May 2022.
I guess once the column widths can be set based on content, the problem will
go away as the table will no longer be the full width of the page.
Yes, your understanding is correct. The table does not resize properly based on
its content, so table exceeds the right margin. So, we have found that, once
the above mentioned AutoFit table based on the content issue gets resolved means
that table exceeds the right margin issue will resolved.
Regards,
Suriya Balamurugan.
Hi Nick,
We have confirmed that the reported issue
with “Table not
resized properly while converting a Word document to PDF” is a defect and
we have logged a defect report. Since you have been using our weekly release (v20.1.0.52), we will include the fix for this defect in our weekly NuGet release
which is estimated to be available on 14th June 2022.
The status of this bug can be tracked through the below link:
https://www.syncfusion.com/feedback/35159/table-not-resized-properly-while-converting-a-word-document-to-pdf
Disclaimer: “Inclusion of this solution in the weekly release may change
due to other factors including but not limited to QA checks and works
reprioritization.”
Note: If you require a patch for the reported issue in any of our
Essential Studio versions (except weekly release version), then kindly let us
know the currently installed version, so that we can provide a patch in that
version based on our SLA policy.
Regards,
Anto Nihil S
Just a note to say thank you. I look forward to seeing the fix in due course but, in the meantime, I just wanted to compliment you all on the effectiveness and speed of support. Many thanks, Nick.
Hi Nick,
We deeply regret for not including the fix for
this issue in our latest weekly NuGet release (v20.1.0.60). So, we have planned
to include this fix in our upcoming weekly NuGet release on 21st June
2022 without further delay.
Regards,
Anto
Nihil S
Thank you for the update.
Hi Nick,
Thank you for your patience.
As promised earlier, we have included the fix for the reported issue with “Table not resized properly while
converting a Word document to PDF” in our latest
weekly NuGet release (v20.1.0.61).
Please use the below link to download our latest weekly NuGet:
https://www.nuget.org/packages/Syncfusion.DocToPDFConverter.AspNet/20.1.0.61
The status of this bug task can be
tracked through the below link:
https://www.syncfusion.com/feedback/35159/table-not-resized-properly-while-converting-a-word-document-to-pdf
Note: We will include this fix in our
2022 Volume 2 release which will be available in mid of June 2022 tentatively.
Please let us know if you have any questions.
Regards,
Anto Nihil S
Hi pleased to report that I have updated the Nuget package version and the table resizing is now working as expected. Thank you for the fix and for the excellent progress updates on the journey.
Best wishes,
Nick