Skip to content

Commit b7da0c7

Browse files
Merge pull request #505 from SyncfusionExamples/1002527-word-to-text
1002527 - How to Convert a Word Document to a Text File Using the .NET Word Library
2 parents 4c27fb9 + 3e816ed commit b7da0c7

File tree

78 files changed

+74908
-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.

78 files changed

+74908
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using System.Diagnostics;
5+
using WordToText.Models;
6+
7+
namespace WordToText.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 WordToText()
19+
{
20+
// Loads an existing Word document into DocIO instance
21+
FileStream fileStreamPath = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
22+
23+
// Load the document into DocIO
24+
WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);
25+
26+
// Create an output memory stream to hold the document content
27+
MemoryStream outputStream = new MemoryStream();
28+
29+
// Save the new document to the output stream
30+
document.Save(outputStream, FormatType.Txt);
31+
32+
// Close the document
33+
document.Close();
34+
35+
//Return the output file
36+
return File(outputStream, "application/txt", "WordToText.txt");
37+
}
38+
39+
public IActionResult TextToWord()
40+
{
41+
//Loads an existing Word document into DocIO instance
42+
FileStream fileStreamPath = new FileStream("Data\\Template.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
43+
44+
// Load the document into DocIO
45+
WordDocument document = new WordDocument(fileStreamPath, FormatType.Txt);
46+
47+
//Create an output memory stream to hold the document content
48+
MemoryStream outputStream = new MemoryStream();
49+
50+
// Save the new document to the output stream
51+
document.Save(outputStream, FormatType.Docx);
52+
53+
//Close the document
54+
document.Close();
55+
56+
//Return the output file
57+
return File(outputStream, "application/docx", "TextToWord.docx");
58+
}
59+
60+
public IActionResult ExtractPlainText()
61+
{
62+
//Loads an existing Word document into DocIO instance
63+
FileStream fileStreamPath = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
64+
65+
// Load the document into DocIO
66+
WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);
67+
68+
//Get the plain text from the document
69+
string text = document.GetText();
70+
71+
//Create a new Word document to hold the extracted text
72+
WordDocument newdocument = new WordDocument();
73+
74+
//Adds new section
75+
IWSection section = newdocument.AddSection();
76+
77+
//Adds new paragraph
78+
IWParagraph paragraph = section.AddParagraph();
79+
80+
//Append the extracted text to the new document.
81+
paragraph.AppendText(text);
82+
83+
//Create an output stream for the new document
84+
MemoryStream outputStream = new MemoryStream();
85+
86+
//Save the new document to the output stream
87+
newdocument.Save(outputStream, FormatType.Docx);
88+
89+
//Close the document
90+
newdocument.Close();
91+
92+
//Return the output file
93+
return File(outputStream, "application/docx", "ExtractPlainText.docx");
94+
}
95+
96+
public IActionResult Index()
97+
{
98+
return View();
99+
}
100+
101+
public IActionResult Privacy()
102+
{
103+
return View();
104+
}
105+
106+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
107+
public IActionResult Error()
108+
{
109+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
110+
}
111+
}
112+
}
15.5 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Northwind Database
2+
3+
The Northwind sample database (Northwind.mdb) is included with all versions of Access. It provides data you can experiment with and database objects that demonstrate features you might want to implement in your own databases. Using Northwind, you can become familiar with how a relational database is structured and how the database objects work together to help you enter, store, manipulate, and print your data..
4+
5+
It contains the following detailed information:
6+
1. Suppliers/Vendors of Northwind � who supply to the company.
7+
2. Customers of Northwind � who buy from Northwind
8+
3. Employee details of Northwind traders � who work for Northwind
9+
4. The product information � the products that Northwind trades in
10+
5. The inventory details � the details of the inventory held by Northwind traders.
11+
6. The shippers � details of the shippers who ship the products from the traders to the end-customers
12+
7. PO transactions i.e Purchase Order transactions � details of the transactions taking place between vendors & the company.
13+
8. Sales Order transaction � details of the transactions taking place between the customers & the company.
14+
9. Inventory transactions � details of the transactions taking place in the inventory
15+
10. Invoices � details of the invoice raised against the order.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace WordToText.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}

Videos/WordToText/Program.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace WordToText
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:3025",
8+
"sslPort": 44347
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5214",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7040;http://localhost:5214",
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/WordToText/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# How to Convert a Word Document to a Text File Using the .NET Word Library
2+
3+
This repository provides an example of how to convert a Word document to a text file using the **Syncfusion .NET Word Library (DocIO)**. It also demonstrates converting a text file back to a Word document and extracting plain text from a Word document.
4+
5+
## Process behind Field Integration
6+
7+
This sample shows how you can easily switch between Word and text formats using the Syncfusion DocIO library. These conversions are essential for scenarios such as storing content in a lightweight text format for efficient processing or extracting text for indexing and search operations.
8+
Using the Syncfusion DocIO library, you can:
9+
10+
- Convert a Word document (.docx) to a plain text file (.txt).
11+
- Convert a text file (.txt) back to a Word document (.docx).
12+
- Extract plain text from a Word document and save it into a new Word file
13+
14+
## Steps to use the sample
15+
16+
1. Open the ASP.NET Core application where the Syncfusion DocIO package is installed.
17+
2. Run the application and click the following buttons:
18+
- **Word to Text**: Converts a Word document to a plain text file.
19+
- **Text to Word**: Converts a text file back to a Word document.
20+
- **Extract Plain Text**: Extracts text from a Word document and saves it into a new Word document.
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"> Text Conversions </h2>
7+
<div>
8+
<button style="width: 200px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
9+
onclick="location.href='@Url.Action("WordToText", "Home")'">
10+
Word to Text
11+
</button>
12+
<button style="width: 200px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
13+
onclick="location.href='@Url.Action("TextToWord", "Home")'">
14+
Text to Word
15+
</button>
16+
<button style="width: 200px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
17+
onclick="location.href='@Url.Action("ExtractPlainText", "Home")'">
18+
Extract Plain Text
19+
</button>
20+
</div>
21+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>

0 commit comments

Comments
 (0)