public void InsertSegments(
RasterImage image,
PdfCompressorSegmentsInfo[] segmentInfo,
bool isThereBackGround,
RasterColor backgroundColor
)
image
Image to be inserted in the PDF file.
segmentInfo
Contains information about all of the segments. PdfCompressorSegmentsInfo
isThereBackGround
Determines whether to use the average backgroundColor for all segments with background type or to use each color of each background segment.
backgroundColor
Average background color, to be used for all backgrounds segments if isThereBackGround is true.
For more information, refer to Creating a Compressed PDF File.
using Leadtools;
using Leadtools.PdfCompressor;
using Leadtools.Codecs;
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"));
PdfCompressorEngine pdfCompressor = new PdfCompressorEngine();
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:\LEADTOOLS22\Resources\Images";
}