Compress PDF

PDF compression programmatically using Aspose.PDF Library in various programming languages

How to compress PDF files with the Aspose.PDF library

Aspose.PDF supports compression of PDF. The way in which the majority of PDF file size can be reduced is by reducing the size of embedding images within the PDF Document. Additionaly we can linearize document in order to open the first page as quickly as possible:

Invoking this method doesn’t actually saves the document. On the contrary the document only is prepared to have optimized structure, call then Save to get optimized document.

Steps to reduce size PDF File


You need Aspose.PDF library 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.

Example for C#: Compress PDF Document

Reducing size of PDF document using C#

Document pdfDocument = new Document(dataDir + "input.pdf");
pdfDocument.OptimizeResources();
pdfDocument.Save(dataDir + "output.pdf");

How to Optimize PDF Document for the Web with Aspose.PDF for Java

Linearization is a process of making a PDF file suitable for online browsing using a web browser. A linearized PDF file is structured in a way that allows the first page of the PDF file to be displayed in a user web browser before the entire file is downloaded from the web server. This makes the web viewing faster and the user does not need to wait for the entire PDF to load to start viewing the document. Using method Optimize we can:

  • open the first page as quickly as possible;
  • display next page or follow by link to the next page as quickly as possible
  • display the page incrementally as it arrives when data for a page is delivered over a slow channel (display the most useful data first);
  • permit user interaction, such as following a link, to be performed even before the entire page has been received and displayed.

Steps to reduce size PDF File via Java

  1. Open a PDF document using Document object.
  2. Call the Optimize method 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.

Example for Java: Compress PDF Document for Web

Reducing size of PDF document using Java

Document pdfDocument = new Document(dataDir + "input.pdf");
// Optimize for web
pdfDocument.optimize();
pdfDocument.save(dataDir + "output.pdf");

How to Removing or Flattening Annotations with Aspose.PDF for C++

Adding annotations to your PDF document naturally increases its size. Annotations can be removed if they are not needed. They may be flattened if necessary, but do not require further modification. Both methods will decrease the file size. Using method Flatten we can:

  • open the document as quickly as possible;
  • display page as single enitity

Steps to reduce size PDF File via C++

  1. Create a document object named document that opens the input PDF file using the MakeObject function.
  2. Loop through each page object in the document object using a for-each loop and a range-based iterator named page.
  3. Loop through each annotation object in the page object using a for-each loop and a range-based iterator named annotation.
  4. Call the Flatten method on the annotation object to merge it with the page content and remove any interactivity.
  5. Call the Save method on the document object to save the updated PDF file.

Example for C++: Flatten PDF Document

Remove unnecessary annotations to reduce the size of PDF documents using C++

// String for path name
String _dataDir("C:\\Samples\\");

// String for input file name
String infilename("OptimizeDocument.pdf");
// String for output file name
String outfilename("OptimizeDocument_out.pdf");

// Open document
auto document = MakeObject<Document>(_dataDir + infilename);

// Flatten annotations
for(auto page : document->get_Pages())
{
    for(auto annotation : page->get_Annotations())
    {
    annotation->Flatten();
    }
}
// Save updated document
document->Save(_dataDir + outfilename);

About Aspose.PDF API

A PDF Processing Library to create cross-platform applications with the ability to generate, modify, convert, render, secure and print documents without using Adobe Acrobat. It supports converting various file formats into PDF including HTML and converting PDF documents into various output formats. Developers can easily render all HTML content in a single Page PDF as well as convert HTML files with SVG graphic tags to Tagged PDF files. .NET PDF API offers compression, table creation, graph & image functions, hyperlinks, stamp and watermarking tasks, extended security controls & custom font handling.