Forum How do I...?

How do I read the width of the left page margin for the current page?

zdenko
Hi,

I have a document that has alternating page margins and need to scale tables that overflows from the container and the page.

I'm using the two-step process to read the layout in the oncomplete phase and apply the css with generated selectors in the second pass.

I'm using the JavaScript API getPrinceBoxes() to get the X coordinate and W of the table in order to calculate the scaling factor. In all cases, the table is nested inside a 'root-level' div and i can use the box tracking on the container div to find the X and W of the page's content area and extrapolate the available width in which the table must fit by using this formula: (container.w - (table.x - container.x)) / table.w

Since the container div is horizontally contained inside the page's content area, container.w gives me the full content area's width and container.x gives me the x coord of there this area starts. Subtracting the table.x from container.x gives me the left offset of the table from the content area which I can subtract the the full width of the container to give me the available width for the table.

The problem is when the container div starts on one page and finishes on another page and there are multiple tables nested in the container div, each requiring different scaling.

If container div starts at page 0, then all the even pages will scale the tables correctly but all odd pages will scale slightly off because the X coord of the container is not directly proportional to X coord of the table on the odd page with different margin for @page:left / right.

In order to avoid this, is there a way to directly read the x-coordinate of the current page's container area?
The width of the current page's left page would also be helpful.

EDIT: Can be ignored. Solution found.

For those interested, the user guide mentions the PDF.pages that given the page number you can find the page's container area.

Extract:
function printbox(str,box) {
  console.log("");
  for (var i in box) {
    console.log(str+i+": "+box[i]);
  }
  for (var i=0; i<box.children.length; i++) {
    printbox(str+"  ",box.children[i]);
  }
}

Prince.addEventListener("complete", function() {
  var pages = PDF.pages;
  for (var i = 0; i<pages.length; ++i)
    {
      console.log("PAGE "+(i+1)+" HAS THESE BOXES");
      printbox("  ",pages[i]);
    }
}, false);