So I’m using linq reporting and inserting html (that comes from a wysiwyg editor) in a word document.
I had the problem of image getting out of the word page, and have been able to fix it using the code proposed in the previous post (thank you) that post process the generated word.
Currently I’ve the same kind of issue but with an html table that can be created in the wysiwig editor, and ends up in the html inserted in the word document by the linq reporting engine. The html table is too large and get out of the word page, and the content is not fully visible.
Do you have a way to limit the table width, and by extension any other html code, so that it fit in the word document and doesn’t expand out of the page ?
@Fabske You can use Table.AutoFit method to autofit the table to the window (page):
Document doc = new Document(@"C:\Temp\in.docx");
foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
t.AutoFit(AutoFitBehavior.AutoFitToWindow);
doc.Save(@"C:\\Temp\\out.docx");
Sometimes such approach does not give good results since page is too narrow to fit the tables content. In this case it is better to widen page to fit whole table. In such case you can use code like the following:
Document doc = new Document(@"C:\Temp\in.docx");
// Determine the maximum table width using LayoutEnumerator.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
double maxTableWidth = 0;
foreach (Section s in doc.Sections)
{
foreach (Table t in s.Body.Tables)
{
enumerator.Current = collector.GetEntity(t.FirstRow.FirstCell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Row)
enumerator.MoveParent();
maxTableWidth = Math.Max(maxTableWidth, enumerator.Rectangle.Width);
}
}
PageSetup ps = doc.FirstSection.PageSetup;
double pageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
double pageHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;
if (pageWidth < maxTableWidth)
ps.PageWidth = maxTableWidth + ps.LeftMargin + ps.RightMargin;
// Update page layout is required since LayoutCollector and LayoutEnumerator were used.
doc.UpdatePageLayout();
doc.Save(@"C:\Temp\out.pdf");