- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Hướng dẫn tạo file PDF từ Database lập trình csharp
Bài viết hôm nay, mỉnh sẽ hướng dẫn các bạn cách truy vấn dữ liệu từ Database MSSQL SERVER và xuất dữ liệu từ database ra file PDF C#.
Để xuất được file PDF, trong bài viết này, mình sử dụng thư viện PDFSharp.
Các bạn có thể download thư viện PDFSharp ở link bên dưới:

Sau khi các bạn download về các bạn tiến hành import thư viên PDFSharp vào project của mình:

Và dưới đây là kết quả của ứng dụng:

Các bạn có thể tham khảo source code ở bên dưới:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Data.SqlClient;
using System.Data;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string connetionString = null;
                SqlConnection connection ;
                SqlCommand command ;
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                int i = 0;
                string sql = null;
                int yPoint = 0;
                string pubname = null;
                string city = null;
                string state = null;
                connetionString = "Data Source=YourServerName;Initial Catalog=pubs;User ID=sa;Password=zen412";
                sql = "select pub_name,city,country from publishers";
                connection = new SqlConnection(connetionString);
                connection.Open();
                command = new SqlCommand(sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                connection.Close();
                PdfDocument pdf = new PdfDocument();
                pdf.Info.Title = "Database to PDF";
                PdfPage pdfPage = pdf.AddPage();
                XGraphics graph = XGraphics.FromPdfPage(pdfPage);
                XFont font = new XFont("Verdana", 20, XFontStyle.Regular );
                yPoint = yPoint + 100;
                for (i = 0; i < = ds.Tables[0].Rows.Count - 1; i++)
                {
                    pubname = ds.Tables[0].Rows[i].ItemArray[0].ToString ();
                    city = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                    state = ds.Tables[0].Rows[i].ItemArray[2].ToString();
                        
                    graph.DrawString(pubname, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
                    graph.DrawString(city, font, XBrushes.Black, new XRect(280, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
                    graph.DrawString(state, font, XBrushes.Black, new XRect(420, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
                    yPoint = yPoint + 40;
                }
                string pdfFilename = "dbtopdf.pdf";
                pdf.Save(pdfFilename);
                Process.Start(pdfFilename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}HAVE FUN :)

![[C#] Hướng dẫn tạo file PDF từ Database lập trình csharp](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Import dữ liệu file Excel (xls, xlsx, csv) vào Dataset hoặc Datatable](https://laptrinhvb.net/uploads/source/image_baiviet/95dae3f410d40a147787f979ba5cc059.png)
![[C#] Hướng dẫn thêm text vào hình ảnh icon winform](https://laptrinhvb.net/uploads/source/vbnet/icon_progress.gif)
![[C#] Hướng dẫn cách viết ứng dụng lập lịch chạy tự động sử dụng thư viện Quartz.NET](https://laptrinhvb.net/uploads/source/image_baiviet/0bec151c11c53dd094691f07905ae002.jpg)
![[C#] Hướng dẫn get dữ liệu từ Table HTML website to DataTable](https://laptrinhvb.net/uploads/source/image_baiviet/5c143f5ad97aca7020a307bc7c253364.jpg)
![[C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp](https://laptrinhvb.net/uploads/source/new_image_baiviet/reminder_email.png)
![[C#] Chia sẽ source code Matrix Rain trên Console](https://laptrinhvb.net/uploads/source/vbnet/matrix_rain_console.png)
![[C#] Hướng dẫn Thêm, xóa, sửa với PostgreSQL winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/screenshot_1686208193.png)
![[C#] Chia sẽ Tool Convert Json String to class csharp, vb, typescript](https://laptrinhvb.net/uploads/source/vbnet/json_tool_thumb.png)
![[C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform](https://laptrinhvb.net/uploads/source/vbnet/simple_observer_pattern_csharp_thumb.png)
![[C#] Sử dụng FolderBrowserDialog Vista trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/folder_brower_vista.png)

![[C#] Hướng dẫn bắt sự kiện khi đối tượng di chuyển chạm vào object trên winform HitTest Object](https://laptrinhvb.net/uploads/source/new_image_baiviet/hitTest_object.gif)
![[C#] Sử dụng scan đọc qrcode bằng Python trong winform](https://laptrinhvb.net/uploads/source/vbnet/qrcode_reader_python.png)
![[C#] Hướng dẫn load hình ảnh vào picture box chế độ Unlock](https://laptrinhvb.net/uploads/source/image_baiviet/3ddeac9c43a69536767cc2870f28b4f1.png)
![[C#] Hướng dẫn sử dụng Ternary Operator (rút gọn cấu trúc rẽ nhánh if else)](https://laptrinhvb.net/uploads/source/csharp/ternary_csharp.png)
![[C#] Hướng dẫn tích hợp ứng dụng Notepad vào Winform](https://laptrinhvb.net/uploads/source/image_baiviet/364e3d7fe76255197cae5b07b9e1fff3.png)
![[C#] Hướng dẫn thêm xóa sửa trong database MongoDB](https://laptrinhvb.net/uploads/source/csharp/mongodb_thumb.png)
![[C#] Hướng dẫn tạo hiệu ứng ẩn hiện ctrol trên Winform sử dụng Bunifu Animation Framework](https://laptrinhvb.net/uploads/source/csharp/animation_csharp_thumb.gif)
![[C#] Hướng dẫn sử dụng định dạng chuỗi với String.Format](https://laptrinhvb.net/uploads/source/csharp/string_format_thumb.jpg)
![[C#] Chia sẽ thư viện sử dụng Color Picker trong Winform](https://laptrinhvb.net/uploads/source/csharp/color_picker_thumb.png)
![[C#] Hướng dẫn thiết kế form loading splash screen transparent winform](https://laptrinhvb.net/uploads/source/vbnet/TransparentSplashScreen_thumb.png)
![[C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF](https://laptrinhvb.net/uploads/source/new_image_baiviet/tao_file_pdf.png)
![[C#] Hướng dẫn tạo Auto Number trên Datagridview winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/auto_number_datagridview_csharp.png)
![[C#] Hướng dẫn chuyển đổi DataTable sang Table HTML](https://laptrinhvb.net/uploads/source/csharp/table_html_thumb.png)
![[C#] Chia sẽ source code phần mềm import dữ liệu từ Excel vào Database Sqlserver](https://laptrinhvb.net/uploads/source/DATABASE/Excel2SQL_thumb.png)
