Articles in this section
Category / Section

How to compress a PDF document in ASP.NET Core

6 mins read

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#

  1. Create a new ASP.NET Core MVC application.

Create a new ASP.NET Core MVC application.

  1. Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your .NET Core project from NuGet.org.

Install Nuget packages

  1. Add a new button (Compress PDF) in the Index.cshtml as follows.

Add a compress PDF button.

@{ Html.BeginForm("CompressPDF", "Home", FormMethod.Post);
{
                <input type="submit" value="Compress PDF Document" class=" btn" />
}}
 

 

  1. Include the following namespaces in the HomeController.cs file.
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Parsing;
    using System.IO;
    

 

  1. 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;
    }
    

 

  1. Build and run the application, the website will open in the browser, then, you can compress a PDF.

Local host screenshot

  1. By executing the program, you will get the PDF document as follows.

Output PDF screenshot

 

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 qualityoptimizing embedded fontsoptimizing 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 Xamarin

Create a PDF file in ASP.NET MVC

Create a PDF file in WPF

Create a PDF file in Windows Forms

 

Note:

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.

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied