Skip to content

Commit

Permalink
Merge pull request #270 from VijayadharshiniMathiyalagan/ES-893363-Ho…
Browse files Browse the repository at this point in the history
…w-to-convert-text-to-a-table-using-comma-as-delimiter-in-a-Word-document

Add the sample for convert text to table in a word document
  • Loading branch information
MohanaselvamJothi authored Oct 28, 2024
2 parents d804c95 + df8fedf commit 9957978
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Tables/Convert-text-to-table/Convert-text-to-table.sln
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.12.35309.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-text-to-table", "Convert-text-to-table\Convert-text-to-table.csproj", "{2F260422-C713-4E9E-81B4-F78B4ED1DBBC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2F260422-C713-4E9E-81B4-F78B4ED1DBBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F260422-C713-4E9E-81B4-F78B4ED1DBBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F260422-C713-4E9E-81B4-F78B4ED1DBBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F260422-C713-4E9E-81B4-F78B4ED1DBBC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A7DA04A5-1071-4910-8C36-F1F9F3C3548F}
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>Convert_text_to_table</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 @@

69 changes: 69 additions & 0 deletions Tables/Convert-text-to-table/Convert-text-to-table/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.Drawing;

using (FileStream inputStream = new FileStream("Data/Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// Open the input Word document.
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
{
// Access the body of the first section.
WTextBody body = document.Sections[0].Body;
// Retrieve the paragraphs that is need to be converted into a table.
WParagraph para1 = body.Paragraphs[0];
WParagraph para2 = body.Paragraphs[1];
WParagraph para3 = body.Paragraphs[2];
WParagraph para4 = body.Paragraphs[3];
// Combine the text from the paragraphs, each separated by a new line.
string text = para1.Text + "\r\n" + para2.Text + "\r\n" + para3.Text + "\r\n" + para4.Text;
// Convert the combined text into a table.
IWTable table = ConvertTextToTable(document, text);
// Get the index of the first paragraph to insert the table at the correct position.
int index = body.ChildEntities.IndexOf(para1);
// Remove the selected paragraphs from the document.
body.ChildEntities.Remove(para1);
body.ChildEntities.Remove(para2);
body.ChildEntities.Remove(para3);
body.ChildEntities.Remove(para4);
// Insert the newly created table at the location of the first paragraph.
body.ChildEntities.Insert(index, table);
// Save the modified document with the table inserted.
using (FileStream outputStream = new FileStream("Output/Output.docx", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream, FormatType.Docx);
}
}
}

/// <summary>
/// Converts the provided text into a table format with rows and columns.
/// </summary>
IWTable ConvertTextToTable(WordDocument document, string text)
{
// Split the text into rows based on line breaks.
string[] rows = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
// Create a new table in the document.
IWTable table = new WTable(document);
// Determine the number of rows and columns based on the text structure.
int rowCount = rows.Length;
int colCount = rows[0].Split(new char[] { ',' }).Length;
// Initialize the table with the appropriate number of rows and columns.
table.ResetCells(rowCount, colCount);
// Populate the table with the text by iterating through each row and column.
for (int i = 0; i < rowCount; i++)
{
// Split the current row into columns using a comma as the delimiter.
string[] columns = rows[i].Split(new char[] { ',' });
for (int j = 0; j < colCount; j++)
{
// Add a paragraph to the cell and append the text.
table[i, j].AddParagraph().AppendText(columns[j].Trim());
}
}
// Set the table's border color to black.
table.TableFormat.Borders.Color = Color.Black;
// Set the table's border style to single line.
table.TableFormat.Borders.BorderType = BorderStyle.Single;
// Return the created table.
return table;
}

0 comments on commit 9957978

Please sign in to comment.