|
| 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 | +} |
0 commit comments