Automatically rotate pages of a multipage TIFF file using OCR and C#

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


C#
// 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);
}
}

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.