Skip to content

Commit 7db598f

Browse files
Merge pull request #369 from SyncfusionExamples/ES-918420-Adjust-first-column-width
ES-918420- Add the sample Adjust-first-column-width
2 parents 660b2fa + 90abe41 commit 7db598f

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adjust-first-column-width", "Adjust-first-column-width\Adjust-first-column-width.csproj", "{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {ADDC6FEE-8718-4B00-B091-6C4DC0CBF62A}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Adjust_first_column_width</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Adjust_first_column_width
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Load the input Word document from file stream
11+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
12+
{
13+
// Open the Word document
14+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
15+
{
16+
// Find all tables in the document
17+
List<Entity> tables = document.FindAllItemsByProperty(EntityType.Table, null, null);
18+
19+
// Initialize variables
20+
bool isFirstCell = false; // Flag to identify the first cell in each row
21+
WTableCell firstCellReference = new WTableCell(document); // To store the reference to the first cell
22+
float totalCellWidth = 0; // Accumulate width of cells except the first one
23+
24+
// Loop through each table found in the document
25+
foreach (WTable table in tables)
26+
{
27+
// Iterate through each row in the table
28+
foreach (WTableRow row in table.Rows)
29+
{
30+
// Reset variables for each row
31+
totalCellWidth = 0;
32+
isFirstCell = false;
33+
34+
// Iterate through each cell in the row
35+
foreach (WTableCell cell in row.Cells)
36+
{
37+
if (!isFirstCell)
38+
{
39+
// Identify the first cell in the row
40+
isFirstCell = true;
41+
firstCellReference = cell; // Store the first cell reference
42+
}
43+
else
44+
{
45+
// Add the width of remaining cells in the row
46+
totalCellWidth += cell.Width;
47+
}
48+
}
49+
50+
// Calculate the remaining width by subtracting totalCellWidth from page width
51+
float pageWidth = (table.Owner.Owner as WSection).PageSetup.ClientWidth;
52+
float remainingWidth = pageWidth - totalCellWidth;
53+
54+
// Set the width of the first cell to the remaining width
55+
firstCellReference.Width = remainingWidth;
56+
}
57+
}
58+
59+
// Save the modified document to a new file
60+
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
61+
{
62+
document.Save(docStream1, FormatType.Docx);
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)