PDF Compression
IronPDF offers the capability to compress PDFs effectively. One of the key methods involves reducing the size of images embedded within the PDF document. This optimization process can be initiated by using the compressSize
method on the PDF object.
When it comes to resizing JPEG images, the quality setting plays a crucial role. A quality setting of 100% results in minimal loss, while 1% represents very low-quality output. Typically, image quality levels of 90% and above are considered high-quality, 80%-90% falls into the medium-quality range, and 70%-80% is considered low-quality. Reducing the quality below 70% can lead to lower-quality images, but it can significantly reduce the overall file size of the PDF document.
It's recommended to experiment with different quality settings to find the right balance between image quality and file size that suits your specific requirements. The extent of quality reduction will ultimately depend on the type of input image, and some images may experience more noticeable reductions in clarity than others.
Here is an example of how you might use IronPDF to compress a PDF file by adjusting image quality settings:
// This C# code snippet demonstrates how to compress PDF images using IronPDF.
// Import the necessary IronPDF namespace.
using IronPdf;
public class PdfOptimization
{
public static void CompressPdf(string inputPdfPath, string outputPdfPath, int imageQualityPercentage)
{
// Load the PDF document to be compressed
var pdfDocument = PdfDocument.FromFile(inputPdfPath);
// Set the image compression quality.
// The quality percentage should be between 1 and 100, where 100 is the highest quality.
pdfDocument.CompressImages(imageQualityPercentage);
// Save the optimized PDF document to the specified output path.
pdfDocument.SaveAs(outputPdfPath);
}
public static void Main(string[] args)
{
// Define the input and output paths for the PDF documents.
string inputPath = "path/to/input.pdf";
string outputPath = "path/to/output.pdf";
// Define the desired image quality percentage for compression.
int imageQuality = 85;
// Call the function to compress the PDF.
CompressPdf(inputPath, outputPath, imageQuality);
}
}
// This C# code snippet demonstrates how to compress PDF images using IronPDF.
// Import the necessary IronPDF namespace.
using IronPdf;
public class PdfOptimization
{
public static void CompressPdf(string inputPdfPath, string outputPdfPath, int imageQualityPercentage)
{
// Load the PDF document to be compressed
var pdfDocument = PdfDocument.FromFile(inputPdfPath);
// Set the image compression quality.
// The quality percentage should be between 1 and 100, where 100 is the highest quality.
pdfDocument.CompressImages(imageQualityPercentage);
// Save the optimized PDF document to the specified output path.
pdfDocument.SaveAs(outputPdfPath);
}
public static void Main(string[] args)
{
// Define the input and output paths for the PDF documents.
string inputPath = "path/to/input.pdf";
string outputPath = "path/to/output.pdf";
// Define the desired image quality percentage for compression.
int imageQuality = 85;
// Call the function to compress the PDF.
CompressPdf(inputPath, outputPath, imageQuality);
}
}
' This C# code snippet demonstrates how to compress PDF images using IronPDF.
' Import the necessary IronPDF namespace.
Imports IronPdf
Public Class PdfOptimization
Public Shared Sub CompressPdf(ByVal inputPdfPath As String, ByVal outputPdfPath As String, ByVal imageQualityPercentage As Integer)
' Load the PDF document to be compressed
Dim pdfDocument = PdfDocument.FromFile(inputPdfPath)
' Set the image compression quality.
' The quality percentage should be between 1 and 100, where 100 is the highest quality.
pdfDocument.CompressImages(imageQualityPercentage)
' Save the optimized PDF document to the specified output path.
pdfDocument.SaveAs(outputPdfPath)
End Sub
Public Shared Sub Main(ByVal args() As String)
' Define the input and output paths for the PDF documents.
Dim inputPath As String = "path/to/input.pdf"
Dim outputPath As String = "path/to/output.pdf"
' Define the desired image quality percentage for compression.
Dim imageQuality As Integer = 85
' Call the function to compress the PDF.
CompressPdf(inputPath, outputPath, imageQuality)
End Sub
End Class
In this code:
- We first import the necessary IronPDF namespace.
- Then, we define a
CompressPdf
function that receives the path to the input PDF file, the path for the output PDF, and the image quality percentage. - The
PdfDocument.FromFile
method loads the PDF document from the specified file path. - The
CompressImages
method is used to adjust the image quality within the PDF. - Finally, we save the compressed PDF to the specified output path using
SaveAs
.
By adjusting the imageQuality
variable, you can control the quality of the images in the PDF, thus impacting the compression level and final file size.