• Danish Yasin

Creating Tables Using Pure Go

Updated: Aug 24



While working with PDFs, you may have come across the challenge of creating a table and placing it at the right spot on the page. Like many others, you must have spent hours fixating on the borders, the shape and the position of the table and would have kept trying to fix it.


Anyone who has ever had to work with PDF reports can relate to that, which is why UniPDF spent a considerable amount of effort on perfecting the creator package. It allows you to create tables, edit them and place them elegantly on pages, all through the power of golang.


UniPDF is powered by code written in pure go, which makes it robust, easy to deploy and free from bugs that can crash your application during runtime. Since it’s written in go, it has a steep learning curve and you can get started with creating interesting documents, in a matter of minutes.


Recently, we’ve discussed how you can use the UniDoc library to create letters, work with

spreadsheets, digitally sign your PDF documents and do a whole lot more. Now lets see how you can use the creator package to create tables in your PDF documents.


UniDoc hosts a playground, where you can test out the power of UniPDF and UniOffice without having to configure the library on your system. This allows you to test out concepts, create code super fast and share your creations, without having to deal with any of the backend. Learn more about UniDoc playground through this introductory tutorial.



Create a Simple Table


All examples shown in this article are built using the UniDoc playground. You can easily open each individual example, play around with it, save it and share with your colleagues.


The following playground example highlights how you can create a simple table using UniPDF library. The example is inspired from this GitHub example, you can check it out here and test out other examples in the repository as well.





The flow of the functions in the above example is simply:


main() -> basicUsage() -> contentAlignH()


The main() function calls the basicUsage() function, which calls the contentAlignH function. In the basicUsage(), you are simply creating a new chapter in your PDF report and the contentAlignH() function creates a subchapter.


Partitioning the program into separate functions like above helps in extending the program when needed. Each new subchapter can be created in a separate function that can be called from the basicUsage() method.


Creating a table is as simple as table := c.NewTable(3), where the parameter denotes the number of columns. You can see this function call at line number 76 in the embedded playground example above.


The magic happens in the inline function that is created at line 79, which is the drawCell() function. The function accepts the text, font family and the alignment of the text as parameters and creates a new styled paragraph which is inserted into each corresponding cell.


drawCell() is called afterwards to create the table header and programmatically create a list of five products by calling a loop.



Style Your Table


UniPDF is focused on making your life easier while you’re working on creating tables in your PDF reports. This is why you can style your table in a number of different ways, according to your requirements.


The following playground example highlights how you can change the type of borders and update the background of cells.






Similar to the previous example, we have one main stylingContent() method that creates a chapter. Further, we have two separate methods that deal with changing borders and updating the background design respectively. Each method creates a separate subchapter.


There is a similar drawCell() method that creates a cell with custom configuration, which is repeatedly called to create cells with customized borders.


A similar drawCell() method also exists in the contentBackground() method that updates the background design of the cell.


The output of all the code is saved in a pdf named styled_table.pdf, which you can view in the playground.



Span a Column Across Multiple Columns


A common challenge that people working with tables in PDF documents face is how to create a column that spans multiple columns. The challenge may seem trivial at first, but becomes quite complicated when trying to actually implement the concept.


The following playground example highlights how you can use the UniPDF library to tackle this issue and create columns that span the length of multiple columns.





Here, you have a single columnSpan() method that has an inline function of drawCell() that accepts a custom number of column spans. This allows you to create cells that can span multiple columns.

However, it should be taken care of here that the columns do not exceed the total number of columns of the table, which in this case were five.


Having a cell span multiple columns is pretty simple, you just call the method table.MultiColCell(colspan), where colspan is an integer that denotes the number of columns to span a cell.


Create Sub-Tables within Tables


In real life, there is hardly any data that can be completely compressed into a two-dimensional table. You usually require more dimensions to amply describe the complex data.


This is where creating sub-tables within a table comes into play. This functionality allows you to increase the complexity of your table to easily integrate data that is too complex for a simple table. The following playground examples deal with the problem and uses UniPDF to create sub-tables within a table.




In the playground example, you have the user defined subtables() method that creates the chapter of subtables in the PDF document.


The generateSubtable() method has been defined, which accepts rows, columns, index of table and a boolean of right border as parameters. The boolean defines whether the sub-table should have a right border or not.


At the end, you use the inbuilt method of table.AddSubtable() that accepts the rows and columns and a table in its parameters. In the example, there is code for five sub-tables, however, sub-table five has been commented out.


You can see the void it leaves in the document visible in the playground, uncomment it and see how it is placed adjacent to table three.



Conclusion


Working with tables can be fun, or it can be a challenge. It all depends on which tools you are using to make the tables. UniPDF provides a powerful interface for you to create tables in PDF documents using the power of pure golang. It is quite simple, as noticeable from the playground examples, to create tables using UniPDF.


Feel free to create new examples using the playground, for starters, uncomment the code for sub-table five and run it to see changes in real time.


Since UniPDF is constantly being updated to add new features that our customers request, you should subscribe to our mailing list to keep up to date with the latest news.

© All rights reserved to unidoc.io

linkedin-icon.png