pdf asker pdf asker - 2 months ago 61
C# Question

how to get pdf image orientation using itextsharp

Im editing a pdf. The client wants the image inside pdf to be resize and rotated.

so what i did is to extract the image inside the pdf to be able to manipulate the image then insert it again to the the pdf(replacing the old one)

here is the code where i got the code for extracting image

https://psycodedeveloper.wordpress.com/2013/01/10/how-to-extract-images-from-pdf-files-using-c-and-itextsharp/

but when i extract the image to image is rotated 180 degree

i even used the free Spire.PDF to extract the image but the extracted image of the spire.pdf is rotated 90 degree. so how can i get the image orientation of the pdf. so that i can make the image to its original orientation. thank you

mkl mkl
Answer

There are two relevant factors deciding on the effective rotation of an image, the current transformation matrix at the time the image is drawn (which also fixes the dimensions of the image) and the page rotation.

You can determine these values as shown below in the code you refer to:

...

public static Dictionary<string, System.Drawing.Image> ExtractImages(string filename)
{
    var images = new Dictionary<string, System.Drawing.Image>();

    using (var reader = new PdfReader(filename))
    {
        var parser = new PdfReaderContentParser(reader);
        ImageRenderListener listener = null;

        for (var i = 1; i <= reader.NumberOfPages; i++)
        {
            // v-- Determine clockwise rotation of page
            Console.WriteLine("Page {1} is rotated by {0}°.\n", reader.GetPageRotation(i), i);
            // ^-- Determine clockwise rotation of page

            parser.ProcessContent(i, (listener = new ImageRenderListener()));
            var index = 1;
            [...]
        }
        return images;
    }
}

...

public void RenderImage(ImageRenderInfo renderInfo)
{
    // v-- Determine transformation matrix of image
    Matrix ctm = renderInfo.GetImageCTM();
    Console.WriteLine("Found image with transformation matrix:\n{0}\n", ctm);
    // ^-- Determine transformation matrix of image

    PdfImageObject image = renderInfo.GetImage();
    PdfName filter = (PdfName)image.Get(PdfName.FILTER);
    [...]
}

...

The output in your case:

Page 1 is rotated by 270°.

Found image with transformation matrix:
792,0001   0   0
  0      612   0
  0        0   1

Found 1 images on page 1.

Thus, the transformation matrix obviously only scales the image to the appropriate dimensions without rotating it but the page itself is defined to be shown rotated by 270°.

This corresponds to my observations. In particular in contrast to what you said:

but when i extract the image to image is rotated 180 degree

I get an image from your code which has to be rotated by 270° clockwise to be upright.

If you indeed get an image rotated by 180°, you should check the version of iTextSharp you use. The archive on the web site you refer to contains a fairly old version, 5.3.5.0, and bugs might have been fixed in the meantime.