Xojo 2022r3 included the ability to easily add tables to PDF documents created with Xojo. Now, starting with Xojo 2022r4, this feature has been improved so you can to merge the cells in table’s row! Additionally, you can now set a page’s upper and lower margins and get informed when the table has finished drawing itself. Continue reading to learn how to do all these things!
In order to merge cells in a table’s row, the PDFTableDataSource class interface includes a new method: MergeCellsForRow(row As Integer, ByRef CellsToMerge() As CellRange) As Boolean. This method will be called for every table row to be drawn so you can provide the range of cells to merge as a CellRange instance.
For example, the following statement will merge all the cells for a row in a five column table:
CellsToMerge.Add New CellRange(0,4)
Where CellsToMerge is an array parameter received by reference. So, if we apply the previous sentence to every even row in the table:
If row Mod 2 = 0 Then
CellsToMerge.Add New CellRange(0,4)
Return True
End If
We will get the following PDF as result (based on the PDF table example project):
Top and Bottom Margins
In addition, the PDFTable class now has two new properties: TopMargin As Double and BottomMargin As Double. As you can guess these will let you set the top and bottom margins for the table when it is added to the PDF document pages; specially interesting when the table is going to spread several pages because you won’t need to do any maths in order to know when a new page needs to be added to the document, PDFDocument will take care of it based on the provided values plus the height value of every row to be drawn!
Hey mate, the table has been drawn!
Lastly, we also added the Completed(x As Double, y As Double) method to the PDFTableDataSource class Interface, so your code can know the right spot at which the table has finished drawing itself. This is specially interesting when the table spreads several pages, so you can get the last X and Y coordinates of the table drawing in order you can continue adding new content to the PDF page right after it!
For example, the following code in the Completed method of our example will render the “Drawing Table Completed!” text in the last added page, just below the drawing of the table, adding 20 points to the received Y value and using the same X value so the left margin for the text is the same that the one used to draw the table:
In Summary
These additions give you more control when drawing the tables to your PDFDocuments: setting both the upper and lower margins for the page, getting informed when the table has finished drawing itself, and the ability to merge cells independently on every row of the table!
Javier Menendez is an engineer at Xojo and has been using Xojo since 1998. He lives in Castellón, Spain and hosts regular Xojo hangouts en español. Ask Javier questions on Twitter at @XojoES or on the Xojo Forum.