Skip to content

Commit 20260a9

Browse files
Added sample for add rows with dynamic data into an existing table
1 parent 1ffe70f commit 20260a9

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35417.141 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-rows-with-dynamic-data", "Add-rows-with-dynamic-data\Add-rows-with-dynamic-data.csproj", "{416B2F2F-28AF-42DD-B2D8-43E539F5C009}"
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+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
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>Add_rows_with_dynamic_data</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.Pdf;
5+
6+
using (FileStream inputStream = new FileStream(@"Data/Template.docx", FileMode.Open, FileAccess.Read))
7+
{
8+
// Open an existing Word document.
9+
using (WordDocument document = new WordDocument(inputStream, FormatType.Automatic))
10+
{
11+
// Find the table with title.
12+
WTable table = document.FindItemByProperty(EntityType.Table, "Title", "Template") as WTable;
13+
// Check if the table with the specified title was found.
14+
if (table != null)
15+
{
16+
// Define new data to add to the table.
17+
List<string[]> rowData =
18+
[
19+
["3.", "Banana", "20"],
20+
["4.", "Grapes", "70"]
21+
];
22+
for (int j = 0; j < rowData.Count; j++)
23+
{
24+
string[] data = rowData[j];
25+
// Add a new row to the table.
26+
WTableRow row = table.AddRow(false);
27+
for (int i = 0; i < data.Length; i++)
28+
{
29+
string cellData = data[i];
30+
// Get the cell at the current index.
31+
WTableCell cell = row.Cells[i];
32+
// Add a paragraph to the cell.
33+
WParagraph cellParagraph = cell.AddParagraph() as WParagraph;
34+
// Add the cell data as text within the cell's paragraph.
35+
cellParagraph.AppendText(cellData);
36+
}
37+
}
38+
}
39+
using (DocIORenderer renderer = new DocIORenderer())
40+
{
41+
// Convert the Word document into a PDF document.
42+
using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
43+
{
44+
using (FileStream outputStream = new FileStream(@"Output/Result.pdf", FileMode.Create, FileAccess.Write))
45+
{
46+
// Save the converted PDF document.
47+
pdfDocument.Save(outputStream);
48+
}
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)