简体   繁体   English

如何使用itextsharp获取pdf图像方向

[英]how to get pdf image orientation using itextsharp

Im editing a pdf. 我正在编辑PDF。 The client wants the image inside pdf to be resize and rotated. 客户希望调整pdf中的图像大小并旋转。

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) 所以我要做的是提取pdf内的图像,以便能够处理该图像,然后将其再次插入pdf(替换旧的pdf)

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/ 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 但是当我提取图像到图像旋转180度

i even used the free Spire.PDF to extract the image but the extracted image of the spire.pdf is rotated 90 degree. 我什至使用免费的Spire.PDF提取图像,但是将spire.pdf提取的图像旋转90度。 so how can i get the image orientation of the pdf. 所以我怎么能得到pdf的图像方向。 so that i can make the image to its original orientation. 这样我就可以使图像恢复其原始方向。 thank you 谢谢

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°. 因此,变换矩阵显然仅将图像缩放到适当的尺寸,而不旋转图像,但是页面本身被定义为旋转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 但是当我提取图像到图像旋转180度

I get an image from your code which has to be rotated by 270° clockwise to be upright. 我从您的代码中获得了一个图像,该图像必须顺时针旋转270°才能直立。

If you indeed get an image rotated by 180°, you should check the version of iTextSharp you use. 如果确实将图像旋转了180°,则应检查所用iTextSharp的版本。 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. 您引用的网站上的存档包含一个相当老的版本5.3.5.0,并且在此期间可能已修复了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM