This example demonstrates how to calculate table width and fit table to page width.
When there are many tables in a word document and we have give them width , a width that of a word document .
First we choose a word file with tables
After clicking generate button it will launch a word document with above choosen version and fit tables to page width
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class FitTabletoPageWidthExample : WordExampleBase
#if WEB
, IUIExample
#endif
{
public string FileName;
public FitTabletoPageWidthExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public FitTabletoPageWidthExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
#if WEB
ProcessForm();
#endif
if (string.IsNullOrWhiteSpace(FileName))
{
ShowError("Please specify word file name");
return null;
}
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument(FileName);
foreach (Section section in document.Sections)
{
// Iterate through all tables in the section.
foreach (Table table in section.Tables)
{
CalculateAndFitTableToPageWidth(table);
}
}
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
private static void CalculateAndFitTableToPageWidth(Table table)
{
float tableWidth = 0.0f;
// Iterate througn all the rows to calculate the longest row.
foreach (TableRow row in table.Rows)
{
float rowWidth = 0.0f;
foreach (TableCell cell in row.Cells)
{
rowWidth += cell.Width;
// Make sure that tableWidth is set to the longest row.
if (rowWidth > tableWidth)
tableWidth = rowWidth;
}
}
float containerWidth;
TableCell parentTableCell = (TableCell)table.GetAncestor(NodeType.TableCell);
if (parentTableCell == null)
{
// Get the parent Section object.
Section section = (Section)table.GetAncestor(NodeType.Section);
// Calculate the container width based on the page size and margins.
containerWidth = section.PageSetup.PageSize.Width - (section.PageSetup.Margins.Left + section.PageSetup.Margins.Right);
}
else
{
// The container width is the width of the parent table cell.
containerWidth = parentTableCell.Width;
}
// Update each cell in the row to the width of the container.
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
// Calculate the ratio of each cell by dividing its width to the table's width and translate this ratio to the container width.
float ratio = cell.Width / tableWidth;
cell.Width = ratio * containerWidth;
//cell.Width *= 0.5f;
}
}
}
#if WEB
void ProcessForm()
{
FileName = GetPostFile("SourceFile");
}
#endif
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
#If WEB Then
Friend Class FitTabletoPageWidthExample
Inherits WordExampleBase
Implements IUIExample
#Else
Friend Class FitTabletoPageWidthExample
Inherits WordExampleBase
#End If
Public FileName As String
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
#If WEB Then
ProcessForm()
#End If
If String.IsNullOrWhiteSpace(FileName) Then
ShowError("Please specify word file name")
Return Nothing
End If
' Create a new instance of PdfDocument class.
Dim document As New WordDocument(FileName)
For Each section As Section In document.Sections
' Iterate through all tables in the section.
For Each table As Table In section.Tables
CalculateAndFitTableToPageWidth(table)
Next table
Next section
'INSTANT VB NOTE: The variable fileName was renamed since Visual Basic does not handle local variables named the same as class members well:
Dim fileName_Renamed As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName_Renamed, SaveAsFormat)
' Close the document.
document.Close()
Return fileName_Renamed
End Function
Private Shared Sub CalculateAndFitTableToPageWidth(ByVal table As Table)
Dim tableWidth As Single = 0.0F
' Iterate througn all the rows to calculate the longest row.
For Each row As TableRow In table.Rows
Dim rowWidth As Single = 0.0F
For Each cell As TableCell In row.Cells
rowWidth += cell.Width
' Make sure that tableWidth is set to the longest row.
If rowWidth > tableWidth Then
tableWidth = rowWidth
End If
Next cell
Next row
Dim containerWidth As Single
Dim parentTableCell As TableCell = CType(table.GetAncestor(NodeType.TableCell), TableCell)
If parentTableCell Is Nothing Then
' Get the parent Section object.
Dim section As Section = CType(table.GetAncestor(NodeType.Section), Section)
' Calculate the container width based on the page size and margins.
containerWidth = section.PageSetup.PageSize.Width - (section.PageSetup.Margins.Left + section.PageSetup.Margins.Right)
Else
' The container width is the width of the parent table cell.
containerWidth = parentTableCell.Width
End If
' Update each cell in the row to the width of the container.
For Each row As TableRow In table.Rows
For Each cell As TableCell In row.Cells
' Calculate the ratio of each cell by dividing its width to the table's width and translate this ratio to the container width.
Dim ratio As Single = cell.Width / tableWidth
cell.Width = ratio * containerWidth
'cell.Width *= 0.5f;
Next cell
Next row
End Sub
#If WEB Then
Private Sub ProcessForm()
FileName = GetPostFile("SourceFile")
End Sub
#End If
End Class
End Namespace