User Guide > How to Work with... > Formats with Additional Functionality > PDF > How to... > Compress a PDF Document |
ImageGear .NET improves the overall size of saved PDF documents by reducing or removing data structures in a PDF document.
File size improvements will vary with each PDF file. In some cases, file size reduction is dramatic. Re-saving an optimized PDF file is not expected to further reduce its size. Less frequent, PDF file size may increase due to metadata that is always written when PDF documents are saved.
WARNING: Saving compressed may invalidate PDF/A and PDF/X compliant documents. Refer to API reference topic ImGearPDFDocument.SaveCompressed() for additional details. |
To compress a PDF document using ImageGear .NET:
The following is a sample method that illustrates how to re-compress a PDF file to memory:
C# Example |
Copy Code |
---|---|
using System; using System.IO; using System.Text; using ImageGear.Core; using ImageGear.Formats; using ImageGear.Windows.Forms; using ImageGear.Formats.PDF; public Stream SaveCompressedPDFtoStream(String inputFileName) { // Load PDF document. ImGearPDFDocument pdfDocument = null; using (Stream fileStream = new FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { pdfDocument = (ImGearPDFDocument)ImGearFileFormats.LoadDocument(fileStream); } // Save compressed PDF document to stream. Stream stream = new MemoryStream(); pdfDocument.SaveCompressed(stream); return stream; } |
VB.NET Example |
Copy Code |
---|---|
Imports System Imports System.IO Imports System.Text Imports ImageGear.Core Imports ImageGear.Formats Imports ImageGear.Formats.PDF Public Function SaveCompressedPDFtoStream(inputFileName As String) As Stream ' Load PDF document. Dim pdfDocument As ImGearPDFDocument Using fileStream As New FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read) pdfDocument = DirectCast(ImGearFileFormats.LoadDocument(fileStream), ImGearPDFDocument) End Using ' Save compressed PDF document to stream. Dim stream As Stream = New MemoryStream() pdfDocument.SaveCompressed(stream) Return stream End Function |
Refer to the sample SaveCompressedPDF for a more complete example of re-saving PDF files (see PDF Samples).