How to Start Convert PDF Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

PDF Compress VB.NET Library
How to compress, reduce PDF file size using vb.net in WinForms, WPF, ASP.NET, MVC application


VB.NET PDF Document Compression and Decompression Control SDK





In this vb.net tutorial, you will learn how to compress and reduce PDF file size using VB.NET code in Visual Basic Windows, web applications.

  • PDF embeded image compress
  • Remove unused objects from PDF document
  • Delete user data
  • Clean up the PDF document
  • Easy to integrate in Windows Forms, WPF applications, ASP.NET using VB.NET

How to compress PDF files using Visual Basic .NET

  1. Download XDoc.PDF Compression VB.NET library
  2. Install vb library to reduce PDF file size
  3. Step by Step Tutorial
















About PDF Compression


Class PDFOptimizer provides many settings to reduce the size of PDF files.

There are four main methods to compress PDF.



  • Image Compression: It is usually the most effective method to compress a PDF file. Allow you set options for color, grayscale, and monochrome image compression, and image downsampling.

  • Discard Objects: Allow you to remove all unused objects from PDF document.

  • Discard User Data: Allow you to delete all user related data from PDF.

  • Document Clean Up: It will remove useless items from the entire PDF document using VB.NET.






Compress Options


Just like Acrobat, XDoc.PDF SDK has included several standard methods to optimize a PDF file. These methods can be easily applied to the pdf file through property settings in VB.NET class "PDFOptimizer"







How to reduce a PDF file size through image compression using VB.NET


The PDF image compression is usually the most effective method to reduce PDF file size. You can apply and downgrade image quality for color, grayscale and monochrome images embeded in the PDF document using VB.NET



  • Downsample: Use options "DownsampleMode", "MaxResolutionLimit" and "TargetResolution" to reduces file size by lowering the resolution of images, which involves merging the colors of original pixels into larger pixels.
  • Compression: Reduces file size by eliminating unnecessary pixel data. In general, JPEG and JPEG 2000 compressions give better results on images like photographs with gradual transitions from color to color. ZIP is the better choice for illustrations with large areas of solid, flat color, or patterns made up of flat colors. For monochrome images, JBIG2 compression, which is available in PDF Optimizer but not in Distiller, and it is superior to CCITT.
  • Quality: Use option "JPEGImageQualityLevel" to apply JPEG and JPEG 2000 formats image quality level. JPEG and JPEG 2000 compression methods are typically lossy, a process that permanently removes some pixel data. You can apply lossy JPEG or JPEG 2000 compression to color images at various levels (Minimum, Low, Medium, High, Maximum). For JPEG 2000 compression, you can also specify lossless so that no pixel data is removed. Compression for monochrome images is lossless, except for JBIG2 compression, which provides both Lossy and Lossless modes of compression.


Our EdgePDF (asp.net pdf viewer and editor web control) has implemented a web PDF document compression solution based on this feature.



The following steps and VB.NET source code explain how to apply image compression settings to reduce PDF file size in Visual Basic Code.

  1. Create a new PDFOptimizeOptions object
  2. Apply settings for Monochrome, Grayscale and Color image
  3. Call PDFOptimizer.Optimize() method to compress the PDF document and save the compressed PDF file


Dim inputFilePath As String = "C:\3.pdf"
Dim outputFilePath As String = "C:\3_optimized.pdf"

' create optimizing options
Dim ops As PDFOptimizeOptions = New PDFOptimizeOptions()

' -- Options for Monochrome Image --
' to enable downsampling for those images with resolution higher than 300 dpi to 150 dpi
ops.MonochromeImageOptions.DownsampleMode = ImageDownsampleMode.Bicubic
ops.MonochromeImageOptions.MaxResolutionLimit = 300.0F
ops.MonochromeImageOptions.TargetResolution = 150.0F
' to change image compression mode to JBIG2
ops.MonochromeImageOptions.KeepCompressionMode = False
ops.MonochromeImageOptions.Compression = PDFCompression.JBIG2Decode

' -- Options for Grayscale Image --
' to enable downsampling for those images with resolution higher than 120 dpi to 96 dpi
ops.GrayscaleImageOptions.DownsampleMode = ImageDownsampleMode.Bilinear
ops.GrayscaleImageOptions.MaxResolutionLimit = 120.0F
ops.GrayscaleImageOptions.TargetResolution = 96.0F
' to change image compression mode to DCT
ops.GrayscaleImageOptions.KeepCompressionMode = False
ops.GrayscaleImageOptions.Compression = PDFCompression.DCTDecode
' set quality level, only available for compression mode DCT
ops.GrayscaleImageOptions.JPEGImageQualityLevel = JPEGImageQualityLevel.High

