0
votes

I have read "media box is always [0, 0, page width, page height]" but my PDF page's MediaBox is [0 383 599 1224].

So when I draw line like "gfx.drawLine(XPens.gold, 0,0, 100, 50);", it is rendered in the middle of the page.

When I draw line like "gfx.drawLine(XPens.gold, 0, -200, 100, 50);", it's rendered at the top of the page.

I want to draw a line at the same position on all PDF pages.
If I use the following code, is it the right way?

gfx.drawline(Xpens.gold, (X - pdfpage.mediabox.x1),
    (Y - pdfpage.mediabox.y1 ), width, height);

And can I get some reference of "PDF page sizes and mediabox"?


Additional Question

1) The exact action i want to do is to open the PDF (created other library) and add the rectangle to the specific position.

how can i get specific position in pages ?

2) about "The crop box defines the region to which the contents of the page shall be clipped (cropped) when displayed or printed"

As far as i understand, i can not see outside the crop box region. when i opened the file using PDF reader

but when i open file using PDF reader(acrobat), the first page has a full sized image like this:

this

Image looks like it was drawn outside the crop box region, is it possible?


I learned something new about my problem and i think my problem goes through the process below

  1. When i open PDF file with PDFSharp and get graphic context, it converts the coordinates system.

  2. When i draw something PDF element at (0,0), it is drawn at the (0,0) point of the crop box area ( In following picture, element is green rectangle)

  3. IF the area of the corp box is [0, 300, 600, 1200], elementis drawn at the (0,300) point of the page area. Because the crop box's (0.0) is equal to the pages'(0,300)

  4. So when i save page, the new element will be 300 points below the top of the original page

Did i get it right?

If it is right, can i convert the coordinates of the four corners of the crop box to match the page coordinates

For example. if i want to drawn rectangle at the top of the page, draw it at (0, -300) instead of (0,0)

1

2
"how can i get specific position in pages ?" - What exactly do you mean by *specific position? "Image looks like it was drawn outside the crop box region, is it possible?" - Please share the PDF in question. Without it this becomes guesswork. In particular as in your image the rectangle appears to be clearly inside the page area...mkl
1. Actually i am not English user, so detailed explain is difficult for me. i mean "specific position" is a point of page that is drawn on the window, when i open file using PDF reader.jung
2. I agree that is the best way, but my company policy does not allow file uploads... so i upload more detailed question, Thanks for your replyjung

2 Answers

0
votes

PDF pages created with PDFsharp will always start at (0, 0) unless the creator tweaked the page settings.

For pages created with other libraries you cannot assume anything and have to take all elements of MediaBox into account when drawing new elements onto the page.

0
votes

To start with, the media box is not the most important box here, the crop box is, cf. this answer quoting the description of the relevant boxes from the PDF specification. If it is not explicitly defined, though, it defaults to the media box.

And just like @Vive already has answered, in general you cannot assume anything in excess of what's specified, i.e.

MediaBox rectangle (Required; inheritable) A rectangle (see 7.9.5, "Rectangles"), expressed in default user space units, that shall define the boundaries of the physical medium on which the page shall be displayed or printed (see 14.11.2, "Page Boundaries").

CropBox rectangle (Optional; inheritable) A rectangle, expressed in default user space units, that shall define the visible region of default user space. When the page is displayed or printed, its contents shall be clipped (cropped) to this rectangle and then shall be imposed on the output medium in some implementation-defined manner (see 14.11.2, "Page Boundaries"). Default value: the value of MediaBox.

(ISO 32000-1, Table 30 – Entries in a page object)

in combination with

Rectangles are used to describe locations on a page and bounding boxes for a variety of objects. A rectangle shall be written as an array of four numbers giving the coordinates of a pair of diagonally opposite corners.

(ISO 32000-1, section 7.9.5 Rectangles)

And in the wild you do find all kinds of media boxes, in particular you even find media boxes which don't contain [lower-left-x lower-left-y upper-right-x upper-right-y] but which instead use the upper-left and lower-right corners or which start with the upper right coordinates and have the lower left coordinates thereafter! They are fairly seldom, though, so you might be lucky and never have to deal with such a box.

On the other hand it is not uncommon not to start in the origin (0,0) but instead somewhere else.


By the way, in the constructors PdfSharp's PdfRectangle class is quite aware that the two given points are merely diagonally opposite corners but not necessarily first lower left corner, then upper right corner, and does not sort the coordinates, cf. the comments there, e.g.:

    /// <summary>
    /// Initializes a new instance of the PdfRectangle class with two points specifying
    /// two diagonally opposite corners.
    /// </summary>
    public PdfRectangle(XPoint pt1, XPoint pt2)
    {
        _x1 = pt1.X;
        _y1 = pt1.Y;
        _x2 = pt2.X;
        _y2 = pt2.Y;
    }

But later, e.g. in the Contains methods, the code suddenly assumes that the first point is the lower left and the second point is the upper right, e.g.

    /// <summary>
    /// Determines if the specified point is contained within this PdfRectangle.
    /// </summary>
    public bool Contains(double x, double y)
    {
        // Treat rectangle inclusive/inclusive.
        return _x1 <= x && x <= _x2 && _y1 <= y && y <= _y2;
    }

So as soon as you stumble over a funny media box, many PdfSharp methods using PdfRectangle are likely to fail in wondrous ways. ;)