Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created September 7, 2026 11:22
Show Gist options
  • Select an option

  • Save aspose-com-gists/13f8f7d6bbadcc30d39eacbd7ef1a410 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/13f8f7d6bbadcc30d39eacbd7ef1a410 to your computer and use it in GitHub Desktop.
Compress or Optimize PDF with Same Quality in C#

Compress or Optimize PDF with Same Quality in C#

Learn how to compress or optimize PDF while keeping quality using Aspose.PDF for .NET in C#. This guide covers prerequisites, a step‑by‑step implementation, a code example, configuration options, and performance tips to shrink PDFs without losing fidelity.

Read the full guide here: https://blog.aspose.com/pdf/compress-or-optimize-pdf-with-same-quality-in-csharp/

using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Optimization;
class Program
{
static void Main()
{
const string inputPdf = "input.pdf"; // PDF generated from XML
const string outputPdf = "output_compressed.pdf";
if (!File.Exists(inputPdf))
{
Console.Error.WriteLine($"File not found: {inputPdf}");
return;
}
// Load the PDF document
using (Document pdfDoc = new Document(inputPdf))
{
// Configure optimization: compress objects into streams
OptimizationOptions opt = new OptimizationOptions
{
CompressObjects = true
// other options remain at their defaults
};
// Apply the optimization to the document resources
pdfDoc.OptimizeResources(opt);
// Save the optimized PDF
pdfDoc.Save(outputPdf);
}
Console.WriteLine($"Compressed PDF saved to '{outputPdf}'.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment