Skip to content

Commit 53c037b

Browse files
Add files via upload
1 parent f92c28e commit 53c037b

File tree

8 files changed

+356
-0
lines changed

8 files changed

+356
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
using HarfBuzzSharp;
2+
using SkiaSharp;
3+
using Syncfusion.DocIO.DLS;
4+
using Syncfusion.DocIORenderer;
5+
using Syncfusion.Drawing;
6+
using Syncfusion.EJ2.PdfViewer;
7+
using Syncfusion.HtmlConverter;
8+
using Syncfusion.Pdf;
9+
using Syncfusion.Pdf.Graphics;
10+
using Syncfusion.Pdf.Parsing;
11+
using Syncfusion.Presentation;
12+
using Syncfusion.PresentationRenderer;
13+
using Syncfusion.OCRProcessor;
14+
using Syncfusion.XlsIO;
15+
using Syncfusion.XlsIORenderer;
16+
using System.IO;
17+
using System.Drawing;
18+
using Microsoft.AspNetCore.Html;
19+
using System.IO.Pipes;
20+
using Syncfusion.Pdf.Graphics.Images.Decoder;
21+
using Microsoft.AspNetCore.Components.Forms;
22+
using Microsoft.AspNetCore.Mvc;
23+
24+
namespace PDF_Conversion_API.Controllers
25+
{
26+
[ApiController]
27+
[Route("api/[controller]")]
28+
public class ProductController : ControllerBase
29+
{
30+
private readonly IWebHostEnvironment _webHostEnvironment;
31+
32+
public ProductController(IWebHostEnvironment webHostEnvironment)
33+
{
34+
_webHostEnvironment = webHostEnvironment;
35+
}
36+
public string filePath;
37+
//[HttpPost("FileUpload")]
38+
39+
private string FileUpload(IFormFile file)
40+
{
41+
42+
string fileName = file.FileName;
43+
string directoryPath = Path.Combine(_webHostEnvironment.ContentRootPath, "uploadedFiles");
44+
filePath = Path.Combine(directoryPath, fileName);
45+
using (var fileStream = new FileStream(filePath, FileMode.Create))
46+
{
47+
file.CopyTo(fileStream);
48+
}
49+
return filePath;
50+
}
51+
private IActionResult HtmlToPdf(IFormFile file)
52+
{
53+
string filePath = FileUpload(file);
54+
//Instantiation of BlinkConverterSettings for HTML to PDF conversion
55+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
56+
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
57+
htmlConverter.ConverterSettings = blinkConverterSettings;
58+
//Loads HTML document
59+
PdfDocument pdfDocument = htmlConverter.Convert(filePath);
60+
//Save the converted PDF document to MemoryStream.
61+
MemoryStream stream = new MemoryStream();
62+
pdfDocument.Save(stream);
63+
stream.Position = 0;
64+
//Download PDF document in the browser.
65+
return File(stream, "application/pdf", "OutputFile.pdf");
66+
}
67+
private IActionResult PptxToPdf(IFormFile file)
68+
{
69+
string filePath = FileUpload(file);
70+
//Loads Pptx document
71+
IPresentation presentation = Presentation.Open(filePath);
72+
//Convert the PowerPoint document to PDF document.
73+
PdfDocument pdfDocument = PresentationToPdfConverter.Convert(presentation);
74+
//Save the converted PDF document to MemoryStream.
75+
MemoryStream stream = new MemoryStream();
76+
pdfDocument.Save(stream);
77+
stream.Position = 0;
78+
//Download PDF document in the browser.
79+
return File(stream, "application/pdf", "OutputFile.pdf");
80+
}
81+
private IActionResult WordToPdf(IFormFile file)
82+
{
83+
string filePath = FileUpload(file);
84+
//Loads file stream into Word document
85+
FileStream docStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
86+
//Instantiation of DocIORenderer for Word to PDF conversion
87+
WordDocument wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic);
88+
DocIORenderer render = new DocIORenderer();
89+
render.Settings.ChartRenderingOptions.ImageFormat = Syncfusion.OfficeChart.ExportImageFormat.Jpeg;
90+
//Converts Word document into PDF document
91+
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
92+
render.Dispose();
93+
wordDocument.Dispose();
94+
//Saves the PDF document to MemoryStream.
95+
MemoryStream stream = new MemoryStream();
96+
pdfDocument.Save(stream);
97+
stream.Position = 0;
98+
//Download PDF document in the browser.
99+
return File(stream, "application/pdf", "OutputFile.pdf");
100+
}
101+
private IActionResult ExcelToPdf(IFormFile file)
102+
{
103+
string filePath = FileUpload(file);
104+
//Initialize ExcelEngine.
105+
ExcelEngine excelEngine = new ExcelEngine();
106+
IApplication application = excelEngine.Excel;
107+
application.DefaultVersion = ExcelVersion.Xlsx;
108+
//Loads file stream into Excel document
109+
FileStream excelStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
110+
IWorkbook workbook = application.Workbooks.Open(excelStream);
111+
//Initialize XlsIO renderer.
112+
XlsIORenderer renderer = new XlsIORenderer();
113+
//Convert Excel document into PDF document
114+
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
115+
//Saves the PDF document to MemoryStream.
116+
MemoryStream stream = new MemoryStream();
117+
pdfDocument.Save(stream);
118+
stream.Position = 0;
119+
//Download PDF document in the browser.
120+
return File(stream, "application/pdf", "OutputFile.pdf");
121+
}
122+
private IActionResult ImageToPdf(IFormFile file)
123+
{
124+
string filePath = FileUpload(file);
125+
//Creating the new PDF document
126+
PdfDocument document = new PdfDocument();
127+
//Loading the image
128+
PdfPage page = document.Pages.Add();
129+
PdfGraphics graphics = page.Graphics;
130+
//Loading the image
131+
FileStream imageStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
132+
PdfBitmap image = new PdfBitmap(imageStream);
133+
//Drawing image to the PDF page
134+
graphics.DrawImage(image, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
135+
//Saves the PDF document to MemoryStream.
136+
MemoryStream stream = new MemoryStream();
137+
document.Save(stream);
138+
document.Close(true);
139+
OCRProcessor processor = new OCRProcessor();
140+
//Load a PDF document stream.
141+
PdfLoadedDocument lDoc = new PdfLoadedDocument(stream);
142+
//Set OCR language to process.
143+
processor.Settings.Language = Languages.English;
144+
//Process OCR by providing the PDF document.
145+
processor.PerformOCR(lDoc);
146+
//Saves the PDF document to MemoryStream.
147+
lDoc.Save(stream);
148+
stream.Position = 0;
149+
//Download PDF document in the browser.
150+
return File(stream, "application/pdf", "OutputFile.pdf");
151+
}
152+
153+
154+
//Convert To PDF
155+
[HttpPost("ConvertToPdf")]
156+
public async Task<IActionResult> ConvertToPdf(IFormFile file)
157+
{
158+
string filePath = FileUpload(file);
159+
IActionResult pdfDocument= null;
160+
//MemoryStream stream = new MemoryStream();
161+
string fileName = file.FileName;
162+
string[] fileFormat = fileName.Split(".");
163+
string fileFormatType = fileFormat[1];
164+
165+
switch (fileFormatType)
166+
{
167+
case "docx":
168+
pdfDocument = WordToPdf(file);
169+
break;
170+
case "pptx":
171+
pdfDocument = PptxToPdf(file);
172+
break;
173+
case "jpg":
174+
case "jpeg":
175+
case "png":
176+
pdfDocument = ImageToPdf(file);
177+
break;
178+
case "xls":
179+
case "xlsx":
180+
pdfDocument = ExcelToPdf(file);
181+
break;
182+
case "html":
183+
pdfDocument = HtmlToPdf(file);
184+
break;
185+
}
186+
return pdfDocument;
187+
}
188+
189+
// Convert HTML to PDF
190+
[HttpPost("HtmlToPdf")]
191+
public IActionResult HtmlToPdfConverstion(IFormFile file)
192+
{
193+
return HtmlToPdf(file);
194+
}
195+
// Convert Pptx to PDF
196+
[HttpPost("PptxToPdf")]
197+
public IActionResult PptxToPdfConversation(IFormFile file)
198+
{
199+
return PptxToPdf(file);
200+
}
201+
// Convert Word to PDF
202+
[HttpPost("WordToPdf")]
203+
public IActionResult WordToPdfConverstion(IFormFile file)
204+
{
205+
return WordToPdf(file);
206+
}
207+
// Convert Excel to PDF
208+
[HttpPost("ExcelToPdf")]
209+
public IActionResult ExcelToPdfConverstion(IFormFile file)
210+
{
211+
return ExcelToPdf(file);
212+
}
213+
// Convert Image to PDF
214+
[HttpPost("ImageToPdf")]
215+
public IActionResult ImageToPdfConverstion(IFormFile file)
216+
{
217+
return ImageToPdf(file);
218+
}
219+
}
220+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="22.2.8" />
13+
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows" Version="22.2.8" />
14+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="22.2.8" />
15+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="22.2.8" />
16+
<PackageReference Include="Syncfusion.PDF.OCR.Net.Core" Version="22.2.8" />
17+
<PackageReference Include="Syncfusion.PresentationRenderer.Net.Core" Version="22.2.8" />
18+
<PackageReference Include="Syncfusion.XlsIORenderer.Net.Core" Version="22.2.8" />
19+
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>https</ActiveDebugProfile>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33829.357
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PDF_Conversion_API", "PDF_Conversion_API.csproj", "{D0D87898-227C-45FE-9E9F-71077109935A}"
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+
{D0D87898-227C-45FE-9E9F-71077109935A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D0D87898-227C-45FE-9E9F-71077109935A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D0D87898-227C-45FE-9E9F-71077109935A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D0D87898-227C-45FE-9E9F-71077109935A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {28F895AA-70E5-49DB-A15B-E9FFC349618A}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
5+
builder.Services.AddControllers();
6+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
7+
builder.Services.AddEndpointsApiExplorer();
8+
builder.Services.AddSwaggerGen();
9+
10+
var app = builder.Build();
11+
app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
12+
// Configure the HTTP request pipeline.
13+
if (app.Environment.IsDevelopment())
14+
{
15+
app.UseSwagger();
16+
app.UseSwaggerUI();
17+
}
18+
19+
app.UseHttpsRedirection();
20+
21+
app.UseAuthorization();
22+
23+
app.MapControllers();
24+
25+
app.Run();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:33068",
8+
"sslPort": 44343
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "https://localhost:7019/;http://localhost:5046",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"https": {
23+
"commandName": "Project",
24+
"dotnetRunMessages": true,
25+
"launchBrowser": true,
26+
"launchUrl": "swagger",
27+
"applicationUrl": "https://localhost:7019;http://localhost:5046",
28+
"environmentVariables": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
}
31+
},
32+
"IIS Express": {
33+
"commandName": "IISExpress",
34+
"launchBrowser": true,
35+
"launchUrl": "swagger",
36+
"environmentVariables": {
37+
"ASPNETCORE_ENVIRONMENT": "Development"
38+
}
39+
}
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)