Leadtools.Pdf Requires PDF Plug-in license | Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.10.31
PdfCompressor Class
See Also  Members   Example 
Leadtools.Pdf Namespace : PdfCompressor Class




Provides the functionality to create a PDF document with or without Mixed Raster Content (MRC) segmentation options.

Syntax

Visual Basic (Declaration) 
Public Class PdfCompressor 
Visual Basic (Usage)Copy Code
Dim instance As PdfCompressor
C# 
public class PdfCompressor 
Managed Extensions for C++ 
public __gc class PdfCompressor 
C++/CLI 
public ref class PdfCompressor 

Example

This example inserts an image to pdf document without using the MRC.


This example inserts an image to pdf document and uses the MRC to make a proper segment for this image.


This example inserts an image to pdf document with using user Segments.

Visual BasicCopy Code
Public Sub pdfCompressor_SegmentImage_PdfDocument(ByVal sender As Object, ByVal e As PdfCompressorSegmentImageEventArgs)
    'This will prevent addition to any segment with type background in all pages except the first page only
    If e.Page <> 0 And e.Segment.Type = SegmentTypeFlags.Background Then
       e.Cancel = True
    Else
       e.Cancel = False
    End If
End Sub
<Test> _
Public Sub PdfCompressorExample_PdfDocument()
  ' Load an image
  RasterCodecs.Startup()
  Dim codecs As RasterCodecs = New RasterCodecs()
  codecs.ThrowExceptionsOnInvalidImages = True

  Dim image As RasterImage = codecs.Load("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\MRCSegmentation.mrc")

  Dim pdfCompressor As PdfCompressor = New PdfCompressor()
  Dim compressionTypes As PdfCompressorCompressionTypes = New PdfCompressorCompressionTypes()

  'Sets compression types needed for each segment
  compressionTypes.Comp1Bit = PdfCompressor1BitCompression.Zip1Bit
  compressionTypes.Comp2Bit = PdfCompressor2BitCompression.Lzw2Bit
  compressionTypes.CompPicture = PdfCompressorPictureCompression.JpgPic
  compressionTypes.QFactor = 2
  'Flags for used compression types should be set
  compressionTypes.Flags = EnabledCompressionsFlags.EnableOneBit Or EnabledCompressionsFlags.EnableTwoBit Or EnabledCompressionsFlags.EnablePicture

  pdfCompressor.SetCompression(compressionTypes)

  pdfCompressor.Insert(image)
    AddHandler pdfCompressor.SegmentImage, AddressOf pdfCompressor_SegmentImage_PdfDocument
  pdfCompressor.Write("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image.pdf")
End Sub
C#Copy Code
public void pdfCompressor_SegmentImage_PdfDocument(object sender, PdfCompressorSegmentImageEventArgs e) 

   //This will prevent addition to any segment with type background in all pages except the first page only 
   if(e.Page != 0 && e.Segment.Type == SegmentTypeFlags.Background) 
      e.Cancel = true; 
   else 
      e.Cancel = false; 

 
 
