Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}°.
", 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:
{0}
", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...