VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
Vintasoft.Imaging.Pdf.Ocr Namespace / PdfDocumentBuilder Class / SetAsBackground Methods / SetAsBackground(Int32,OcrPage) Method
Syntax Example Requirements SeeAlso
In This Topic
SetAsBackground(Int32,OcrPage) Method (PdfDocumentBuilder)
In This Topic
Sets the OCR page as background layer of PDF page with specified index.
Syntax
'Declaration

Public Overloads Sub SetAsBackground( _
ByVal index
The index of PDF page.
As System.Int32, _
ByVal ocrPage
The OCR page, which must be set as background of PDF page.
As Vintasoft.Imaging.Ocr.Results.OcrPage _
)
public void SetAsBackground(
System.Int32 index,
Vintasoft.Imaging.Ocr.Results.OcrPage ocrPage
)

Parameters

index
The index of PDF page.
ocrPage
The OCR page, which must be set as background of PDF page.
Example

This C#/VB.NET code shows how to convert image collection to a searchable PDF document with MRC compression:


' Required assemblies to run this code:
' Vintasoft.Imaging.dll, Vintasoft.Imaging.Ocr.dll, Vintasoft.Imaging.Ocr.Tesseract.dll,
' Vintasoft.Imaging.Pdf.dll, Vintasoft.Imaging.Pdf.Ocr.dll, Vintasoft.Imaging.Pdf.Mrc.dll,
' Vintasoft.Imaging.Jbig2.dll, Vintasoft.Imaging.Jpeg2000.dll
Public Shared Sub ConvertToSearchablePdfWithMrcCompression(language As Vintasoft.Imaging.Ocr.OcrLanguage, images As Vintasoft.Imaging.ImageCollection, mrcResolution As Vintasoft.Imaging.Resolution, ocrResolution As Vintasoft.Imaging.Resolution, pdfFilename As String)
    ' create PDF MRC encoder
    Dim encoder As New Vintasoft.Imaging.Codecs.Encoders.PdfMrcEncoder()

    ' set MRC compression settings
    SetSetting_DocumentWithImagesHiQuality(encoder.MrcCompressionSettings)

    ' set MRC resolution (if source is PDF document)
    images.SetRenderingSettings(New Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(mrcResolution))

    ' handle ImageSaving event to set JBIG2 compression for black-white images
    AddHandler encoder.ImageSaving, New System.EventHandler(Of Vintasoft.Imaging.ImageSavingEventArgs)(AddressOf images_ImageSaving)

    ' save images to PDF use MRC compression
    System.Console.WriteLine("Encoding using PDF MRC compression...")
    images.SaveSync(pdfFilename, encoder)

    ' open PDF document with MRC compression
    Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, False)
        System.Console.WriteLine("Create Tesseract OCR engine...")
        Using tesseractOcr As New Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr()
            System.Console.WriteLine("Initialize OCR engine...")
            tesseractOcr.Init(New Vintasoft.Imaging.Ocr.OcrEngineSettings(language))

            ' create PDF document builder
            Dim documentBuilder As New Vintasoft.Imaging.Pdf.Ocr.PdfDocumentBuilder(document)

            ' set OCR resolution (if source is PDF document)
            images.SetRenderingSettings(New Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(ocrResolution))

            ' foreach image
            For i As Integer = 0 To images.Count - 1
                System.Console.WriteLine("Recognize image...")
                Dim page As Vintasoft.Imaging.Ocr.Results.OcrPage = tesseractOcr.Recognize(images(i))

                System.Console.WriteLine("Set PDF page background...")
                documentBuilder.SetAsBackground(i, page)
            Next

            ' shutdown OCR engine
            tesseractOcr.Shutdown()

            System.Console.WriteLine("Pack PDF document...")
            document.Pack()
            System.Console.WriteLine("OK.")
        End Using
    End Using
End Sub

