[HttpPost("Load")]
[Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]
[Route("[controller]/Load")]
//Post action for Loading the PDF documents
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
SettingsController settings = new SettingsController(_context);
Settings setting = settings.GetSettingsByName("TEMPLATE_DIR_PATH");
PdfRenderer pdfviewer = new PdfRenderer(_cache);
MemoryStream stream = new MemoryStream();
object jsonResult = new object();
Templates template = new TemplatesController(_context).GetTemplates(Int32.Parse(jsonObject["document"]));
string documentName = template.FilePath;
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
if (bool.Parse(jsonObject["isFileName"]))
{
string documentPath = GetDocumentPath(setting.SettingValue + documentName);
if (!string.IsNullOrEmpty(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(bytes);
Syncfusion.Drawing.SizeF pageSize = loadedDocument.Pages[0].Size;
PdfMargins margins = new PdfMargins();
margins.All = 0;
//Insert a new page in the beginning of the document
if (template.HasCoverPage.Value)
{
loadedDocument.Pages.Insert(0, pageSize , margins);
}
if (template.HasTrailingPage.Value)
{
loadedDocument.Pages.Add(pageSize , margins);
}
loadedDocument.Save(stream);
stream.Position = 0;
}
else
{
//return this.Content(jsonObject["document"] + " is not found");
string fileName = jsonObject["document"].Split("://")[0];
if (fileName == "http" || fileName == "https")
{
var WebClient = new WebClient();
byte[] pdfDoc = WebClient.DownloadData(documentName);
stream = new MemoryStream(pdfDoc);
}
else
{
return this.Content(documentName + " is not found");
}
}
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
|