Skip to content

Commit 022d376

Browse files
committed
Added the sample for field.
1 parent 8e7db48 commit 022d376

File tree

79 files changed

+74886
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+74886
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Diagnostics;
2+
using Fields.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Syncfusion.DocIO;
5+
using Syncfusion.DocIO.DLS;
6+
7+
namespace Fields.Controllers
8+
{
9+
public class HomeController : Controller
10+
{
11+
private readonly ILogger<HomeController> _logger;
12+
13+
public HomeController(ILogger<HomeController> logger)
14+
{
15+
_logger = logger;
16+
}
17+
18+
public IActionResult AddField()
19+
{
20+
using (FileStream fileStream = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read))
21+
{
22+
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
23+
24+
foreach (WSection section in document.Sections)
25+
{
26+
WParagraph footerParagraph = (WParagraph)section.HeadersFooters.Footer.AddParagraph();
27+
28+
footerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
29+
30+
footerParagraph.AppendText("Page ");
31+
footerParagraph.AppendField("Page", FieldType.FieldPage);
32+
33+
footerParagraph.AppendText(" of ");
34+
footerParagraph.AppendField("NumPages", FieldType.FieldNumPages);
35+
}
36+
37+
return CreateFileResult(document, "AddField.docx");
38+
}
39+
}
40+
41+
public IActionResult UpdateField()
42+
{
43+
using (FileStream fileStream = new FileStream("Data\\Input.docx", FileMode.Open, FileAccess.Read))
44+
{
45+
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
46+
47+
document.UpdateDocumentFields();
48+
49+
return CreateFileResult(document, "UpdateField.docx");
50+
}
51+
}
52+
53+
public IActionResult UnlinkField()
54+
{
55+
using (FileStream fileStream = new FileStream("Data\\InputTemplate.docx", FileMode.Open, FileAccess.Read))
56+
{
57+
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
58+
59+
foreach (WSection section in document.Sections)
60+
{
61+
foreach (WParagraph paragraph in section.Body.Paragraphs)
62+
{
63+
for (int i = 0; i < paragraph.Items.Count; i++)
64+
{
65+
if (paragraph.Items[i] is WField field)
66+
{
67+
if (field.FieldType == FieldType.FieldDate)
68+
{
69+
field.Unlink();
70+
}
71+
}
72+
}
73+
}
74+
}
75+
76+
return CreateFileResult(document, "UnlinkField.docx");
77+
}
78+
}
79+
80+
private FileStreamResult CreateFileResult(WordDocument document, string fileName)
81+
{
82+
MemoryStream outputStream = new MemoryStream();
83+
document.Save(outputStream, FormatType.Docx);
84+
outputStream.Position = 0;
85+
return File(outputStream, "application/docx", fileName);
86+
}
87+
88+
public IActionResult Index()
89+
{
90+
return View();
91+
}
92+
93+
public IActionResult Privacy()
94+
{
95+
return View();
96+
}
97+
98+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
99+
public IActionResult Error()
100+
{
101+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
102+
}
103+
}
104+
}

Videos/Fields/Data/Input.docx

129 KB
Binary file not shown.
8.85 KB
Binary file not shown.

Videos/Fields/Data/Template.docx

47.5 KB
Binary file not shown.

Videos/Fields/Fields.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="31.1.22" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Fields.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}

Videos/Fields/Program.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Fields
2+
{
3+
public class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
builder.Services.AddControllersWithViews();
11+
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (!app.Environment.IsDevelopment())
16+
{
17+
app.UseExceptionHandler("/Home/Error");
18+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
19+
app.UseHsts();
20+
}
21+
22+
app.UseHttpsRedirection();
23+
app.UseStaticFiles();
24+
25+
app.UseRouting();
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllerRoute(
30+
name: "default",
31+
pattern: "{controller=Home}/{action=Index}/{id?}");
32+
33+
app.Run();
34+
}
35+
}
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:2118",
8+
"sslPort": 44354
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5021",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7051;http://localhost:5021",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}

Videos/Fields/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# How to Work with Fields in Word Documents Using the .NET Word Library
2+
3+
This repository provides an example of how to work with fields in a Word document using the **Syncfusion .NET Word Library (DocIO)**. It demonstrates how to add field, update field, and unlink field in a Word document.
4+
5+
## Process behind Field Integration
6+
7+
This sample demonstrates how Word fields can be used to automate dynamic content in documents. Fields act as placeholders that display information such as page numbers, total pages, or dates, and they update automatically when the document changes.
8+
9+
Using the Syncfusion DocIO library, you can:
10+
11+
- Add fields like page numbers into headers or footers for consistent formatting.
12+
- Update fields to refresh their values after edits or content changes.
13+
- Unlink fields to convert them into static text when you no longer need dynamic updates.
14+
15+
## Steps to use the sample
16+
17+
1. Open the ASP.NET Core application where the Syncfusion DocIO package is installed.
18+
2. Run the application and click the following buttons:
19+
- **AddField**: Creates a Word document with page number fields.
20+
- **UpdateField**: Updates all fields in the document.
21+
- **UnlinkField**: Converts date fields to static text.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div>
6+
<h2 style="margin-bottom: 20px; font-size: 22px;">Working with Fields in Word Document</h2>
7+
<div>
8+
<button style="width: 180px; margin-bottom: 20px; height: 40px; display: block; font-size: 18px;"
9+
onclick="location.href='@Url.Action("AddField", "Home")'">
10+
Add Field
11+
</button>
12+
<button style="width: 180px; margin-bottom: 20px; height: 40px; display: block; font-size: 18px;"
13+
onclick="location.href='@Url.Action("UpdateField", "Home")'">
14+
Update Field
15+
</button>
16+
<button style="width: 180px; margin-bottom: 20px; height: 40px; display: block; font-size: 18px;"
17+
onclick="location.href='@Url.Action("UnlinkField", "Home")'">
18+
Unlink Field
19+
</button>
20+
</div>
21+
</div>

0 commit comments

Comments
 (0)