如何使用itextsharp获取pdf图像方向 [英] how to get pdf image orientation using itextsharp

查看:215
本文介绍了如何使用itextsharp获取pdf图像方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑pdf.客户希望调整pdf中的图像大小并旋转.

所以我要做的是提取pdf内的图像以能够处理该图像,然后将其再次插入pdf(取代旧的pdf)

这是我获取图像提取代码的代码

解决方案

有两个相关因素决定图像的有效旋转,即绘制图像时的当前变换矩阵(这也确定了图像的尺寸).图片)和页面旋转.

您可以确定以下参考代码中所示的这些值:

...

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);
    [...]
}

...

您的情况下的输出:

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.

因此,变换矩阵显然仅将图像缩放到适当的尺寸而没有旋转图像,但是页面本身被定义为显示为旋转270°.

这符合我的观察.特别是与您所说的相反:

但是当我将图像提取到图像时,图像会旋转180度

我从您的代码中获得了一幅图像,该图像必须顺时针旋转270°才能直立.

如果确实将图像旋转了180°,则应检查所用iTextSharp的版本.您所引用的网站上的存档包含一个相当老的版本5.3.5.0,并且在此期间可能已修复了错误.

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

解决方案

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.

这篇关于如何使用itextsharp获取pdf图像方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