This is a professional solution to compress PDF using C#, F#, VB.NET. Use our PDF optimization API to develop high-level, platform-independent software for .NET and .NET Core platforms. Try it online for free!
This C# library provides developers with the ability to quickly and efficiently compress PDF documents programmatically. Easily get optimized PDF output ready for printing, archiving or sharing over the network. It is a standalone PDF document processing solution for C# that does not require any third party software to be installed.
Our product provides C# developers with an integrated API to reduce the size of PDF files. PDF compression is performed in two passes. During the first pass, all pages of the PDF are converted to image format. Our product uses adaptive and highly efficient image compression algorithms that allow developers to get compact PDF output while maintaining the optimal quality of the document. On the next pass, the sequence of images is compressed and combined to form the content of a new, optimized PDF file.
PDF compression made easy with 'Aspose.Words for .NET'. The following example demonstrates how reduce the size of a PDF file in C#:
dotnet add package Aspose.Words
Copy
using Aspose.Words;
using Aspose.Words.Pdf2Word.FixedFormats;
using var pdfStream = File.OpenRead("Input.pdf");
var renderer = new PdfFixedRenderer();
var pdfReadOptions = new PdfFixedOptions
{
ImageFormat = FixedImageFormat.Jpeg,
JpegQuality = 50
};
const double maxPageDimension = 1584;
var pagesStream = renderer.SavePdfAsImages(pdfStream, pdfReadOptions);
var builder = new DocumentBuilder();
for (var i = 0; i < pagesStream.Count; ++i)
{
// Set maximum page size to avoid the current page image scaling.
var pageSetup = builder.PageSetup;
SetPageSize(pageSetup, maxPageDimension, maxPageDimension);
var pageImage = builder.InsertImage(pagesStream[i]);
SetPageSize(pageSetup, pageImage.Width, pageImage.Height);
pageSetup.TopMargin = 0;
pageSetup.LeftMargin = 0;
pageSetup.BottomMargin = 0;
pageSetup.RightMargin = 0;
if (i != pagesStream.Count - 1)
builder.InsertBreak(BreakType.SectionBreakNewPage);
}
var saveOptions = new PdfSaveOptions
{
CacheBackgroundGraphics = true
};
builder.Document.Save("Output.pdf", saveOptions);
private void SetPageSize(PageSetup pageSetup, double width, double height)
{
pageSetup.PageWidth = width;
pageSetup.PageHeight = height;
}
using Aspose.Words;
var doc = new Document("Input.pdf");
doc.Cleanup();
var nodes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in nodes)
{
if (shape.IsImage)
{
// It's up to developer to choose the library for image compression.
using var image = Image.FromStream(shape.ImageData.ToStream());
// ...
// Compress image and set it back to the shape.
shape.ImageData.SetImage("yourCompressedImage");
}
}
var saveOptions = new PdfSaveOptions
{
CacheBackgroundGraphics = true
};
doc.Save("Output.pdf", saveOptions);
using Aspose.Words;
var doc = new Document("Input.pdf");
doc.Cleanup();
var nodes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in nodes)
{
if (shape.IsImage)
{
// It's up to developer to choose the library for image compression.
using var image = Image.Fromstream(shape.ImageData.ToStream());
// ...
// Compress image and set it back to the shape.
shape.ImageData.SetImage("yourCompressedImage");
}
}
doc.Save("Output.pdf");
using Aspose.Words;
var doc = new Document("Input.pdf");
doc.Cleanup();
var nodes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in nodes)
{
if (shape.IsImage)
{
// It's up to developer to choose the library for image compression.
using var image = Image.FromStream(shape.ImageData.ToStream());
// ...
// Compress image and set it back to the shape.
shape.ImageData.SetImage("yourCompressedImage");
}
}
var saveOptions = new OoxmlSaveOptions
{
CompressionLevel = CompressionLevel.Maximum
};
doc.Save("Output.pdf", saveOptions);
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var shape = builder.InsertImage("Input.pdf");
var saveOptions = new ImageSaveOptions(SaveFormat.Pdf)
{
};
shape.GetShapeRenderer().Save("Output.pdf", saveOptions);
using Aspose.Words;
using Aspose.Words.Pdf2Word.FixedFormats;
using var pdfStream = File.OpenRead("Input.pdf");
var renderer = new PdfFixedRenderer();
var pdfReadOptions = new PdfFixedOptions
{
ImageFormat = FixedImageFormat.Jpeg,
JpegQuality = 50
};
const double maxPageDimension = 1584;
var pagesStream = renderer.SavePdfAsImages(pdfStream, pdfReadOptions);
var builder = new DocumentBuilder();
for (var i = 0; i < pagesStream.Count; ++i)
{
// Set maximum page size to avoid the current page image scaling.
var pageSetup = builder.PageSetup;
SetPageSize(pageSetup, maxPageDimension, maxPageDimension);
var pageImage = builder.InsertImage(pagesStream[i]);
SetPageSize(pageSetup, pageImage.Width, pageImage.Height);
pageSetup.TopMargin = 0;
pageSetup.LeftMargin = 0;
pageSetup.BottomMargin = 0;
pageSetup.RightMargin = 0;
if (i != pagesStream.Count - 1)
builder.InsertBreak(BreakType.SectionBreakNewPage);
}
var saveOptions = new PdfSaveOptions
{
CacheBackgroundGraphics = true
};
builder.Document.Save("Output.pdf", saveOptions);
private void SetPageSize(PageSetup pageSetup, double width, double height)
{
pageSetup.PageWidth = width;
pageSetup.PageHeight = height;
}
There are three alternative options to install "Aspose.Words for .NET" onto your system. Please choose one that resembles your needs and follow the step-by-step instructions:
Our product is fully cross-platform and supports all major .NET implementations:
As far as .NET code doesn't depend on the underlying hardware or operating system, but only on a Virtual Machine, you are free to develop any kind of software for Windows, macOS, Android, iOS and Linux. Just make sure you have installed the corresponding version of .NET Framework, .NET Core, Windows Azure, Mono or Xamarin.
We recommend using Microsoft Visual Studio, Xamarin, and MonoDevelop integrated development environments to create C#, F#, VB.NET applications.
Fore more details please refer to Product Documentation.
You can optimize files in many other file formats: