Skip to content

ES-918420- Add the sample Adjust-first-column-width #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adjust-first-column-width", "Adjust-first-column-width\Adjust-first-column-width.csproj", "{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ADDC6FEE-8718-4B00-B091-6C4DC0CBF62A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Adjust_first_column_width</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace Adjust_first_column_width
{
internal class Program
{
static void Main(string[] args)
{
// Load the input Word document from file stream
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
{
// Open the Word document
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
{
// Find all tables in the document
List<Entity> tables = document.FindAllItemsByProperty(EntityType.Table, null, null);

// Initialize variables
bool isFisrtCell = false; // Flag to identify the first cell in each row
WTableCell firstCellReference = new WTableCell(document); // To store the reference to the first cell
float totalCellWidth = 0; // Accumulate width of cells except the first one

// Loop through each table found in the document
foreach (WTable table in tables)
{
// Iterate through each row in the table
foreach (WTableRow row in table.Rows)
{
// Reset variables for each row
totalCellWidth = 0;
isFisrtCell = false;

// Iterate through each cell in the row
foreach (WTableCell cell in row.Cells)
{
if (!isFisrtCell)
{
// Identify the first cell in the row
isFisrtCell = true;
firstCellReference = cell; // Store the first cell reference
}
else
{
// Add the width of remaining cells in the row
totalCellWidth += cell.Width;
}
}

// Calculate the remaining width by subtracting totalCellWidth from page width
float pageWidth = (table.Owner.Owner as WSection).PageSetup.ClientWidth;
float remainingWidth = pageWidth - totalCellWidth;

// Set the width of the first cell to the remaining width
firstCellReference.Width = remainingWidth;
}
}

// Save the modified document to a new file
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
{
document.Save(docStream1, FormatType.Docx);
}
}
}
}
}
}
Loading