' -- Options for Color Image --
' to enable downsampling for those images with resolution higher than 120 dpi to 96 dpi
ops.ColorImageOptions.DownsampleMode = ImageDownsampleMode.Bicubic
ops.ColorImageOptions.MaxResolutionLimit = 120.0F
ops.ColorImageOptions.TargetResolution = 96.0F
' to change image compression mode to DCT
ops.ColorImageOptions.KeepCompressionMode = False
ops.ColorImageOptions.Compression = PDFCompression.DCTDecode
' set quality level, only available for compression mode DCT
ops.ColorImageOptions.JPEGImageQualityLevel = JPEGImageQualityLevel.Highest

' apply optimizing
PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops)






How to compress PDF through removing unused objects from PDF using VB.NET


The following steps and VB.NET source code show how to remove unused objects from PDF document in Visual Basic .NET application.

  1. Create a new PDFOptimizeOptions object
  2. Apply settings
  3. Call PDFOptimizer.Optimize() method to compress the PDF document and save the compressed PDF file


Dim inputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\optimized.pdf"

' Initial an optimize options object with default settings.
Dim ops As PDFOptimizeOptions = New PDFOptimizeOptions()
' Discard all form submission, import And reset actions.
ops.DiscardOptions.DiscardFormActions = True
' Flatten form fields.
ops.DiscardOptions.FlattenFormFields = True
' Discard all JavaScript actions.
ops.DiscardOptions.DiscardJavaScriptActions = True
' Discard all alternate images.
ops.DiscardOptions.DiscardAlternateImages = True
' Discard embedded page thumbnails.
ops.DiscardOptions.DiscardPageThumbnails = True
' Discard embedded print settings.
ops.DiscardOptions.DiscardEmbeddedPrintSettings = True
' Discard embedded search index.
ops.DiscardOptions.DiscardEmbeddedSearchIndex = True
' Discard bookmarks.
ops.DiscardOptions.DiscardBookmarks = True
' Discard all unused embedded resources in page.
ops.DiscardOptions.DiscardUnusedResourcesInPage = True

Try
    PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops)
Catch pdfEx As PDFException
    Console.WriteLine("[Warning]: " + pdfEx.Message)
Catch ex As Exception
    Console.WriteLine("[Error]: unexcepted exception - " + ex.Message)
End Try






How to compress PDF through removing user data from PDF using VB.NET


The following VB.NET source code show how to remove user data from PDF document in Visual Basic .NET application.



Dim inputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\optimized.pdf"

' Initial an optimize options object with default settings.
Dim ops As PDFOptimizeOptions = New PDFOptimizeOptions()
' Discard all comments.
ops.DiscardOptions.DiscardComments = True
' Discard all form fields.
ops.DiscardOptions.DiscardFormFields = True
' Discard all multimedia.
ops.DiscardOptions.DiscardMultimedia = True
' Discard document information.
ops.DiscardOptions.DiscardDocumentInfo = True
' Discard all metadata streams.
ops.DiscardOptions.DiscardMetadata = True
' Discard file attachments.
ops.DiscardOptions.DiscardFileAttachments = True
' Discard external cross references.
ops.DiscardOptions.DiscardExternalCrossRefs = True
' Discard private data of other applications.
ops.DiscardOptions.DiscardPrivateData = True
' Discard hidden layer content And flatten visible layers.
ops.DiscardOptions.FlattenPageLayers = True
' Discard all link annotations.
ops.DiscardOptions.DiscardAllLinks = True

Try
    PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops)
Catch pdfEx As PDFException
    Console.WriteLine("[Warning]: " + pdfEx.Message)
Catch ex As Exception
    Console.WriteLine("[Error]: unexcepted exception - " + ex.Message)
End Try






How to compress PDF through deleting useless items from PDF using VB.NET


The following VB.NET source code show how to clean up the PDF document and remove all useless items from PDF in vb.net class.



Dim inputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\optimized.pdf"

' Initial an optimize options object with default settings.
Dim ops As PDFOptimizeOptions = New PDFOptimizeOptions()
' Disable flag RemoveFlateCompression.
ops.DiscardOptions.RemoveFlateCompression = False
' Use Flate To Encode Streams That Are Not Encoded
' This property is ignored if RemoveFlateCompression is set.
ops.DiscardOptions.UseFlateToEncodePlainStreams = True
' In Streams That Use LZW Encoding, Use Flate Instead
' This property is ignored if RemoveFlateCompression is set.
ops.DiscardOptions.UseFlateToReplaceLZW = True
' Discard Invalid Bookmarks
ops.DiscardOptions.DiscardInvalidBookmarks = True
' Discard Invalid Links
ops.DiscardOptions.DiscardInvalidLinks = True
' Discard Unreferenced Named Destinations
ops.DiscardOptions.DiscardUnReferencedNamedDest = True
' Optimize Page Content
ops.DiscardOptions.OptimizePageContent = True


Try
    PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops)
Catch pdfEx As PDFException
    Console.WriteLine("[Warning]: " + pdfEx.Message)
Catch ex As Exception
    Console.WriteLine("[Error]: unexcepted exception - " + ex.Message)
End Try