User Tools

Site Tools


k25:k25.1.13:start

25.1.13 Project 3 - Database report

Drawing text should primarily be used for labelling selected details of a drawing. An exception could be texts with a clear structure in the form of rows and columns - as is typical for tables, for example. Project 3 therefore presents a simple database report that was drawn using methods of the Cairo class.

In order to be able to test the project without any problems, an SQLite3 database with the database table 'contacts' in the project folder is provided as a database.

B1

Figure 25.1.13.1: Data browser for the DB data

To realise your own projects, you will also find brief instructions on how to create an SQLite3 database in project 3 'Database report'. In this you will also find the necessary SQL instructions.

25.1.13.1 Results

The DB report as a PDF file has a different layout for the first page - compared to all subsequent pages - but the same design for the data rows and the footer. There is only a header on the first page, in which the names of the database and the database table as well as the print date are specified:

B2

Figure 25.1.13.1.1: DB report - 1st page

All subsequent pages look like this:

B3

Figure 25.1.13.1.2: DB report - page 2 to k

Only a subset of all data is displayed in this DB report. The selection made is sufficient for an address list. However, if you want to display all data, you must change the SQL statements for the DB query and certainly also change the PDF layout from portrait to landscape format.

The page number is displayed in the footer under a closing line:

B4

Figure 25.1.13.1.3: Footer

The dotted lines shown in all the images above were only used as control lines during testing and can be omitted in the finished report.

Drawing the DB report with methods of the Cairo class is based on the following considerations:

  • The DB report is drawn on a PDF drawing surface (CairoPDFSurface).
  • The number of fields and the fields in a data table row are fixed.
  • Each page has a table header with the names of the selected fields (columns). The spacing between the fields is based on tried and tested values.
  • Almost all vertical spacing and the text sizes in the DB report are defined using constants. This makes it possible to calculate how many pages the report has and how many data table rows can be drawn on the first page and the following pages after drawing the first data table row. Knowing these values enables an exact page layout.
  • All dimensions are given in millimetres or converted. Drawing is done in Cairo Space coordinates (points), whereby the dimensions are converted from millimetres back into points. This procedure has already proven itself in some Cairo projects.
  • The finished DB report as a PDF file is displayed in the system PDF reader and can then be printed out immediately.

25.1.13.1 Source code snippet

In the ExportPDF() procedure, the DB report is drawn using further procedures and functions:

Private Sub ExportPDF()
 
  Dim PDFSurface As CairoPdfSurface
  Dim sPfadPDFDatei, sMessage As String
  Dim iDataSet As Integer
 
' Initialisation
  iCWidth = PDF_WIDTH - MARGIN_LEFT - MARGIN_RIGHT   '-- Content-Width (Millimeter)
  iCHeight = PDF_HEIGHT - MARGIN_TOP - MARGIN_BOTTOM '-- Content-Height (Millimeter)
  iCurrentPage = 1 '-- Page number of the current page
  iCurrentTableRow = 1 '-- Number of table rows on the current page
  fCurrentY = 0 '-- y-coordinate on the current page
  sPfadPDFDatei = User.Home &/ "kontakte.pdf" 
  PDFSurface = New CairoPdfSurface(sPfadPDFDatei, PDF_WIDTH, PDF_HEIGHT) ' DIN A4 - Hochformat
 
  Cairo.Begin(PDFSurface)
  ' Shift of the coordinate origin - >> Left = MARGIN_LEFT, Top = MARGIN_TOP
    Cairo.Matrix = Cairo.Matrix.Translate(MMToPoints(MARGIN_LEFT), MMToPoints(MARGIN_TOP))
    Cairo.Matrix = Cairo.Matrix.Scale(1, 1) '-- Zoom-Factor = 1        
    Cairo.Font.Name = FONT_NAME    
 
    GetDBData() '-- Providing the DB data to be displayed (DB result)
 
    If resDBData.Count = 0 Then
       sMessage = "<font color='red'><center>The DB selection set is empty.</font>"
       sMessage &= "<hr>"
       sMessage &= "A DB report cannot be generated!</center>"
       Message.Warning(sMessage)
       Return
    Endif
 
    DrawHeader() 
    DrawDatabaseInformation() 
    DrawTableHeader()
    resDBData.MoveTo(0) '-- Set data record pointer to the first data record
    DrawTableRow() '-- Show data record
    DrawFooter()
    DrawBorder() '-- For testing purposes only
    For iDataSet = 1 To resDBData.Max '-- Draw all data records (text) of the selection set
      If iCurrentPage = 1 Then
         If iCurrentTableRow = iPage1RowMax Then
            iCurrentPage = 2
            iCurrentTableRow = 0
            fCurrentY = 0
            Cairo.ShowPage()
              DrawBorder() '-- Only for controls in testing
            DrawTableHeader()
            DrawFooter()
         Endif
         resDBData.MoveTo(iDataSet) 
         DrawTableRow()  
         Inc iCurrentTableRow 
      Else
         If iCurrentTableRow = iPage2RowMax Then             
            Inc iCurrentPage
            iCurrentTableRow = 0
            fCurrentY = 0
            Cairo.ShowPage()
              DrawBorder() '-- Only for controls in testing
            DrawTableHeader()
            DrawFooter()
         Endif
         resDBData.MoveTo(iDataSet) 
         DrawTableRow()  
         Inc iCurrentTableRow
      Endif
    Next
  Cairo.End()
 
  Desktop.Open(sPfadPDFDatei) '-- Show DB-Report
 
End

The complete, extensive source code for project 3 can be found in the download area in a project archive.

Download

Chapter & Projects

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k25/k25.1.13/start.txt · Last modified: 20.02.2024 by emma

Page Tools