简体   繁体   中英

Why X/Y coordinators of image in pdf are always ZERO by GetImageCTM?

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. Any help will be appreciated.

I get the Matrix by ImageRenderInfo#GetImageCTM(), and get the X/Y coordiantors. but it is always 0!

I have tried these api, GetStartPoint(), and GetImageCTM. However, the X/Y coordinator is always 0 :-(

Note: I have some images in pdf in some positons(not the (0,0) coordinator).

void IRenderListener.RenderImage(ImageRenderInfo imgRenderInfo)
{
    Matrix mtx = imgRenderInfo.GetImageCTM();
    // x, y
    float[] coordinate = new float[] { mtx[Matrix.I31], mtx[Matrix.I32] };

    // Why the coordinate[0] and coordinate[1]
    // are always be ZERO regardless the positon in Pdf

}

Answer to the question as is

I parsed the page contents of an arbitrary file I had at hands (the example PDF from this question ) using a render listener with your implementation code plus output of the coordinates:

public void ExtractImageCoordinatesFromArchmodels()
{
    using (PdfReader reader = new PdfReader(@"EVERMOTION ARCHMODELS VOL.78.pdf"))
    {
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        ImageCoordinatesRenderListener listener = new ImageCoordinatesRenderListener();
        for (var i = 1; i <= reader.NumberOfPages; i++)
        {
            parser.ProcessContent(i, listener);
        }
    }
}

internal class ImageCoordinatesRenderListener : IRenderListener
{
    public void BeginTextBlock()
    { }

    public void EndTextBlock()
    { }

    public void RenderText(TextRenderInfo renderInfo)
    { }

    public void RenderImage(ImageRenderInfo renderInfo)
    {
        Matrix mtx = renderInfo.GetImageCTM();
        // x, y
        float[] coordinate = new float[] { mtx[Matrix.I31], mtx[Matrix.I32] };
        Console.WriteLine("Image at {0}, {1}.", coordinate[0], coordinate[1]);
    }
}

and the output was

Image at 6,00029, 52,15466.
Image at 19,84251, 363,4501.
Image at 294,091, 361,5604.
Image at 300,0336, 81,089.
Image at 15,59055, 72,94052.
Image at 5,322647, 340,7029.
Image at 288,5311, 386,0621.
Image at 291,7613, 69,35573.
Image at 28,50845, 53,13286.
Image at 41,2021, 380,3172.
Image at 290,8796, 368,9564.
Image at 295,8532, 50,71478.
Image at 19,13385, 49,21146.
Image at 25,5118, 385,9343.
Image at 282,4584, 379,8427.
Image at 293,5927, 65,19702.
Image at 4,535416, 60,35075.
Image at 3,364258, 374,4344.
Image at 288,0557, 373,5591.
Image at 299,9102, 59,13971.
Image at 11,33858, 66,10181.
Image at 11,66959, 380,3134.
Image at 297,1836, 378,4615.
Image at 299,9689, 66,74164.
Image at 10,62991, 53,18137.
Image at 5,180252, 377,7065.
Image at 279,9567, 377,9544.
Image at 289,9219, 69,23323.
Image at 6,400314, 68,17795.
Image at 11,33858, 361,2458.
Image at 297,1935, 373,4553.
Image at 299,8854, 68,30142.
Image at 7,086609, 68,13367.
Image at 3,82518, 352,3451.
Image at 287,9208, 373,4846.
Image at 294,6425, 68,3132.
Image at 41,2271, 68,15968.
Image at 5,709488, 356,2161.
Image at 304,9857, 373,593.
Image at 282,4557, 48,97745.
Image at 5,669281, 53,65367.
Image at 27,34265, 382,0123.
Image at 297,1409, 373,494.
Image at 300,0584, 50,23624.
Image at 7,245102, 68,23528.
Image at 8,503922, 380,1963.
Image at 290,1901, 355,9355.
Image at 287,2598, 60,53516.
Image at 5,102356, 68,01541.
Image at 17,00786, 378,9057.
Image at 296,8928, 373,5667.
Image at 299,9655, 68,04535.

(My locale uses a comma as decimal separator.)

So I cannot reproduce your claim

the X/Y coordinator is always 0

Thus, what you observed is either due to some issue of your remaining code or something special about all your test PDFs; probably they simply indeed all have their images positioned at 0,0

Clarifications from comments

Meanwhile the OP has clarified in comments that the images of interest are located in annotation appearance streams, not in the page content stream.

Coordinates therein are in the respective annotation appearance stream coordinate system which is implied by the appearance's bounding box (its BBox entry). This bounding box then optionally is transformed by the appearance matrix (its Matrix entry). The resulting four sided area then is scaled and moved into the annotation's rectangle (its Rect entry). And depending on page rotation and annotation properties, this rectangle may be rotated by a multiple of 90° relative to the page coordinates.

Thus, a general solution transforming those coordinates into the default user space coordinate system of the page requires some math.

Often, though, bitmaps in annotation appearances are filling the bounding box (nearly) completely. Often there is no appearance matrix. And often annotations rotate with the page.

Thus, an often good approximation is to simply use the annotation rectangle. This also is what the OP now uses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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