|
| 1 | + |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using Microsoft.AspNetCore.Hosting; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using PDFCompression.Models; |
| 7 | +using Syncfusion.Pdf; |
| 8 | +using Syncfusion.Pdf.Parsing; |
| 9 | + |
| 10 | +namespace PDFCompression.Controllers |
| 11 | +{ |
| 12 | + public class HomeController : Controller |
| 13 | + { |
| 14 | + private readonly IWebHostEnvironment _hostingEnvironment; |
| 15 | + |
| 16 | + public HomeController(IWebHostEnvironment hostingEnvironment) |
| 17 | + { |
| 18 | + _hostingEnvironment = hostingEnvironment; |
| 19 | + } |
| 20 | + |
| 21 | + public IActionResult Index() |
| 22 | + { |
| 23 | + return View(); |
| 24 | + } |
| 25 | + |
| 26 | + public IActionResult Privacy() |
| 27 | + { |
| 28 | + return View(); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + public IActionResult CompressPDF() |
| 33 | + { |
| 34 | + string path = Path.Combine(_hostingEnvironment.ContentRootPath, "Data", "PDF_succinctly.pdf"); |
| 35 | + |
| 36 | + FileStream inputDocument = new FileStream(path, FileMode.Open); |
| 37 | + |
| 38 | + //Load an existing PDF document |
| 39 | + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument); |
| 40 | + |
| 41 | + //Create a new compression option. |
| 42 | + PdfCompressionOptions options = new PdfCompressionOptions(); |
| 43 | + |
| 44 | + //Enable the compress image. |
| 45 | + options.CompressImages = true; |
| 46 | + |
| 47 | + //Set the image quality. |
| 48 | + options.ImageQuality = 30; |
| 49 | + |
| 50 | + //Optimize the font in the PDF document |
| 51 | + options.OptimizeFont = true; |
| 52 | + |
| 53 | + //Optimize page contents |
| 54 | + options.OptimizePageContents = true; |
| 55 | + |
| 56 | + //Remove metadata from the PDF document |
| 57 | + options.RemoveMetadata = true; |
| 58 | + |
| 59 | + //Flatted form fields in the PDF document |
| 60 | + if (loadedDocument.Form != null) |
| 61 | + loadedDocument.Form.Flatten = true; |
| 62 | + |
| 63 | + //Flatten all the annotation in PDF document |
| 64 | + foreach (PdfPageBase page in loadedDocument.Pages) |
| 65 | + { |
| 66 | + if (page.Annotations != null) |
| 67 | + page.Annotations.Flatten = true; |
| 68 | + } |
| 69 | + |
| 70 | + //Assign the compression option and compress the PDF document |
| 71 | + loadedDocument.Compress(options); |
| 72 | + |
| 73 | + //Save the PDF document. |
| 74 | + MemoryStream outputDocument = new MemoryStream(); |
| 75 | + |
| 76 | + //Save the PDF document |
| 77 | + loadedDocument.Save(outputDocument); |
| 78 | + outputDocument.Position = 0; |
| 79 | + |
| 80 | + //Close the document |
| 81 | + loadedDocument.Close(true); |
| 82 | + |
| 83 | + |
| 84 | + //Download the PDF document in the browser. |
| 85 | + FileStreamResult fileStreamResult = new FileStreamResult(outputDocument, "application/pdf"); |
| 86 | + fileStreamResult.FileDownloadName = "Compressed_PDF_document.pdf"; |
| 87 | + |
| 88 | + return fileStreamResult; |
| 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 | +} |
0 commit comments