VintaSoft Imaging .NET SDK 12.4: Documentation for .NET developer
Vintasoft.Imaging.FormsProcessing.FormRecognition Namespace / FormTemplateManager Class / CreateImageFromPageTemplate(FormDocumentTemplate,FormPageTemplate) Method
Syntax Exceptions Remarks Example Requirements SeeAlso
In This Topic
    CreateImageFromPageTemplate(FormDocumentTemplate,FormPageTemplate) Method (FormTemplateManager)
    In This Topic
    Creates/loads an image associated with the page template.
    Syntax
    'Declaration
    
    Protected Overridable Function CreateImageFromPageTemplate( _
    ByVal document
    The document template.
    As FormDocumentTemplate, _
    ByVal page
    The page template.
    As FormPageTemplate _
    ) As Vintasoft.Imaging.VintasoftImage
    protected virtual Vintasoft.Imaging.VintasoftImage CreateImageFromPageTemplate(
    FormDocumentTemplate document,
    FormPageTemplate page
    )
    protected: virtual Vintasoft.Imaging.VintasoftImage* CreateImageFromPageTemplate(
    FormDocumentTemplate* document,
    FormPageTemplate* page
    )

    Parameters

    document
    The document template.
    page
    The page template.

    Return Value

    Image associated with page template.
    Exceptions
    ExceptionDescription
    Thrown if page template does not contain correct file name.
    Remarks

    This method can be used if algorithm that creates/loads an image must be overridden, for example, image must be loaded from a database.

    Example

    This C#/VB.NET code shows how to load the document template that stores page template images in database.

    
    ''' <summary>
    ''' The form template manager that loads page template images from database.
    ''' </summary>
    Public Class MyFormTemplateManager
        Inherits Vintasoft.Imaging.FormsProcessing.FormRecognition.FormTemplateManager
    
        ''' <summary>
        ''' Creates/loads an image, which is associated with the page template.
        ''' </summary>
        ''' <param name="document">The document template.</param>
        ''' <param name="page">The page template.</param>
        ''' <returns>Image associated with page template.</returns>
        Protected Overrides Function CreateImageFromPageTemplate(document As Vintasoft.Imaging.FormsProcessing.FormRecognition.FormDocumentTemplate, page As Vintasoft.Imaging.FormsProcessing.FormRecognition.FormPageTemplate) As Vintasoft.Imaging.VintasoftImage
            ' get the page template image identifier from FormTempatePage.ImageFileName property
            Dim pageTemplateImageId As String = String.Format("{0}{1}", document.FileName, page.ImageFileName)
            If String.IsNullOrEmpty(pageTemplateImageId) Then
                Throw New System.IO.InvalidDataException("Image file identifier is not specified.")
            End If
    
            ' load image file from database
            Using imageFileStream As System.IO.Stream = LoadImageFromDatabase(pageTemplateImageId)
                ' create image decoder for image file
                Using decoder As Vintasoft.Imaging.Codecs.Decoders.DecoderBase = Vintasoft.Imaging.Codecs.Decoders.AvailableDecoders.CreateDecoder(imageFileStream)
                    ' if image file does not have image with specified index
                    If page.ImagePageIndex < 0 OrElse page.ImagePageIndex >= decoder.PageCount Then
                        Throw New System.InvalidOperationException("Cannot create template image: page index is out of range.")
                    End If
    
                    ' get a page template image
                    Using pageTemplateImage As Vintasoft.Imaging.VintasoftImage = decoder.GetImage(page.ImagePageIndex)
                        ' return a clone of page template image because we need to dispose decoder and image file stream
                        Return DirectCast(pageTemplateImage.Clone(), Vintasoft.Imaging.VintasoftImage)
                    End Using
                End Using
            End Using
        End Function
    
    
        ''' <summary>
        ''' Loads image from database.
        ''' </summary>
        ''' <param name="imageId">An image file identifier in database.</param>
        ''' <returns>A stream that contains image file data.</returns>
        Private Function LoadImageFromDatabase(imageId As String) As System.IO.Stream
            ' load image file from file stream - change this code to the code that loads image file from database
            Return New System.IO.FileStream(imageId, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        End Function
    
    End Class
    
    
    
    /// <summary>
    /// The form template manager that loads page template images from database.
    /// </summary>
    public class MyFormTemplateManager : Vintasoft.Imaging.FormsProcessing.FormRecognition.FormTemplateManager
    {
    
        /// <summary>
        /// Creates/loads an image, which is associated with the page template.
        /// </summary>
        /// <param name="document">The document template.</param>
        /// <param name="page">The page template.</param>
        /// <returns>Image associated with page template.</returns>
        protected override Vintasoft.Imaging.VintasoftImage CreateImageFromPageTemplate(
            Vintasoft.Imaging.FormsProcessing.FormRecognition.FormDocumentTemplate document,
            Vintasoft.Imaging.FormsProcessing.FormRecognition.FormPageTemplate page)
        {
            // get the page template image identifier from FormTempatePage.ImageFileName property
            string pageTemplateImageId = string.Format("{0}{1}", document.FileName, page.ImageFileName);
            if (string.IsNullOrEmpty(pageTemplateImageId))
                throw new System.IO.InvalidDataException("Image file identifier is not specified.");
    
            // load image file from database
            using (System.IO.Stream imageFileStream = LoadImageFromDatabase(pageTemplateImageId))
            {
                // create image decoder for image file
                using (Vintasoft.Imaging.Codecs.Decoders.DecoderBase decoder = Vintasoft.Imaging.Codecs.Decoders.AvailableDecoders.CreateDecoder(imageFileStream))
                {
                    // if image file does not have image with specified index
                    if (page.ImagePageIndex < 0 || page.ImagePageIndex >= decoder.PageCount)
                        throw new System.InvalidOperationException("Cannot create template image: page index is out of range.");
    
                    // get a page template image
                    using (Vintasoft.Imaging.VintasoftImage pageTemplateImage = decoder.GetImage(page.ImagePageIndex))
                    {
                        // return a clone of page template image because we need to dispose decoder and image file stream
                        return (Vintasoft.Imaging.VintasoftImage)pageTemplateImage.Clone();
                    }
                }
            }
        }
    
    
        /// <summary>
        /// Loads image from database.
        /// </summary>
        /// <param name="imageId">An image file identifier in database.</param>
        /// <returns>A stream that contains image file data.</returns>
        private System.IO.Stream LoadImageFromDatabase(string imageId)
        {
            // load image file from file stream - change this code to the code that loads image file from database
            return new System.IO.FileStream(imageId, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        }
    
    }
    
    

    Requirements

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

    See Also