ImageGear v26.5 - Updated
ImageGear.Formats.Pdf Assembly / ImageGear.Formats.PDF Namespace / ImGearPDFDocument Class / SaveCompressed Method / SaveCompressed(Stream) Method
Stream object to save to. Only FileStream and MemoryStream are currently supported.
Example




In This Topic
    SaveCompressed(Stream) Method
    In This Topic
    Save highly-compressed PDF document to a System.IO.Stream object.
    Syntax
    'Declaration
     
    Public Overloads Sub SaveCompressed( _
       ByVal stream As Stream _
    ) 
    'Usage
     
    Dim instance As ImGearPDFDocument
    Dim stream As Stream
     
    instance.SaveCompressed(stream)
    public void SaveCompressed( 
       Stream stream
    )
    public: void SaveCompressed( 
       Stream* stream
    ) 
    public:
    void SaveCompressed( 
       Stream^ stream
    ) 

    Parameters

    stream
    Stream object to save to. Only FileStream and MemoryStream are currently supported.
    Exceptions
    ExceptionDescription
    Thrown when stream is not writable.
    Remarks

    ImageGear improves the overall size of saved PDF documents by reducing or removing data structures in the PDF document, specifically:

    • Re-compress embedded bitonal Images to use lossless JBIG2 compression, which offers superior compression to either Flate or CCITTFax compression.
    • Re-compress embedded gray and color Images to use lossy sequential JPEG compression.
    • Re-compress LZW compressed Streams to use Flate compression.
    • Remove ASCII85 compression from Streams.
    • Compress uncompressed Streams to use Flate compression.
    • Optimize content Streams to share common sub-sequences.
    • Downsample high-resolution images, which may result reduce image quality when displayed zoomed-in.
    • Remove Thumbnail Images of PDF pages.
    • Remove embedded Standard Type 1 Fonts.
    • Merge identical Font descriptors and encodings.
    • Merge identical Forms and Images.
    • Remove symbols specific to deleted images from JBIG2 dictionaries.
    • Remove any unused Objects.

    WARNING: Re-compressing Images with JBIG2 will invalidate PDF/X compliant documents.

    WARNING: Removing embedded Standard Type 1 Fonts will invalidate PDF/X compliant documents.

    WARNING: Removing embedded Standard Type 1 Fonts will invalidate PDF/A compliant documents when visible text is affected.

    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 frequently, PDF file size may increase due to metadata that is always written when PDF documents are saved. Unmodified PDF files that cannot be further reduced are returned unaltered.

    Example
    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;
    }
    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
    See Also