AdvGridPDFIO in multiple grids

I have several TAdvStringGrid in different forms in my application and I want to print them all in the same PDF document using AdvGridPDFIO.
I have tried to save them to a common stream and then store the stream to disk without success.
How can this be solved?

TAdvGridPDFIO exports one grid to a PDF. At this moment, there is no built-in support to combine content of multiple grids into a single PDF. The solution is:

  • create a temporary grid,
  • load the data of the multiple grids together into this temporary grid
  • export this temporary grid to PDF
  • destroy the temporary grid

Thank you for fast support:)

Yes we can do as you suggest, but if we use FlexCel instead can we then get a more flexible output?

Today we use FastReport (with 3-4 pages with different grids) but we will try to reproduce the current layout with TMS components without to much coding. The nice thing with AdvGridPDFIO is that with one line of code we get a nice wysiwyg output.

With TMS Flexcel Grid Bridge

you can get grid data into FlexCel and from FlexCel you can also export to XLS, XLSX, PDF, HTML.
Once the data is in FlexCel, it offers a very rich API to generate reports in several ways. So, indeed, this is also a possible solution.

Is there some example code on how to produce almost the same PDF output as TAdvGridPDFIO does?

The simplest example to export of a grid would be Simple Export (VCL) | TMS VCL Grid Excel bridge documentation
You can also get this example directly when you install the bridges: most of the code is used to setup the grid anyway, what you need to do is to call


But, in your case, you want to mix many grids in a single file, you might need to do more things. First of all, you need to export the file to Excel first (you can do this in memory, you don't need to create a physical xlsx file). This is actually the same as ExportPdf above does, it is just that you don't see it.

To export to xlsx in memory, you need to call


where xls is a TXlsFile object that will keep the xlsx file in memory. You have an example of that at: Templates (VCL) | TMS VCL Grid Excel bridge documentation

You can export many grids one after the other in the same sheet, or export one grid per sheet, or any combination, as you prefer. If you export all grids to the same sheet, they will just print one after the other in the pdf, and the page breaks might happen in the middle of a grid. If you export one grid to a different sheet, that grid will appear as a new page inside the same pdf document. In addition, each sheet will add a bookmark to the pdf so you can easily find the grid (the bookmark is the sheet name):

finally, once you have the XlsFile as you want it (as said, you don't need to save it, but you can if you want to see what is getting exported to pdf), you can use FlexCelPdfExport to export the XlsFile object to pdf. FlexCelPdfExport is a FlexCel class, and you can find examples on how to use it in flexcel docs, like Exporting Excel files to PDF (Delphi) | FlexCel Studio for VCL and FireMonkey documentation

But it is actually simple to use, just call FlexCelPdfExport.Export. There is a lot of customization if you want (from setting the pdf properties to say exporting to pdf/A), but the simple use is just FlexCelPdfExport.Export.

I think this is mostly it, but let me know any additional doubts that might arise. In short, what I see as pros of using the bridges for export are:

  1. You get a lot of control on the file you create. You can control where you put the grids, some might be in the same sheet, some in different sheets, etc. You might also add some extra text or a logo to the export as say a caption before the sheet. You can even use an empty Excel "template" to put the grids inside, so you can customize that file with the branding you want.
  2. You can do a lot of stuff with the pdf itself. You can for example create pdf/a files or sign then (see Sign your PDF files | FlexCel Studio for VCL and FireMonkey documentation ), you can control the fonts, change the PDF properties (Author, keywords, etc), change the accessibility by creating tagged files (see FlexCel PDF Exporting Guide | FlexCel Studio for VCL and FireMonkey documentation ), etc.
  3. You also have control on how to print. For example, using "intelligent page breaks" you can make a new grid appear in a new page only if more than 80% of the page is used, or print after the previous grid otherwise. You can fit the grid to be one page in width, so it doesn't overflow to the next one. For more info about how to customize the printing, see FlexCel API Developer Guide | FlexCel Studio for VCL and FireMonkey documentation

As cons the main issue with using the bridges is that you have 2 conversions going on.

Instead of converting the grid directly to pdf as in with AdvGridPDFIO, you are converting the file to Excel, and then that Excel to pdf. This extra step allows most of the functionality listed in the "pros" (because you can edit that xlsx file and customize it as you want), but it also means that the grid should be converted to Excel, and that is not a 100% perfect process, because the grid and Excel have different capabilities. When converting to PDF directly there is no problem because PDF is just an "image" format, so everything can be converted. When converting to Excel it isn't that straightforward, and the most challenging part is the converting of format strings (Excel and AdvStringGrid use different format strings to format the cells). You'll likely need to look at On-demand formatting (VCL) | TMS VCL Grid Excel bridge documentation too. But well, as an added advantange of using xlsx as intermediate format, you can also send the xlsx files to your customers if they want them. And you can convert those files to html too, with multiple grids per sheet and all the customizations too.