''' <summary>
''' Handles the ImageSaving event of the image collection.
''' </summary>
Private Shared Sub images_ImageSaving(sender As Object, e As Vintasoft.Imaging.ImageSavingEventArgs)
    Dim encoder As Vintasoft.Imaging.Codecs.Encoders.PdfMrcEncoder = TryCast(sender, Vintasoft.Imaging.Codecs.Encoders.PdfMrcEncoder)
    ' set JBIG2 compression for black-white images
    Dim isBlackWhiteImage As Boolean = e.Image.PixelFormat = Vintasoft.Imaging.PixelFormat.BlackWhite
    If Not isBlackWhiteImage Then
        Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = Vintasoft.Imaging.Pdf.PdfDocumentController.GetPageAssociatedWithImage(e.Image)
        If page IsNot Nothing AndAlso page.BackgroundImage IsNot Nothing Then
            isBlackWhiteImage = page.BackgroundImage.PixelFormat = Vintasoft.Imaging.PixelFormat.BlackWhite
        End If
        If isBlackWhiteImage Then
            e.Image.RenderingSettings.Resolution = page.DefaultResolution
        End If
    End If
    If isBlackWhiteImage Then
        encoder.MrcCompressionSettings.EnableMrcCompression = False
        encoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jbig2
        System.Console.WriteLine(String.Format("Encoding image {0} using JBIG2 compression...", e.Image.SourceInfo.PageIndex))
    Else
        encoder.MrcCompressionSettings.EnableMrcCompression = True
        System.Console.WriteLine(String.Format("Encoding image {0} using MRC compression...", e.Image.SourceInfo.PageIndex))
    End If
End Sub

''' <summary>
''' Sets MRC compression settings to compress the document
''' images, which contain text and pictures, using MRC compression
''' algorithm and get the resulting images with high quality.
''' </summary>
Public Shared Sub SetSetting_DocumentWithImagesHiQuality(settings As Vintasoft.Imaging.Codecs.Encoders.PdfMrcCompressionSettings)
    settings.CreateBackgroundLayer = True
    settings.BackgroundLayerCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg Or Vintasoft.Imaging.Pdf.PdfCompression.Zip
    settings.BackgroundLayerCompressionSettings.JpegQuality = 60

    settings.ImageSegmentation = New Vintasoft.Imaging.ImageProcessing.Info.ImageSegmentationCommand()
    settings.CreateImagesLayer = False

    settings.HiQualityMask = True
    settings.MaskCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jbig2
    settings.MaskCompressionSettings.Jbig2Settings.Lossy = True

    settings.CreateFrontLayer = True
    settings.HiQualityFrontLayer = True
    settings.FrontLayerCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg2000
    settings.FrontLayerCompressionSettings.Jpeg2000Settings.CompressionRatio = 300 * 3
    settings.FrontLayerCompressionSettings.Jpeg2000Settings.CompressionType = Vintasoft.Imaging.Codecs.ImageFiles.Jpeg2000.Jpeg2000CompressionType.Lossy
End Sub


// Required assemblies to run this code:
// Vintasoft.Imaging.dll, Vintasoft.Imaging.Ocr.dll, Vintasoft.Imaging.Ocr.Tesseract.dll,
// Vintasoft.Imaging.Pdf.dll, Vintasoft.Imaging.Pdf.Ocr.dll, Vintasoft.Imaging.Pdf.Mrc.dll,
// Vintasoft.Imaging.Jbig2.dll, Vintasoft.Imaging.Jpeg2000.dll
public static void ConvertToSearchablePdfWithMrcCompression(
    Vintasoft.Imaging.Ocr.OcrLanguage language,
    Vintasoft.Imaging.ImageCollection images,
    Vintasoft.Imaging.Resolution mrcResolution,
    Vintasoft.Imaging.Resolution ocrResolution,
    string pdfFilename)
{
    // create PDF MRC encoder
    Vintasoft.Imaging.Codecs.Encoders.PdfMrcEncoder encoder =
        new Vintasoft.Imaging.Codecs.Encoders.PdfMrcEncoder();

    // set MRC compression settings
    SetSetting_DocumentWithImagesHiQuality(encoder.MrcCompressionSettings);

    // set MRC resolution (if source is PDF document)
    images.SetRenderingSettings(new Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(mrcResolution));

    // handle ImageSaving event to set JBIG2 compression for black-white images
    encoder.ImageSaving +=
        new System.EventHandler<Vintasoft.Imaging.ImageSavingEventArgs>(images_ImageSaving);

    // save images to PDF use MRC compression
    System.Console.WriteLine("Encoding using PDF MRC compression...");
    images.SaveSync(pdfFilename, encoder);

    // open PDF document with MRC compression
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, false))
    {
        System.Console.WriteLine("Create Tesseract OCR engine...");
        using (Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr tesseractOcr =
            new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr())
        {
            System.Console.WriteLine("Initialize OCR engine...");
            tesseractOcr.Init(new Vintasoft.Imaging.Ocr.OcrEngineSettings(language));

            // create PDF document builder
            Vintasoft.Imaging.Pdf.Ocr.PdfDocumentBuilder documentBuilder =
                new Vintasoft.Imaging.Pdf.Ocr.PdfDocumentBuilder(document);

            // set OCR resolution (if source is PDF document)
            images.SetRenderingSettings(new Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(ocrResolution));

            // foreach image
            for (int i = 0; i < images.Count; i++)
            {
                System.Console.WriteLine("Recognize image...");
                Vintasoft.Imaging.Ocr.Results.OcrPage page = tesseractOcr.Recognize(images[i]);

                System.Console.WriteLine("Set PDF page background...");
                documentBuilder.SetAsBackground(i, page);
            }

            // shutdown OCR engine
            tesseractOcr.Shutdown();

            System.Console.WriteLine("Pack PDF document...");
            document.Pack();
            System.Console.WriteLine("OK.");
        }
    }
}