public void PdfCompressorExample_PdfDocument() 

   // Load an image 
   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.ThrowExceptionsOnInvalidImages = true; 
 
   RasterImage image = codecs.Load(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\MRCSegmentation.mrc"); 
 
   PdfCompressor pdfCompressor = new PdfCompressor(); 
   PdfCompressorCompressionTypes compressionTypes = new PdfCompressorCompressionTypes(); 
 
   //Sets compression types needed for each segment 
   compressionTypes.Comp1Bit = PdfCompressor1BitCompression.Zip1Bit; 
   compressionTypes.Comp2Bit = PdfCompressor2BitCompression.Lzw2Bit; 
   compressionTypes.CompPicture = PdfCompressorPictureCompression.JpgPic; 
   compressionTypes.QFactor = 2; 
   //Flags for used compression types should be set 
   compressionTypes.Flags = EnabledCompressionsFlags.EnableOneBit | 
      EnabledCompressionsFlags.EnableTwoBit | 
      EnabledCompressionsFlags.EnablePicture; 
 
   pdfCompressor.SetCompression(compressionTypes); 
 
   pdfCompressor.Insert(image); 
   pdfCompressor.SegmentImage += new EventHandler<PdfCompressorSegmentImageEventArgs>(pdfCompressor_SegmentImage_PdfDocument); 
   pdfCompressor.Write(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image.pdf"); 
}
Visual BasicCopy Code
Public Sub pdfCompressor_SegmentImage_MrcDocument(ByVal sender As Object, ByVal e As PdfCompressorSegmentImageEventArgs)
  'This will prevent addition to any segment with type background
  If e.Segment.Type = SegmentTypeFlags.Background Then
    e.Cancel = True
  Else
    e.Cancel = False
  End If
End Sub
<Test> _
Public Sub PdfCompressorExample_MrcDocument()
  ' Load an image
  RasterCodecs.Startup()
  Dim codecs As RasterCodecs = New RasterCodecs()
  codecs.ThrowExceptionsOnInvalidImages = True

  Dim image As RasterImage = codecs.Load("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\MRCSegmentation.mrc")

  Dim pdfCompressor As PdfCompressor = New PdfCompressor()
  Dim compressionTypes As PdfCompressorCompressionTypes = New PdfCompressorCompressionTypes()
  Dim pdfCompressorOptions As PdfCompressorOptions = New PdfCompressorOptions()

  'Sets compression types needed for each segment
  compressionTypes.Comp1Bit = PdfCompressor1BitCompression.Zip1Bit
  compressionTypes.Comp2Bit = PdfCompressor2BitCompression.Lzw2Bit
  compressionTypes.CompPicture = PdfCompressorPictureCompression.JpgPic
  compressionTypes.QFactor = 2

  'Flags for used compression types should be set
  compressionTypes.Flags = EnabledCompressionsFlags.EnableOneBit Or EnabledCompressionsFlags.EnableTwoBit Or EnabledCompressionsFlags.EnablePicture

  pdfCompressor.SetCompression(compressionTypes)

  'Specify suitable profiles for input and output image
  pdfCompressorOptions.ImageQuality = PdfCompressorImageQuality.ComputerGenerated
  pdfCompressorOptions.OutputQuality = PdfCompressorOutputQuality.Excellent
  pdfCompressorOptions.Flags = SegmentationOptionsFlags.FavorOneBit Or SegmentationOptionsFlags.WithBackground

    AddHandler pdfCompressor.SegmentImage, AddressOf pdfCompressor_SegmentImage_MrcDocument
  pdfCompressor.Insert(image, pdfCompressorOptions)

  pdfCompressor.Write("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image.pdf")
End Sub
C#Copy Code
public void pdfCompressor_SegmentImage_MrcDocument(object sender, PdfCompressorSegmentImageEventArgs e) 

   //This will prevent addition to any segment with type background  
   if(e.Segment.Type == SegmentTypeFlags.Background) 
      e.Cancel = true;  
   else 
      e.Cancel = false;  

 
public void PdfCompressorExample_MrcDocument() 

   // Load an image 
   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.ThrowExceptionsOnInvalidImages = true; 
 
   RasterImage image = codecs.Load(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\MRCSegmentation.mrc"); 
 
   PdfCompressor pdfCompressor = new PdfCompressor(); 
   PdfCompressorCompressionTypes compressionTypes = new PdfCompressorCompressionTypes(); 
   PdfCompressorOptions pdfCompressorOptions = new PdfCompressorOptions(); 
 
   //Sets compression types needed for each segment 
   compressionTypes.Comp1Bit = PdfCompressor1BitCompression.Zip1Bit; 
   compressionTypes.Comp2Bit = PdfCompressor2BitCompression.Lzw2Bit; 
   compressionTypes.CompPicture = PdfCompressorPictureCompression.JpgPic; 
   compressionTypes.QFactor = 2; 
 
   //Flags for used compression types should be set 
   compressionTypes.Flags = EnabledCompressionsFlags.EnableOneBit | 
      EnabledCompressionsFlags.EnableTwoBit | 
      EnabledCompressionsFlags.EnablePicture; 
 
   pdfCompressor.SetCompression(compressionTypes); 
 
   //Specify suitable profiles for input and output image 
   pdfCompressorOptions.ImageQuality = PdfCompressorImageQuality.ComputerGenerated; 
   pdfCompressorOptions.OutputQuality = PdfCompressorOutputQuality.Excellent; 
   pdfCompressorOptions.Flags = SegmentationOptionsFlags.FavorOneBit | 
      SegmentationOptionsFlags.WithBackground; 
 
   pdfCompressor.SegmentImage += new EventHandler<PdfCompressorSegmentImageEventArgs>(pdfCompressor_SegmentImage_MrcDocument); 
   pdfCompressor.Insert(image, pdfCompressorOptions); 
 
   pdfCompressor.Write(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image.pdf"); 
}
Visual BasicCopy Code
Public Sub pdfCompressor_SegmentImage_MrcSegmentsDocument(ByVal sender As Object, ByVal e As PdfCompressorSegmentImageEventArgs)
  'This will prevent addition to any segment with type background
  If e.Segment.Type = SegmentTypeFlags.Background Then
    e.Cancel = True
  Else
    e.Cancel = False
  End If
End Sub
<Test> _
Public Sub PdfCompressorExample_MrcSegmentsDocument()
  ' Load an image
  RasterCodecs.Startup()
  Dim codecs As RasterCodecs = New RasterCodecs()
  codecs.ThrowExceptionsOnInvalidImages = True

  Dim image As RasterImage = codecs.Load("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\MRCSegmentation.mrc")

  Dim pdfCompressor As PdfCompressor = New PdfCompressor()
  Dim compressionTypes As PdfCompressorCompressionTypes = New PdfCompressorCompressionTypes()

  'Sets compression types needed for each segment
  compressionTypes.Comp1Bit = PdfCompressor1BitCompression.Zip1Bit
  compressionTypes.Comp2Bit = PdfCompressor2BitCompression.Lzw2Bit
  compressionTypes.CompPicture = PdfCompressorPictureCompression.JpgPic
  compressionTypes.QFactor = 2

  'Flags for used compression types should be set
  compressionTypes.Flags = EnabledCompressionsFlags.EnableOneBit Or EnabledCompressionsFlags.EnableTwoBit Or EnabledCompressionsFlags.EnablePicture

    pdfCompressor.SetCompression(compressionTypes)
    AddHandler pdfCompressor.SegmentImage, AddressOf pdfCompressor_SegmentImage_MrcSegmentsDocument
    Dim segmentInfo As PdfCompressorSegmentsInfo() = New PdfCompressorSegmentsInfo(1) {}
    segmentInfo(0) = New PdfCompressorSegmentsInfo()
    segmentInfo(1) = New PdfCompressorSegmentsInfo()
    segmentInfo(0).Rectangle = New Rectangle(0, 0, 100, 100)
    segmentInfo(0).Type = SegmentTypeFlags.Background
    segmentInfo(0).ValidColors = 1
    segmentInfo(0).Colors(0) = New RasterColor(0, 0, 0)



    segmentInfo(1).Rectangle = New Rectangle(100, 100, 200, 200)
    segmentInfo(1).Type = SegmentTypeFlags.Gray
    segmentInfo(1).ValidColors = 1
    segmentInfo(1).Colors(0) = New RasterColor(255, 255, 255)

  pdfCompressor.InsertSegments(image, segmentInfo, False, New RasterColor(0, 0, 0))
  pdfCompressor.Write("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image.pdf")
End Sub
C#Copy Code
public void pdfCompressor_SegmentImage_MrcSegmentsDocument(object sender, PdfCompressorSegmentImageEventArgs e) 

   //This will prevent addition to any segment with type background  
   if(e.Segment.Type == SegmentTypeFlags.Background)  
      e.Cancel = true;  
   else 
      e.Cancel = false;  

 
public void PdfCompressorExample_MrcSegmentsDocument() 

   // Load an image 
   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.ThrowExceptionsOnInvalidImages = true; 
 
   RasterImage image = codecs.Load(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\MRCSegmentation.mrc"); 
 
   PdfCompressor pdfCompressor = new PdfCompressor(); 
   PdfCompressorCompressionTypes compressionTypes = new PdfCompressorCompressionTypes(); 
 
   //Sets compression types needed for each segment 
   compressionTypes.Comp1Bit = PdfCompressor1BitCompression.Zip1Bit; 
   compressionTypes.Comp2Bit = PdfCompressor2BitCompression.Lzw2Bit; 
   compressionTypes.CompPicture = PdfCompressorPictureCompression.JpgPic; 
   compressionTypes.QFactor = 2; 
 
   //Flags for used compression types should be set 
   compressionTypes.Flags = EnabledCompressionsFlags.EnableOneBit | 
                            EnabledCompressionsFlags.EnableTwoBit | 
                            EnabledCompressionsFlags.EnablePicture; 
 
   pdfCompressor.SetCompression(compressionTypes); 
 
 
   PdfCompressorSegmentsInfo[] segmentInfo = new PdfCompressorSegmentsInfo[2]; 
   segmentInfo[0] = new PdfCompressorSegmentsInfo(); 
   segmentInfo[1] = new PdfCompressorSegmentsInfo(); 
   pdfCompressor.SegmentImage += new EventHandler<PdfCompressorSegmentImageEventArgs>(pdfCompressor_SegmentImage_MrcSegmentsDocument); 
   segmentInfo[0].Rectangle = new Rectangle(0, 0, 100, 100); 
   segmentInfo[0].Type = SegmentTypeFlags.Background; 
   segmentInfo[0].ValidColors = 1; 
 
   segmentInfo[0].Colors[0] = new RasterColor(0, 0, 0); 
 
 
 
 
   segmentInfo[1].Rectangle = new Rectangle(100, 100, 200, 200); 
   segmentInfo[1].Type = SegmentTypeFlags.Gray; 
   segmentInfo[1].ValidColors = 1;          
   segmentInfo[1].Colors[0] = new RasterColor(255, 255, 255); 
 
 
   pdfCompressor.InsertSegments(image, segmentInfo, false, new RasterColor(0, 0, 0)); 
   pdfCompressor.Write(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image.pdf"); 
}

Remarks

This class provides the functionality to create a PDF document and provides the following options:
  • Use SetCompression to specify the compression type to be used for segments with the same type, or for the entire image when not using MRC segmentation.
  • Use InsertSegments to control the way that the image will be segmented.
  • Use the PdfCompressorSegmentImageEventArgs delegate method to control how segments are added from the image to the PDF document.

For more information, refer to Creating a Compressed PDF File.

Inheritance Hierarchy

System.Object
   Leadtools.Pdf.PdfCompressor

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Leadtools.Pdf requires a PDF Plug-in license and unlock key. For more information, refer to: Raster Pro/Document/Medical Features