OPTEN, das einzige Umbraco-zertifizierte Unternehmen der Schweiz

Getting the co-ordinates of a cell using Migradoc

I received a question relating to my earlier blog post about positioning text vertically in a Migradoc table cell. The question was for more detail on finding the co-ordinates of the cell inside the page. So I thought this would make a good topic for a follow up blog.

As I mentioned in my earlier blog, I had to save the locations of the cell on the page in a special class which I created for this purpose named CellLocation. The idea is to then add this class as a tag to the cell itself. I also use it as a key I know that a cell with a CellLocation tag is a cell where I want the text to be vertical. Here is the CellLocation class:

public Unit X;
public Unit Y;
public Unit Width;
public string Text;

The question related to how to find the co-ordinates of the cell in the page. Unfortunately I was not able to find an easy way of doing this with Migradoc. The way that I solved this problem was to calculate for each cell the distance from the top of the page (which I called Y) and the distance from the left of the page (which I called X). 

Calculating Y

In my pdf it is only the cells in the first row which had to be vertical. On the pdf, there was a top margin, then a company logo, then some text beneath the logo, then the table. So to calculate Y, I added together these 3 values (the top margin, the logo height and the logo text height).

Calculating X

The next job was to calculate the X value (the distance from the left of the page). To do this I took the width of the left margin and added to this the widths of all the columns. In my case there were several columns of varying widths before the contract columns which needed vertical text. So added this pre-column widths value, to the amount of contract columns multiplied by the column width.

The Code

The whole method used to calculate the position of each cell, and create the CellLocation objects and add this object as a tag to the cell is shown below;

private void AddContractCells(List<Contract> contracts)
     int logoTextHeight = 10;
     Cell contractCell;
     for (int i = 0; i < contracts.Count; i++)
          contractCell = _row.Cells[i + _columnsBeforeContracts];
          CellLocation cellLocation = new CellLocation()
               Text = contracts[i].ContractNameDefault ?? contracts[i].ContractNameDictionaryLookup ?? string.Empty,
               Y = _section.PageSetup.TopMargin + _logo.Height + logoTextHeight,
               X = _section.PageSetup.LeftMargin + _preContractColumnWidths + (i * _columnSectorUnit),
               Width = _columnSectorUnit
     contractCell.Tag = cellLocation;


It is annoying that there is not an easier way to get the co-ordinates of a cell using Migradoc. I looked for another solution, but in the end settled on this option of calculating the co-ordinates for each cell and it worked for me. I hope this clears up questions arising from my earlier blog post and is helpful for anyone working with Migradoc tables.


0 Kommentar(e):