Write Data to a Table in a PDF Document in ASP.NET

A sample ASP.NET script using PDFBuilderASP

This example script demonstrates how to retrieve data from a database table in an ASP.NET script, then write that data to a table in a PDF document. The demo uses an MS Access database but could easily be modified to use another type of database such as SQL. The sizing of the rows and columns of the table will be adjusted as needed to fit the data.

To run this example script, download the trial version of PDFBuilderASP and a copy of the ASP.NET script used for this demo from the links below.

Trial version of PDFBuilderASP. (504kB)

Download the example ASP.NET script. (9kB)

Running the example script

To run the example, extract the files 'pbaspnetdemodatabase1.aspx' and 'Data.mdb' from the downloaded zip file and copy these files to a web server directory. A True Type font file must also be copied to this directory and the script edited to refer to both this file and the database file Data.mdb. Open the ASP.NET page in a browser and a PDF file will be created and displayed.

The data table contains customer names and transaction amounts for sales of an imaginary company. The script first steps through the records in the database table and writes each customer name and each amount to separate text blocks. By keeping them separate, each can later be positioned individually where it is needed in the table.

While stepping through the records, the width of each text block is found using the TextBlockWidth property and the maximum width of each of the two columns is stored in variables MaxW1 and MaxW2. This information is then used to lay out the table. The text block heights could also be retrieved using the TextBlockHeight property but as all blocks will be the same size, this has not been done in order to keep the example as simple as possible.

After creating the text blocks, a graphic is generated by drawing vertical and horizontal lines. The position and length of the lines is calculated based on the number of records in the table and the maximum field widths found when writing the text. Finally the text blocks are written in the appropriate positions in the table.

More detailed instructions for running this script are available in the readme.txt file in the download.