How to compress a PDF document in ASP.NET Core
The Syncfusion Essential® PDF is a .NET Core PDF library used to create, read, and edit PDF documents. Using this library, you can compress a PDF document in the ASP.NET Core platform.
Steps to compress a PDF document in ASP.NET Core using C#
- Create a new ASP.NET Core MVC application.
- Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your .NET Core project from NuGet.org.
- Add a new button (Compress PDF) in the Index.cshtml as follows.
@{ Html.BeginForm("CompressPDF", "Home", FormMethod.Post); { <input type="submit" value="Compress PDF Document" class=" btn" /> }}
- Include the following namespaces in the HomeController.cs file.
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using System.IO;
- Add a new action method CompressPDF in HomeController.cs and include the following code sample to compress a PDF file and download it.
//To get content root path of the project private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public IActionResult CompressPDF() {string path = Path.Combine(_hostingEnvironment.ContentRootPath, "Data", "PDF_succinctly.pdf"); FileStream inputDocument = new FileStream(path, FileMode.Open); //Load an existing PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument); //Create a new compression option. PdfCompressionOptions options = new PdfCompressionOptions(); //Enable the compress image. options.CompressImages = true; //Set the image quality. options.ImageQuality = 50; //Assign the compression option to the document loadedDocument.Compress(options); //Save the PDF document. MemoryStream outputDocument = new MemoryStream(); //Save the PDF document loadedDocument.Save(outputDocument); outputDocument.Position = 0; //Close the document loadedDocument.Close(true); //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(outputDocument, "application/pdf"); fileStreamResult.FileDownloadName = "Compressed_PDF.pdf"; return fileStreamResult; }
- Build and run the application, the website will open in the browser, then, you can compress a PDF.
- By executing the program, you will get the PDF document as follows.
A complete work sample for compress a PDF document can be downloaded from PDFCompress.zip.
Take a moment to peruse the documentation. You will find other options like compressing images with image quality, optimizing embedded fonts, optimizing page contents, and removing metadata information with code examples.
Click here to explore a rich set of Syncfusion Essential® PDF features.
See Also:
Compress a PDF file in WinForms
Create a PDF file in ASP.NET MVC
Create a PDF file in Windows Forms
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.