6

how can I can export the data shown on a DBgrid to a pdf file?

Graham
  • 14,885
  • 4
  • 36
  • 42

5 Answers5

7

Well, data being shown in a DBGrid is provided by the dataset attached to that dbgrid, so exporting data in DBGrid to PDF means exporting data in your dataset to PDF.

The easiest option is to use a Reporting tool. There are many different reporting tools available for Delphi e.g. Rave Report, FastReport, Report Builder, QuickReport, and so on.

Such tools let you to design a printing report from your data, and let you to either print the report or export it to formats like HTML, DOC, PDF, and so on. Rave Report is shipped with Delphi and you can use it for free. I personally like FastReport and use it in my applications.

Another option is that, if you have a virtual PDF printer installed on the target system, you can select it as your printer and use Delphi's TPrinter class to write directly on printer canvas, and your virtual PDF printer will make a PDF file for you rather than printing your data on paper.

A third option is to use third-party components which are specifically built for PDF manipulation and let you to create or edit PDF files in your application.

Regards

vcldeveloper
  • 7,399
  • 2
  • 33
  • 39
3

The Scalabium Export suite for Delphi (including 2009) supports many export formats, including PDF and other office formats with and without OLE. The export components can be used with TDBGrid and TDataSet descendants.

It can be used non-visual but also offers configurable export wizards. We successfully use it in a application suite migration from Delphi 7 to 2009.

mjn
  • 36,362
  • 28
  • 176
  • 378
2

try EMS Advanced Data Export VCL

http://sqlmanager.net/en/products/tools/advancedexport

  • Data (Datasets) export into 17 most popular formats: MS Access, MS Excel, MS Word, Open XML Format, Open Document Format (ODF), RTF, HTML, XML, PDF, TXT, DBF, CSV, SYLK, DIF, LaTeX, SQL and Windows Clipboard
  • Borland Delphi 5-7, 2005, 2006, CodeGear Delphi 2007, 2009 and Borland C++ Builder 5-6, CodeGear C++ Builder 2007, 2009 support
  • Export of Unicode data. Manually preset text encoding for exported data (UTF-8, UTF-16/UCS-2, UTF-32/UCS-4, Latin1, Latin2, Latin5, Latin7 and more)
  • Saving data for future viewing, modification, printing or web publication
  • Easy-to-use wizard allows your end-users to export data quickly
  • Powerful export options for each data format
  • 100% native Delphi code
  • No additional libraries or software required to operate
  • Detailed help system and demo application
  • Powerful components and properties editors
  • Setting the user formats for each field separately
  • Multilanguage support
mghie
  • 32,028
  • 6
  • 87
  • 129
RRUZ
  • 134,889
  • 20
  • 356
  • 483
1

You may iterate through the data yourself and use excellent exporting VCL eDocEngine from Gnostice. It also connects to reporting tools, and other components.

Mihaela
  • 2,482
  • 3
  • 21
  • 27
0
Public Sub ExportDataTableToPDF(ByVal dtImport As DataTable)
    Dim strQuery As String = "select er_num,er_shortd,er_longd,er_severity from mv_error"
    Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
    Try

        '====================================================================
        'Dim str As String = DateTime.Now.ToString("yyyyMMddHHmmssff")
        'Dim sWebSettingPath As String = ConfigurationSettings.AppSettings("ExptLoctPath")
        'Dim str1 As String = "E:\ExportPdf_File"
        'Dim WorkingFile As String
        'Dim s As String
        'If (Directory.Exists(sWebSettingPath)) Then
        '    s = "already exists"
        '    WorkingFile = Path.Combine(sWebSettingPath, "" & str & " DataTestData.pdf")
        'Else
        '    Dim ss As String = "C:\ReportPDF"
        '    WorkingFile = Path.Combine(str1, "" & str & " DataTestData.pdf")
        '    Directory.CreateDirectory(ss)
        'End If


        '====================================================================

        'Folder Exists in Particular folder or not cheak ifnot create a folder
        Dim strDateTime As String = DateTime.Now.ToString("yyyyMMddHHmmssff")
        Dim sWebSettingPath As String = ConfigurationSettings.AppSettings("ExptLoctPath")
        Dim str1 As String = "E:\ExportPdf_File"
        Dim WorkingFile As String
        Dim s As String
        If (Directory.Exists(sWebSettingPath)) Then
            s = "already exists"
            WorkingFile = Path.Combine(sWebSettingPath, "" & strDateTime & "_DataTestData.pdf")
        Else
            Dim sWebStingNotPath As String = "C:\ReportPDFTest"
            Directory.CreateDirectory(sWebStingNotPath)
            WorkingFile = Path.Combine(sWebStingNotPath, "" & strDateTime & " DataTestData.pdf")

        End If
        '====================================================================

        Dim fs As New FileStream(WorkingFile, FileMode.Create, FileAccess.Write, FileShare.None)


        'Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
        PdfWriter.GetInstance(doc, fs)
        ' Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("C:\Documents and Settings\lessly.l\Desktop\iTextSharp\Test11.pdf", FileMode.Create))
        doc.Open()
        'Open Document to write
        Dim font8 As Font = FontFactory.GetFont("ARIAL", 7)

        'Write some content
        Dim paragraph As New Paragraph("Team :: CataPult")

        Dim dt As DataTable = dtImport

        If dt IsNot Nothing Then
            'Craete instance of the pdf table and set the number of column in that table
            Dim PdfTable As New PdfPTable(dt.Columns.Count)


            Dim PdfPCell As PdfPCell = Nothing
            Dim pdfrow As PdfPRow = Nothing


            For column As Integer = 0 To dt.Columns.Count - 1

                PdfTable.HeaderRows = dt.Columns.Count


                PdfPCell = New PdfPCell(New Phrase(New Chunk(dt.Columns(column).Caption.ToString().ToUpper, New Font(Font.HELVETICA, 8.0F, Font.BOLD, Color.WHITE))))
                PdfPCell.BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#66CCFF"))

                PdfTable.AddCell(PdfPCell)

            Next

            'Each Row Values added

            For rows As Integer = 0 To dt.Rows.Count - 1
                For column As Integer = 0 To dt.Columns.Count - 1
                    PdfTable.HeaderRows = dt.Columns.Count
                    PdfPCell = New PdfPCell(New Phrase(New Chunk(dt.Rows(rows)(column).ToString(), font8)))
                    PdfTable.AddCell(PdfPCell)
                Next
            Next

            PdfTable.SpacingBefore = 15.0F
            ' Give some space after the text or it may overlap the table
            doc.Add(paragraph)
            ' add paragraph to the document
            ' add pdf table to the document
            doc.Add(PdfTable)

        End If
    Catch docEx As DocumentException
        'handle pdf document exception if any
    Catch ioEx As IOException
        ' handle IO exception
    Catch ex As Exception
        ' ahndle other exception if occurs
    Finally
        'Close document and writer

        doc.Close()
    End Try
End Sub
ronalchn
  • 12,225
  • 10
  • 51
  • 61
  • Please add a slim explanation of *what* your code does, *why* you'd recommend it, or *how* you came to the conclusion. =) Also, it's full of commented out code. Is that *really* necessary? – J. Steen Oct 18 '12 at 11:36
  • OP asked for a solution in Delphi, not Visual Basic; although I suppose he _could_ manually convert this (if so, please post here, thanks) – Mawg says reinstate Monica Mar 06 '13 at 01:49