Compress PDF via C#

PDF compression programmatically using Aspose.PDF for .NET Library

Optimize PDF with C#

Do you need to compress PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With .NET libraries like Aspose.PDF – are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional C# developers.

This C# library enables developers to compress PDF documents in a fast and efficient manner through programming. With this library, you can easily produce optimized PDF output for printing, archiving, or network sharing. It is a standalone solution for processing PDF documents in C# that does not require the installation of any third-party software.

In order to compress PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for .NET platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

How to Compress PDF via C#


You need Aspose.PDF for .NET to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Call the method for optimization on the document object, which removes unnecessary objects and compresses images in the PDF file
  3. Save the optimized document to the new path using the Save() method.

If you want to make it easier and faster to share or store a PDF file, compressing it can help. With this powerful Aspose.PDF .NET library, you can reduce the size of your PDF while keeping its quality and important details intact. This professional tool uses C# to compress PDF effectively. The following example demonstrates how to modify a PDF document in C#:

Compress PDF Files - C#

This sample code shows how to Optimize PDF Document for the Web - C#

Input file:

File not added

Output format:

PDF

Output file:


	string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

	// Open document
	Document pdfDocument = new Document(dataDir + "OptimizeDocument.pdf");

	// Optimize for web
	pdfDocument.Optimize();

	dataDir = dataDir + "OptimizeDocument_out.pdf";

	// Save output document
	pdfDocument.Save(dataDir);

About Aspose.PDF for C# API

Aspose.PDF for C# API provides a wide range of features for working with PDF files. Some of the features include:

  • Create PDF documents from scratch or from HTML, XML, or images.
  • Compress existing PDF documents by adding or removing pages, text, images, and other content.
  • Convert PDF documents to other formats such as HTML, XML, and images.
  • Render PDF documents to images or XPS format.
  • Print PDF documents directly from your application.
  • Digitally sign PDF documents.

You can find more information on Aspose.PDF for C# API in this Aspose documentation