Reference Guide
Programming / OCR / How to automatically rotate pages of a multipage TIFF file using OCR
In This Topic
    How to automatically rotate pages of a multipage TIFF file using OCR
    In This Topic

    This is how to automatically rotate pages of a multipage TIFF file with help of OCR Tesseract engine method. After the successful rotation of all pages the source file is saved to the destination file.

    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    Dim filepath As String = "multipage.tif"
    Dim destpath As String = "output.tif"
    'Specify your path.
    Dim DICT_PATH As String = "C:\GdPicture.NET 14\Redist\OCR"
    'Specify your language.
    Dim LANG As OCRLanguage = OCRLanguage.English
    
    Dim status As GdPictureStatus
    Using gdpictureImaging As New GdPictureImaging()
        Dim hasRotation As Boolean = False
        Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile(filepath)
        status = gdpictureImaging.GetStat()
        If status = GdPictureStatus.OK Then
            Dim pageCount As Integer = gdpictureImaging.GetPageCount(imageID)
            For i As Integer = 1 To pageCount
                status = gdpictureImaging.SelectPage(imageID, i)
                If status = GdPictureStatus.OK Then
                    Using gdpictureOCR As GdPictureOCR = New GdPictureOCR()
                        gdpictureOCR.ResourceFolder = DICT_PATH
                        gdpictureOCR.AddLanguage(LANG)
                        status = gdpictureOCR.SetImage(imageID)
                        Dim pageRotation As Integer = gdpictureOCR.GetOrientation()
                        If pageRotation <> 0 Then
                            hasRotation = True
                            status = gdpictureImaging.RotateAngle(imageID, 360 - pageRotation)
                        End If
                    End Using
                End If
                If status <> GdPictureStatus.OK Then
                    MessageBox.Show("Error: " + status, "Rotation + OCR Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit For
                End If
            Next
            If status = GdPictureStatus.OK Then
                If hasRotation Then
                    status = gdpictureImaging.TiffSaveMultiPageToFile(imageID, destpath, TiffCompression.TiffCompressionAUTO)
                    If status = GdPictureStatus.OK Then
                        MessageBox.Show("Done!", "Rotation + OCR Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Else
                        MessageBox.Show("Error: " + status, "Rotation + OCR Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    System.IO.File.Copy(filepath, destpath)
                End If
            End If
            gdpictureImaging.ReleaseGdPictureImage(imageID)
        End If
    End Using                    
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    string filepath = "multipage.tif";
    string destpath = "output.tif";
    string DICT_PATH = "C:\\GdPicture.NET 14\\Redist\\OCR"; //Specify your path.
    OCRLanguage LANG = OCRLanguage.English; //Specify your language.
    
    GdPictureStatus status;
    using (GdPictureImaging gdpictureImaging = new GdPictureImaging())
    {
        bool hasRotation = false;
        int imageID = gdpictureImaging.CreateGdPictureImageFromFile(filepath);
        status = gdpictureImaging.GetStat();
        if (status == GdPictureStatus.OK)
        {
            int pageCount = gdpictureImaging.GetPageCount(imageID);
            for (int i = 1; i <= pageCount; i++)
            {
                status = gdpictureImaging.SelectPage(imageID, i);
                if (status == GdPictureStatus.OK)
                {
                    using (GdPictureOCR gdpictureOCR = new GdPictureOCR())
                    {
                        gdpictureOCR.ResourceFolder = DICT_PATH;
                        gdpictureOCR.AddLanguage(LANG);
                        status = gdpictureOCR.SetImage(imageID);
                        int pageRotation = gdpictureOCR.GetOrientation();
                        if (pageRotation != 0)
                        {
                            hasRotation = true;
                            status = gdpictureImaging.RotateAngle(imageID, 360 - pageRotation);
                        }
                    }
                }
                if (status != GdPictureStatus.OK)
                {
                    MessageBox.Show("Error: " + status, "Rotation + OCR Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }
            if (status == GdPictureStatus.OK)
            {
                if (hasRotation)
                {
                    status = gdpictureImaging.TiffSaveMultiPageToFile(imageID, destpath, TiffCompression.TiffCompressionAUTO);
                    if (status == GdPictureStatus.OK)
                        MessageBox.Show("Done!", "Rotation + OCR Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show("Error: " + status, "Rotation + OCR Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    System.IO.File.Copy(filepath, destpath);
                }
            }
            gdpictureImaging.ReleaseGdPictureImage(imageID);
        }
    }