-
-
Save sliceofbytes/877e08b52c0e50301d9879a1cf40c95e to your computer and use it in GitHub Desktop.
Updated PdfViewerController for Asp.net core 5.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Caching.Memory; | |
using Newtonsoft.Json; | |
using Syncfusion.EJ2.PdfViewer; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
namespace CompanyName.ProjectName.Controllers | |
{ | |
[Route("[controller]")] | |
[ApiController] | |
public class PdfViewerController : ControllerBase | |
{ | |
private IHostingEnvironment _hostingEnvironment; | |
//Initialize the memory cache object | |
public IMemoryCache _cache; | |
public PdfViewerController(IHostingEnvironment hostingEnvironment, IMemoryCache cache) | |
{ | |
_hostingEnvironment = hostingEnvironment; | |
_cache = cache; | |
Console.WriteLine("PdfViewerController initialized"); | |
} | |
[HttpPost] | |
[HttpPost("Load")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/Load")] | |
//Post action for loading the PDF documents | |
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
Console.WriteLine("Load called"); | |
//Initialize the PDF viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
MemoryStream stream = new MemoryStream(); | |
object jsonResult = new object(); | |
if (jsonObject != null && jsonObject.ContainsKey("document")) | |
{ | |
if (bool.Parse(jsonObject["isFileName"])) | |
{ | |
string documentPath = GetDocumentPath(jsonObject["document"]); | |
if (!string.IsNullOrEmpty(documentPath)) | |
{ | |
byte[] bytes = System.IO.File.ReadAllBytes(documentPath); | |
stream = new MemoryStream(bytes); | |
} | |
else | |
{ | |
return this.Content(jsonObject["document"] + " is not found"); | |
} | |
} | |
else | |
{ | |
byte[] bytes = Convert.FromBase64String(jsonObject["document"]); | |
stream = new MemoryStream(bytes); | |
} | |
} | |
jsonResult = pdfviewer.Load(stream, jsonObject); | |
return Content(JsonConvert.SerializeObject(jsonResult)); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("Bookmarks")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/Bookmarks")] | |
//Post action for processing the bookmarks from the PDF documents | |
public IActionResult Bookmarks([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
var jsonResult = pdfviewer.GetBookmarks(jsonObject); | |
return Content(JsonConvert.SerializeObject(jsonResult)); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("RenderPdfPages")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/RenderPdfPages")] | |
//Post action for processing the PDF documents | |
public IActionResult RenderPdfPages([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
object jsonResult = pdfviewer.GetPage(jsonObject); | |
return Content(JsonConvert.SerializeObject(jsonResult)); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("RenderPdfTexts")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/RenderPdfTexts")] | |
//Post action for processing the PDF texts | |
public IActionResult RenderPdfTexts([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
object jsonResult = pdfviewer.GetDocumentText(jsonObject); | |
return Content(JsonConvert.SerializeObject(jsonResult)); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("RenderThumbnailImages")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/RenderThumbnailImages")] | |
//Post action for rendering the thumbnail images | |
public IActionResult RenderThumbnailImages([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
object result = pdfviewer.GetThumbnailImages(jsonObject); | |
return Content(JsonConvert.SerializeObject(result)); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("RenderAnnotationComments")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/RenderAnnotationComments")] | |
//Post action for rendering the annotations | |
public IActionResult RenderAnnotationComments([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
object jsonResult = pdfviewer.GetAnnotationComments(jsonObject); | |
return Content(JsonConvert.SerializeObject(jsonResult)); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("ExportAnnotations")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/ExportAnnotations")] | |
//Post action to export annotations | |
public IActionResult ExportAnnotations([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
string jsonResult = pdfviewer.ExportAnnotation(jsonObject); | |
return Content(jsonResult); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("ImportAnnotations")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/ImportAnnotations")] | |
//Post action to import annotations | |
public IActionResult ImportAnnotations([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
string jsonResult = string.Empty; | |
object JsonResult; | |
if (jsonObject != null && jsonObject.ContainsKey("fileName")) | |
{ | |
string documentPath = GetDocumentPath(jsonObject["fileName"]); | |
if (!string.IsNullOrEmpty(documentPath)) | |
{ | |
jsonResult = System.IO.File.ReadAllText(documentPath); | |
} | |
else | |
{ | |
return this.Content(jsonObject["document"] + " is not found"); | |
} | |
} | |
else | |
{ | |
string extension = Path.GetExtension(jsonObject["importedData"]); | |
if (extension != ".xfdf") | |
{ | |
JsonResult = pdfviewer.ImportAnnotation(jsonObject); | |
return Content(JsonConvert.SerializeObject(JsonResult)); | |
} | |
else | |
{ | |
string documentPath = GetDocumentPath(jsonObject["importedData"]); | |
if (!string.IsNullOrEmpty(documentPath)) | |
{ | |
byte[] bytes = System.IO.File.ReadAllBytes(documentPath); | |
jsonObject["importedData"] = Convert.ToBase64String(bytes); | |
JsonResult = pdfviewer.ImportAnnotation(jsonObject); | |
return Content(JsonConvert.SerializeObject(JsonResult)); | |
} | |
else | |
{ | |
return this.Content(jsonObject["document"] + " is not found"); | |
} | |
} | |
} | |
return Content(jsonResult); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("ExportFormFields")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/ExportFormFields")] | |
//Post action to export form fields | |
public IActionResult ExportFormFields([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
string jsonResult = pdfviewer.ExportFormFields(jsonObject); | |
return Content(jsonResult); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("ImportFormFields")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/ImportFormFields")] | |
//Post action to import form fields | |
public IActionResult ImportFormFields([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
jsonObject["data"] = GetDocumentPath(jsonObject["data"]); | |
object jsonResult = pdfviewer.ImportFormFields(jsonObject); | |
return Content(JsonConvert.SerializeObject(jsonResult)); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("Unload")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/Unload")] | |
//Post action for unloading and disposing the PDF document resources | |
public IActionResult Unload([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
pdfviewer.ClearCache(jsonObject); | |
return this.Content("Document cache is cleared"); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("Download")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/Download")] | |
//Post action for downloading the PDF documents | |
public IActionResult Download([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject); | |
return Content(documentBase); | |
} | |
[AcceptVerbs("Post")] | |
[HttpPost("PrintImages")] | |
[Microsoft.AspNetCore.Cors.EnableCors("AllowAnyPolicy")] | |
[Route("[controller]/PrintImages")] | |
//Post action for printing the PDF documents | |
public IActionResult PrintImages([FromBody] Dictionary<string, string> jsonObject) | |
{ | |
//Initialize the PDF Viewer object with memory cache object | |
PdfRenderer pdfviewer = new PdfRenderer(_cache); | |
object pageImage = pdfviewer.GetPrintImage(jsonObject); | |
return Content(JsonConvert.SerializeObject(pageImage)); | |
} | |
[NonAction] | |
//Returns the PDF document path | |
private string GetDocumentPath(string document) | |
{ | |
string documentPath = string.Empty; | |
if (!System.IO.File.Exists(document)) | |
{ | |
var path = _hostingEnvironment.ContentRootPath; | |
if (System.IO.File.Exists(path + "/Data/" + document)) | |
documentPath = path + "/Data/" + document; | |
} | |
else | |
{ | |
documentPath = document; | |
} | |
Console.WriteLine(documentPath); | |
return documentPath; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.HttpsPolicy; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.AspNetCore.ResponseCompression; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.OpenApi.Models; | |
using Newtonsoft.Json.Serialization; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace CompanyName.ProjectName | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
public readonly string AllowAllCORSPolicy = "AllowAnyPolicy"; | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddControllers(); | |
services.AddMemoryCache(); | |
services.AddControllers().AddNewtonsoftJson(options => | |
{ | |
// Use the default property (Pascal) casing | |
options.SerializerSettings.ContractResolver = new DefaultContractResolver(); | |
}); | |
services.AddCors(o => o.AddPolicy("AllowAnyPolicy", builder => | |
{ | |
builder.AllowAnyOrigin() | |
.AllowAnyMethod() | |
.AllowAnyHeader(); | |
})); | |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); | |
services.Configure<GzipCompressionProviderOptions>(options => options.Level = System.IO.Compression.CompressionLevel.Optimal); | |
services.AddResponseCompression(); | |
services.AddSwaggerGen(c => | |
{ | |
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WRA.PDFServices", Version = "v1" }); | |
}); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
app.UseSwagger(); | |
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WRA.PDFServices v1")); | |
} | |
else | |
{ | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseRouting(); | |
app.UseAuthorization(); | |
app.UseCors(AllowAllCORSPolicy); | |
app.UseResponseCompression(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapControllers().RequireCors(AllowAllCORSPolicy); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment