[C#] Automatically rotate pages of a multipage tiff

Example requests & Code samples for GdPicture Toolkits.
Post Reply
User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

[C#] Automatically rotate pages of a multipage tiff

Post by Loïc » Wed Jun 10, 2015 2:41 pm

Hi,

Here a code snippet that demonstrates how to rotate page of a multipage tiff document.

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            autoRotate("d:\\multipage.tif", "d:\\output.tif");
        }

        private const string DICT_PATH = @"D:\GdPicture\redist\ocr";
        private const string LANG = "eng";
        private static GdPictureStatus autoRotate(string filePath, string destPath)
        {
            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)
                        {
                            int pageRotation = gdpictureImaging.OCRTesseractGetOrientation(imageID, LANG, DICT_PATH);
                            if (pageRotation != 0)
                            {
                                hasRotation = true;
                                status = gdpictureImaging.RotateAngle(imageID, 360 - pageRotation);
                            }
                        }
                        if (status != GdPictureStatus.OK)
                        {
                            break;
                        }
                    }

                    if (status == GdPictureStatus.OK)
                    {
                        if (hasRotation)
                        {
                            status = gdpictureImaging.TiffSaveMultiPageToFile(imageID, destPath, TiffCompression.TiffCompressionAUTO);
                        }
                        else
                        {
                            System.IO.File.Copy(filePath, destPath);
                        }
                    }
                    gdpictureImaging.ReleaseGdPictureImage(imageID);
                }
            }

            return status;
        }

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: [C#] Automatically rotate pages of a multipage tiff

Post by Gabriela » Wed Nov 22, 2017 12:01 pm

Here you can find updated code snippet based on GdPicture.NET 14:

How to automatically rotate pages of a multipage TIFF file using OCR
https://www.gdpicture.com/guides/gdpicture/web ... 0OCR.html

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest