Skip to content

Snapping elements together

kelvin0 edited this page Apr 10, 2012 · 7 revisions

PyXML2PDF uses the 'snapto' attribute that allows to position an element relatively to another. The tags that consider this attribute are :

  • <rlbox>

  • <rlimage>

  • <p>

The attribute values should be of the form :

snapto="other_element_id|pos_this_element|pos_other_element"

If multiple elements have the same id, the last one declared in the xml file will be used as the element we will position relatively to.

Each corner of an element's rectangle boundary is NW, NE, SE, SW and C (center). Any combination of these 5 snap positions can be made to suit your needs.

    NW            NE
    +-------------+
    |      C      |
    +-------------+
    SW            SE

This makes it possible to create complex tables quite simply by 'snapping' various elements together. This also has the advantage that you can then tweak the appearance of your document by simply modifying one anchor element's posx and posy attributes instead of having to tweak posx, posy for ALL the elements in your xml file.

Here is a simple example, of 3 elements snapped together:

                    +---------------+
                    |       top     |
        +-----------+---------------+
        |   mid     |
        |           |
        +-----------+-------+
                    |       | 
                    |  low  |
                    |       |
                    |       |
                    +-------+   

In the XML file, this could be done as such:

<rlxml>
...
<!-- position the 'top' element (arbitrarily) -->
<p id="top" posx="300" posy="150" width="100" height="20" ...> top </p>

<!-- we want to have 'mid' NE corner snapped to 'top' SW corner -->
<p id="mid" width="75" height="40" snapto="top|NE|SW" ... > mid </p>

<!-- we want to have 'low' NW corner snapped to 'mid' SE corner -->    
<p id="low" width="50" height="80" snapto="mid|NW|SE" ... > mid </p>
...
</rlxml>

When using this attribute, make sure:

  • The id of the element you wish to position relatively to exists

  • Do not specify 'posx', 'posy' since they will conflict with the 'snapto' attribute

Clone this wiki locally