Compress PDF document using C#, VB.NET
July 12, 2024
/// <summary>
/// Packs the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void PackDocument(string inPdfFilename, string outPdfFilename)
{
// create compressor with empty compression settings
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();
// specify that compressor must use maximum Flate compression level (best compression)
compressor.FlateCompressionLevel = 9;
// specify that compressor must recompress all resource that uses None, LZW, Flate compression using Flate compression
compressor.RecompressFlateCompression = true;
compressor.UseFlateInsteadLzwCompression = true;
compressor.UseFlateInsteadNoneCompression = true;
// specify that compressor must remove incremental update info and unused objects
compressor.PackDocument = true;
// if version of PDF document is lower than 1.7
if (GetPdfDocumentVersion(inPdfFilename) < 17)
{
// set output format to PDF 1.7
compressor.DocumentPackFormat = Vintasoft.Imaging.Pdf.PdfFormat.Pdf_17;
}
// compress PDF document
compressor.Compress(inPdfFilename, outPdfFilename);
}
/// <summary>
/// Returns the PDF document version.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <returns>The version number in dual-digit format (10,11,12,13,14,15,16,17,20,...).</returns>
private static int GetPdfDocumentVersion(string pdfFilename)
{
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
return document.Format.VersionNumber;
}
/// <summary>
/// Subsets fonts in PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void SubsetFonts(string inPdfFilename, string outPdfFilename)
{
// create compressor with empty compression settings
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();
// specify that compressor must subset fonts in PDF document
compressor.SubsetFonts = true;
// compress PDF document
compressor.Compress(inPdfFilename, outPdfFilename);
}
/// <summary>
/// Detects "read color depth" of PDF image resources and compress PDF document with intent to view in 150DPI.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void CompressToViewIn150DPI(string inPdfFilename, string outPdfFilename)
{
// create compressor that will compress PDF document using lossy compression algorithms
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateLossyCompressor(150, false, false, false);
// specify that compressor must use JPEG compression for color images
compressor.ColorImagesCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg;
// specify that compressor must set JPEG quality to 70
compressor.ColorImagesCompressionSettings.JpegQuality = 70;
// specify that compressor must detect if image is bitonal image and use optimal compression for bitonal image
compressor.DetectBitonalImageResources = true;
// specify that compressor must detect if image is black-white image and use optimal compression for black-white image
compressor.DetectBlackWhiteImageResources = true;
// specify that compressor must detect if image is grayscale image and use optimal compression for grayscale image
compressor.DetectGrayscaleImageResources = true;
// compress PDF document
compressor.Compress(inPdfFilename, outPdfFilename);
}
/// <summary>
/// Removes unused and duplicated resources in the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void RemoveUnsusedResources(string inPdfFilename, string outPdfFilename)
{
// create compressor with empty compression settings
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();
// specify that compressor must remove duplicate resources from PDF document
compressor.RemoveDuplicateResources = true;
// specify that compressor must remove unused names resources from PDF document
compressor.RemoveUnusedNamedResources = true;
// specify that compressor must remove unused names from PDF document
compressor.RemoveUnusedNames = true;
// specify that compressor must remove unused pages from PDF document
compressor.RemoveUnusedPages = true;
// specify that compressor must remove invalid bookmarks from PDF document
compressor.RemoveInvalidBookmarks = true;
// specify that compressor must remove invalid links from PDF document
compressor.RemoveInvalidLinks = true;
// compress PDF document
compressor.Compress(inPdfFilename, outPdfFilename);
}
/// <summary>
/// Removes metadata, bookmarks, document information, embedded files, embedded thumbnails, interactive form and structure tree of the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void RemoveObjects(string inPdfFilename, string outPdfFilename)
{
// create compressor with empty compression settings
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();
// specify that compressor must remove metadata from PDF document
compressor.RemoveMetadata = true;
// specify that compressor must remove bookmarks from PDF document
compressor.RemoveBookmarks = true;
// specify that compressor must remove document information from PDF document
compressor.RemoveDocumentInformation = true;
// specify that compressor must remove embedded files from PDF document
compressor.RemoveEmbeddedFiles = true;
// specify that compressor must remove embedded thumbnails from PDF document
compressor.RemoveEmbeddedThumbnails = true;
// specify that compressor must remove interactive form from PDF document
compressor.RemoveInteractiveForm = true;
// specify that compressor must remove structure tree from PDF document
compressor.RemoveStructureTree = true;
// compress PDF document
compressor.Compress(inPdfFilename, outPdfFilename);
}
/// <summary>
/// Flatten an annotations and remove intractive form of the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void FlattenAnnotations(string inPdfFilename, string outPdfFilename)
{
// create compressor with empty compression settings
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();
// specify that compressor must remove interactive form from PDF document
compressor.RemoveInteractiveForm = true;
// specify that compressor must flatten annotations in PDF document
compressor.FlattenAnnotations = true;
// compress PDF document
compressor.Compress(inPdfFilename, outPdfFilename);
}