Data Export PDF in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

This sample demonstrates how to export data table into pdf file.

private void btnPDF_Click(object sender, EventArgs e)
{
    System.Data.OleDb.OleDbConnection oleDbConnection1
         = new System.Data.OleDb.OleDbConnection();
    oleDbConnection1.ConnectionString
        = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb";

    System.Data.OleDb.OleDbCommand oleDbCommand1
        = new System.Data.OleDb.OleDbCommand();
    oleDbCommand1.CommandText = "select * from parts";
    oleDbCommand1.Connection = oleDbConnection1;

    Spire.DataExport.PDF.PDFExport pdfExport1 = new Spire.DataExport.PDF.PDFExport();
    pdfExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
    pdfExport1.DataFormats.CultureName = "zh-CN";
    pdfExport1.DataFormats.Currency = "c";
    pdfExport1.DataFormats.DateTime = "yyyy-M-d H:mm";
    pdfExport1.DataFormats.Float = "g";
    pdfExport1.DataFormats.Integer = "g";
    pdfExport1.DataFormats.Time = "H:mm";
    pdfExport1.FileName = "sample.pdf";
    pdfExport1.PDFOptions.DataFont.CustomFont = new System.Drawing.Font("Arial", 10F);
    pdfExport1.PDFOptions.FooterFont.CustomFont = new System.Drawing.Font("Arial", 10F);
    pdfExport1.PDFOptions.HeaderFont.CustomFont = new System.Drawing.Font("Arial", 10F);
    pdfExport1.PDFOptions.PageOptions.Format = Spire.DataExport.PDF.PageFormat.User;
    pdfExport1.PDFOptions.PageOptions.Height = 11.67;
    pdfExport1.PDFOptions.PageOptions.MarginBottom = 0.78;
    pdfExport1.PDFOptions.PageOptions.MarginLeft = 1.17;
    pdfExport1.PDFOptions.PageOptions.MarginRight = 0.57;
    pdfExport1.PDFOptions.PageOptions.MarginTop = 0.78;
    pdfExport1.PDFOptions.PageOptions.Width = 10.25;
    pdfExport1.PDFOptions.TitleFont.CustomFont = new System.Drawing.Font("Arial", 10F);
    pdfExport1.SQLCommand = oleDbCommand1;

    oleDbConnection1.Open();
    pdfExport1.SaveToFile();
}

Private Sub btnPDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPDF.Click
    Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection()
    oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"

    Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand()
    oleDbCommand1.CommandText = "select * from parts"
    oleDbCommand1.Connection = oleDbConnection1

    Dim pdfExport1 As New Spire.DataExport.PDF.PDFExport()
    pdfExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView
    pdfExport1.DataFormats.CultureName = "zh-CN"
    pdfExport1.DataFormats.Currency = "c"
    pdfExport1.DataFormats.DateTime = "yyyy-M-d H:mm"
    pdfExport1.DataFormats.Float = "g"
    pdfExport1.DataFormats.[Integer] = "g"
    pdfExport1.DataFormats.Time = "H:mm"
    pdfExport1.FileName = "sample.pdf"
    pdfExport1.PDFOptions.DataFont.CustomFont = New System.Drawing.Font("Arial", 10.0F)
    pdfExport1.PDFOptions.FooterFont.CustomFont = New System.Drawing.Font("Arial", 10.0F)
    pdfExport1.PDFOptions.HeaderFont.CustomFont = New System.Drawing.Font("Arial", 10.0F)
    pdfExport1.PDFOptions.PageOptions.Format = Spire.DataExport.PDF.PageFormat.User
    pdfExport1.PDFOptions.PageOptions.Height = 11.67
    pdfExport1.PDFOptions.PageOptions.MarginBottom = 0.78
    pdfExport1.PDFOptions.PageOptions.MarginLeft = 1.17
    pdfExport1.PDFOptions.PageOptions.MarginRight = 0.57
    pdfExport1.PDFOptions.PageOptions.MarginTop = 0.78
    pdfExport1.PDFOptions.PageOptions.Width = 10.25
    pdfExport1.PDFOptions.TitleFont.CustomFont = New System.Drawing.Font("Arial", 10.0F)
    pdfExport1.SQLCommand = oleDbCommand1

    oleDbConnection1.Open()
    pdfExport1.SaveToFile()
End Sub