LEADTOOLS PDF Segmentation (Leadtools.Pdf assembly) Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.30
Insert(RasterImage) Method
See Also 
Leadtools.Pdf Namespace > PdfCompressor Class > Insert Method : Insert(RasterImage) Method



image
Image to be inserted in the PDF file.
image
Image to be inserted in the PDF file.
Compresses the specified image, without segmenting it, and inserts the image in the PDF file.

Syntax

Visual Basic (Declaration) 
Overloads Public Sub Insert( _
   ByVal image As RasterImage _
) 
Visual Basic (Usage)Copy Code
Dim instance As PdfCompressor
Dim image As RasterImage
 
instance.Insert(image)
C# 
public void Insert( 
   RasterImage image
)
C++/CLI 
public:
void Insert( 
   RasterImage^ image
) 

Parameters

image
Image to be inserted in the PDF file.

Example

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

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

   Public Sub PdfCompressorExample_MrcSegmentsDocument()
      ' Load an image
      Dim codecs As RasterCodecs = New RasterCodecs()
      codecs.ThrowExceptionsOnInvalidImages = True

      Dim image As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "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 LeadRect(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 LeadRect(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(Path.Combine(LEAD_VARS.ImagesDir, "Image.pdf"))
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
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 codecs = new RasterCodecs();
      codecs.ThrowExceptionsOnInvalidImages = true;

      RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir,"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 LeadRect(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 LeadRect(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(Path.Combine(LEAD_VARS.ImagesDir,"Out_Image.pdf"));
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}

Remarks

This method will not use MRC segmentation for the inserted image in Pdf file. If Leadtools.Pdf.PdfCompressor.SetCompression method is set, segments will be compressd according to compression types set by it.
For more information, refer to Creating a Compressed PDF File.

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 2000, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7

See Also

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