Easily Publish an ASP.NET Core App in Linux Docker that Compresses PDF Documents
Tags: csharp, webdev, linux, docker
Published: 2020-08-27 06:02:12

---
title: Easily Publish an ASP.NET Core App in Linux Docker that Compresses PDF Documents
published: true
date: 2020-07-23 10:00:55 UTC
tags: csharp, webdev, linux, docker
canonical_url: https://www.syncfusion.com/blogs/post/easily-publish-an-asp-net-core-app-in-linux-docker-that-compresses-pdf-documents.aspx
cover_image: https://www.syncfusion.com/blogs/wp-content/uploads/2020/07/Easily-Publish-an-ASP.NET-Core-App-in-Linux-Docker-that-Compresses-PDF-Documents-1-672x372.jpg
---

We are glad to announce that the [Syncfusion PDF Library](https://www.syncfusion.com/pdf-framework/net-core/pdf-library) has been extended to support compressing PDF documents in the ASP.NET Core platform from [version 18.2.0.44](https://www.syncfusion.com/forums/155798/essential-studio-2020-volume-2-release-v18-2-0-44-is-available-for-download) (Essential Studio 2020 Volume 2 release) onwards.

In this blog, I am going to create an ASP.NET Core web application to compress PDF documents and run the application in Linux Docker. The following topics will be covered:

- [Create an ASP.NET Core web application.](#create-an-asp.net-core-web-application)
- [Install the necessary NuGet packages to compress a PDF document.](#install-necessary-nuget-packages-to-compress-pdf-documents)
- [Compress PDF document](#compress-pdf-documents)
- [Run PDF compression in Linux Docker](#run-pdf-compression-in-linux-docker)
- [GitHub samples](#github-samples)

Let’s get started!

## Create an ASP.NET Core web application

Follow these steps to create an ASP.NET Core web application in Visual Studio 2019:

1. In Visual Studio 2019, go to  **File > New**  and then select  **Project**.
2. Select  **Create a new project**.
3. Select the  **ASP.NET Core Web Application**  template.
4. Enter the  **Project name**  and then click  **Create**. The **Project template** dialog will be displayed.
5. Select the **Enable Docker Support** check box and select **Linux** in the drop-down list and click **Create.**

Refer to the following screenshot.  
 ![](https://www.syncfusion.com/blogs/wp-content/uploads/2020/07/Create-a-new-ASP-.NET-Core-Web-Application-dialog-box-1.png)

**Note:** Install [Docker for Windows](https://www.docker.com/products/docker-desktop) to deploy applications in a Linux Docker container.

## Install necessary NuGet packages to compress PDF documents

After creating the ASP.NET Core web application, follow these steps to install the [Syncfusion.Pdf.Imaging.Net.Core](https://www.nuget.org/packages/Syncfusion.Pdf.Imaging.Net.Core) NuGet package in the project:

1. Right-click on the project and select **Manage NuGet Packages.**

2. Search for the [**Syncfusion.**** Pdf.Imaging.Net.Core**](https://www.nuget.org/packages/Syncfusion.Pdf.Imaging.Net.Core) package and then install it.

Refer to the following screenshot.  
 ![Search for the Syncfusion.Pdf.Imaging.Net.Core package and then install it.](https://www.syncfusion.com/blogs/wp-content/uploads/2020/07/Search-for-the-Syncfusion.Pdf.Imaging.Net_.Core-package-and-then-install-it-1.png)

## Compress PDF documents

After installing the necessary NuGet packages, follow these steps to compress PDF documents:

1. Place the input document in the **Data** folder, and in the properties of the PDF document, set **Copy to Output Directory** to **Copy if newer** for the data as shown in the following screenshot.  
 ![Set Copy to Output Directory to Copy if newer for the data in the Properties section](https://www.syncfusion.com/blogs/wp-content/uploads/2020/07/Set-Copy-to-Output-Directory-to-Copy-if-newer-for-the-data-in-the-Properties-section.png)
2. Add a new button in the **index.**** cshtml** file.
```csharp
@{ Html.BeginForm("CompressPDF", "Home", FormMethod.Post);
    {
        <input type="submit" value="Compress PDF Document" class=" btn" />
    }
}
```
3. Include the following namespaces in **HomeController.**** cs**.
```csharp
using Microsoft.AspNetCore.Mvc;
using Syncfusion.Pdf.Parsing;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using Syncfusion.Pdf;
```
4. Include the following code in the **HomeController.**** cs** file to compress the PDF document.
```csharp
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 = 30;
 
//Optimize the font in the PDF document
options.OptimizeFont = true;
 
//Optimize page contents
options.OptimizePageContents = true;
 
//Remove metadata from the PDF document
options.RemoveMetadata = true;
 
//Flatten form fields in the PDF document
if (loadedDocument.Form != null)
loadedDocument.Form.Flatten = true;
 
//Flatten all the annotations in the PDF document
foreach (PdfPageBase page in loadedDocument.Pages)
{
if (page.Annotations != null)
page.Annotations.Flatten = true;
}
 
//Assign the compression option and compress the PDF document
loadedDocument.Compress(options);
 
//Create a MemoryStream instance to save the 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_document.pdf";
 
return fileStreamResult;
}
```

**Note:** Compression optimizes the image quality and fonts, removes unwanted comments and white spaces, flattens annotations and form fields, and more.

You can also customize the compression operation with the help of the **PdfCompressOptions** class. To do so, refer to the following table of properties.

![PDF compression properties and description](https://dev-to-uploads.s3.amazonaws.com/i/xded8vf1owdlxyby5uhh.png)

By executing the code above, you will get a compressed PDF document.

## Run PDF compression in Linux Docker

After successfully executing the project, follow these steps to run the application in Linux Docker:

1. Select the target platform **Docker** from the toolbar as shown in the following screenshot.  
 ![](https://www.syncfusion.com/blogs/wp-content/uploads/2020/07/Select-the-target-platform-Docker-from-the-toolbar.png)
2. Click on the **Docker** button to run the application. Visual Studio will now build and run your container image and launch a web browser.  
 ![](https://www.syncfusion.com/blogs/wp-content/uploads/2020/07/Compress-PDF-Document-Web-Browser-Home-Page.png)

## GitHub samples

You can download an example of this PDF compression app in ASP.NET Core from this [GitHub repository](https://github.com/SyncfusionExamples/ASP.NET-Core-App-in-Linux-Docker-that-Compresses-PDF-Documents).

## Conclusion

In this blog post, we have learned an easy way to compress PDF documents in an ASP.NET Core web application that is deployed in Linux Docker. With this app, you can reduce the file size of PDF documents without degrading the document quality or appearance. This PDF compression feature is available in ASP.NET Core as of the [2020 Volume 2 release](https://www.syncfusion.com/forums/155798/essential-studio-2020-volume-2-release-v18-2-0-44-is-available-for-download).

Take a moment to peruse our [documentation](https://help.syncfusion.com/file-formats/pdf/working-with-compression), where you’ll find other options and features of the Syncfusion PDF Library, all with accompanying code examples.

If you have any questions about these features, please let us know in the comments section below. You can also contact us through our [support forum](https://www.syncfusion.com/forums)s, [Direct-Trac](https://www.syncfusion.com/support/directtrac/?_ga=2.203813140.507257297.1548648173-2146943073.1535695045), or [feedback portal](https://www.syncfusion.com/feedback/pdf). We are always happy to assist you!

If you like this article, we think you’ll also like the following articles about our PDF Library:

- [Create and Validate PDF Digital Signatures in C#](https://www.syncfusion.com/blogs/post/create-validate-pdf-digital-signatures-csharp.aspx)
- [How to Find Corrupted PDF Files in C# Easily](https://dev.to/syncfusion/how-to-find-corrupted-pdf-files-in-c-easily-16hi)
- [How to Convert an Image to PDF in ASP.NET Core](https://www.syncfusion.com/blogs/post/how-to-convert-image-to-pdf-in-asp-net-core.aspx)
- [Easy Ways to Redact PDFs Using C#](https://www.syncfusion.com/blogs/post/easy-ways-to-redact-pdfs-using-c.aspx)
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx).