Visitar URL original
Merge pull request #282 from SyncfusionExamples/1064700-extraction · SyncfusionExamples/PDF-Examples@95c3f21 · GitHub
Skip to content

Commit 95c3f21

Browse files
Merge pull request #282 from SyncfusionExamples/1064700-extraction
1064700 - How to Convert PDF to Word in ASP.NET Core Using the Smart Data Extractor
2 parents c1c2ca6 + 935fd6d commit 95c3f21

79 files changed

Lines changed: 83072 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Convert_PDF_to_Word.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Diagnostics;
4+
using Syncfusion.DocIO;
5+
using Syncfusion.DocIO.DLS;
6+
using Syncfusion.SmartDataExtractor;
7+
8+
namespace Convert_PDF_to_Word.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
public IActionResult Index()
13+
{
14+
return View();
15+
}
16+
17+
/// <summary>
18+
/// Converts an entire PDF document to a Word document.
19+
/// </summary>
20+
public IActionResult ConvertPDFtoWord()
21+
{
22+
// Open the input PDF document.
23+
using FileStream stream = new FileStream("Data/Input-file.pdf", FileMode.Open, FileAccess.Read);
24+
25+
// Create a DataExtractor instance.
26+
DataExtractor extractor = new DataExtractor();
27+
28+
// Extract PDF content as a Word document.
29+
WordDocument document = extractor.ExtractDataAsWordDocument(stream);
30+
31+
// Create a memory stream for the output document.
32+
MemoryStream outputStream = new MemoryStream();
33+
34+
// Save the Word document in DOCX format.
35+
document.Save(outputStream, FormatType.Docx);
36+
37+
// Close the Word document.
38+
document.Close();
39+
40+
// Reset the stream position.
41+
outputStream.Position = 0;
42+
43+
// Return the generated DOCX file.
44+
return File(
45+
outputStream,
46+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
47+
"Output.docx");
48+
}
49+
50+
/// <summary>
51+
/// Converts selected PDF pages to Word.
52+
/// </summary>
53+
public IActionResult PDFRangetoWord()
54+
{
55+
// Open the input PDF document.
56+
using FileStream stream = new FileStream("Data/Input-file.pdf", FileMode.Open, FileAccess.Read);
57+
58+
// Create a DataExtractor instance.
59+
DataExtractor extractor = new DataExtractor();
60+
61+
// Specify the page range to convert.
62+
extractor.PageRange = new int[,] { { 1, 2 } };
63+
64+
// Extract PDF content as a Word document.
65+
WordDocument document = extractor.ExtractDataAsWordDocument(stream);
66+
67+
// Create a memory stream for the output document.
68+
MemoryStream outputStream = new MemoryStream();
69+
70+
// Save the Word document in DOCX format.
71+
document.Save(outputStream, FormatType.Docx);
72+
73+
// Close the Word document.
74+
document.Close();
75+
76+
// Reset the stream position.
77+
outputStream.Position = 0;
78+
79+
// Return the generated DOCX file.
80+
return File(
81+
outputStream,
82+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
83+
"Output.docx");
84+
}
85+
86+
public IActionResult Privacy()
87+
{
88+
return View();
89+
}
90+
91+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
92+
public IActionResult Error()
93+
{
94+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
95+
}
96+
}
97+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>Convert_PDF_to_Word</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<None Remove="Data\Input-file.pdf" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<EmbeddedResource Include="Data\Input-file.pdf">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</EmbeddedResource>
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<PackageReference Include="Syncfusion.SmartDataExtractor.Net.Core" Version="35.1.41" />
22+
</ItemGroup>
23+
24+
</Project>
266 KB
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Convert_PDF_to_Word.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Convert_PDF_to_Word
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+
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("NxYtFisQPR08Cit/VkN+XU9FfVZLVGpUf1FrRmJJfV16cVxMZVVaRnZdRF1kSHlSfkFiXHxednNRTWNZ");
23+
24+
25+
app.UseHttpsRedirection();
26+
app.UseRouting();
27+
28+
app.UseAuthorization();
29+
30+
app.MapStaticAssets();
31+
app.MapControllerRoute(
32+
name: "default",
33+
pattern: "{controller=Home}/{action=Index}/{id?}")
34+
.WithStaticAssets();
35+
36+
app.Run();
37+
}
38+
}
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost:5043",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": true,
17+
"applicationUrl": "https://localhost:7184;http://localhost:5043",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div>
6+
<h2 style="margin-bottom: 20px">Working with Smart Data Extractor</h2>
7+
<div>
8+
<button style="width: 250px; margin-bottom: 20px; height: 40px; display:block; font-size:18px;"
9+
onclick="location.href='@Url.Action("ConvertPDFtoWord", "Home")'">
10+
PDF to Word
11+
</button>
12+
<button style="width: 250px; margin-bottom: 20px; height: 40px; display:block; font-size:18px;"
13+
onclick="location.href='@Url.Action("PDFRangetoWord", "Home")'">
14+
PDF Page Range to Word
15+
</button>
16+
</div>
17+
</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>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - Convert_PDF_to_Word</title>
7+
<script type="importmap"></script>
8+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
9+
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
10+
<link rel="stylesheet" href="~/Convert_PDF_to_Word.styles.css" asp-append-version="true" />
11+
</head>
12+
<body>
13+
<header>
14+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
15+
<div class="container-fluid">
16+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Convert_PDF_to_Word</a>
17+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
18+
aria-expanded="false" aria-label="Toggle navigation">
19+
<span class="navbar-toggler-icon"></span>
20+
</button>
21+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
22+
<ul class="navbar-nav flex-grow-1">
23+
<li class="nav-item">
24+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
25+
</li>
26+
<li class="nav-item">
27+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
28+
</li>
29+
</ul>
30+
</div>
31+
</div>
32+
</nav>
33+
</header>
34+
<div class="container">
35+
<main role="main" class="pb-3">
36+
@RenderBody()
37+
</main>
38+
</div>
39+
40+
@* <footer class="border-top footer text-muted">
41+
<div class="container">
42+
&copy; 2026 - Convert_PDF_to_Word - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
43+
</div>
44+
</footer> *@
45+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
46+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
47+
<script src="~/js/site.js" asp-append-version="true"></script>
48+
@await RenderSectionAsync("Scripts", required: false)
49+
</body>
50+
</html>

0 commit comments

Comments
 (0)