/// <summary>
/// Handles the ImageSaving event of the image collection.
/// </summary>
private static void images_ImageSaving(object sender, Vintasoft.Imaging.ImageSavingEventArgs e)
{
    Vintasoft.Imaging.Codecs.Encoders.PdfMrcEncoder encoder =
        sender as Vintasoft.Imaging.Codecs.Encoders.PdfMrcEncoder;
    // set JBIG2 compression for black-white images
    bool isBlackWhiteImage = e.Image.PixelFormat == Vintasoft.Imaging.PixelFormat.BlackWhite;
    if (!isBlackWhiteImage)
    {
        Vintasoft.Imaging.Pdf.Tree.PdfPage page =
            Vintasoft.Imaging.Pdf.PdfDocumentController.GetPageAssociatedWithImage(e.Image);
        if (page != null && page.BackgroundImage != null)
            isBlackWhiteImage = page.BackgroundImage.PixelFormat == Vintasoft.Imaging.PixelFormat.BlackWhite;
        if (isBlackWhiteImage)
            e.Image.RenderingSettings.Resolution = page.DefaultResolution;
    }
    if (isBlackWhiteImage)
    {
        encoder.MrcCompressionSettings.EnableMrcCompression = false;
        encoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jbig2;
        System.Console.WriteLine(string.Format("Encoding image {0} using JBIG2 compression...", e.Image.SourceInfo.PageIndex));
    }
    else
    {
        encoder.MrcCompressionSettings.EnableMrcCompression = true;
        System.Console.WriteLine(string.Format("Encoding image {0} using MRC compression...", e.Image.SourceInfo.PageIndex));
    }
}

/// <summary>
/// Sets MRC compression settings to compress the document
/// images, which contain text and pictures, using MRC compression
/// algorithm and get the resulting images with high quality.
/// </summary>
public static void SetSetting_DocumentWithImagesHiQuality(
    Vintasoft.Imaging.Codecs.Encoders.PdfMrcCompressionSettings settings)
{
    settings.CreateBackgroundLayer = true;
    settings.BackgroundLayerCompression =
        Vintasoft.Imaging.Pdf.PdfCompression.Jpeg |
        Vintasoft.Imaging.Pdf.PdfCompression.Zip;
    settings.BackgroundLayerCompressionSettings.JpegQuality = 60;

    settings.ImageSegmentation =
        new Vintasoft.Imaging.ImageProcessing.Info.ImageSegmentationCommand();
    settings.CreateImagesLayer = false;

    settings.HiQualityMask = true;
    settings.MaskCompression =
        Vintasoft.Imaging.Pdf.PdfCompression.Jbig2;
    settings.MaskCompressionSettings.Jbig2Settings.Lossy = true;

    settings.CreateFrontLayer = true;
    settings.HiQualityFrontLayer = true;
    settings.FrontLayerCompression =
        Vintasoft.Imaging.Pdf.PdfCompression.Jpeg2000;
    settings.FrontLayerCompressionSettings.Jpeg2000Settings.CompressionRatio = 300 * 3;
    settings.FrontLayerCompressionSettings.Jpeg2000Settings.CompressionType =
        Vintasoft.Imaging.Codecs.ImageFiles.Jpeg2000.Jpeg2000CompressionType.Lossy;
}

Requirements

Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also