NEWS
                        
                    - [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#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn phần mềm PDF Split, dùng để tách tập tin PDF thành nhiều trang theo tùy chọn.
[Tool] Phần mềm tách một file tập tin pdf thành nhiều file.
Giao diện demo ứng dụng C#:

Phần mềm cho phép bạn tách file pdf theo trang chẵn, hoặc theo trang lẻ, hoặc nhập số 2 vào.
Tùy chọn xuất 1 file vào 1 folder, hoặc tất cả vào chung 1 folder.
Source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace PDFSplitterApp
{
    public partial class FormMain : Form
    {
        private List<string> selectedPDFPaths = new List<string>();
        private string outputFolderPath = string.Empty;
        private string inputFolderPath = string.Empty;
        public FormMain()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
            this.btnSelectPDF = new System.Windows.Forms.Button();
            this.btnSelectOutputFolder = new System.Windows.Forms.Button();
            this.btnSplitPDF = new System.Windows.Forms.Button();
            this.lblInputFiles = new System.Windows.Forms.Label();
            this.lblOutputFolder = new System.Windows.Forms.Label();
            this.txtOutputPrefix = new System.Windows.Forms.TextBox();
            this.lblOutputPrefix = new System.Windows.Forms.Label();
            this.rbSplitEvenPages = new System.Windows.Forms.RadioButton();
            this.rbSplitOddPages = new System.Windows.Forms.RadioButton();
            this.rbSplitCustomPages = new System.Windows.Forms.RadioButton();
            this.txtCustomPages = new System.Windows.Forms.TextBox();
            this.lblCustomPagesHelp = new System.Windows.Forms.Label();
            this.statusStrip = new System.Windows.Forms.StatusStrip();
            this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.progressBar = new System.Windows.Forms.ToolStripProgressBar();
            this.lvResults = new System.Windows.Forms.ListView();
            this.colOutFileName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.colOutFilePath = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.colPages = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.groupBoxSplitOptions = new System.Windows.Forms.GroupBox();
            this.lvInputFiles = new System.Windows.Forms.ListView();
            this.colFileName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.colFilePath = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.colPageCount = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.btnRemoveFiles = new System.Windows.Forms.Button();
            this.btnOpenInputFolder = new System.Windows.Forms.Button();
            this.groupBoxOutputOptions = new System.Windows.Forms.GroupBox();
            this.rbSeparateFolders = new System.Windows.Forms.RadioButton();
            this.rbSingleFolder = new System.Windows.Forms.RadioButton();
            this.statusStrip.SuspendLayout();
            this.groupBoxSplitOptions.SuspendLayout();
            this.groupBoxOutputOptions.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnSelectPDF
            // 
            this.btnSelectPDF.Location = new System.Drawing.Point(12, 12);
            this.btnSelectPDF.Name = "btnSelectPDF";
            this.btnSelectPDF.Size = new System.Drawing.Size(130, 30);
            this.btnSelectPDF.TabIndex = 0;
            this.btnSelectPDF.Text = "Chọn File PDF";
            this.btnSelectPDF.UseVisualStyleBackColor = true;
            this.btnSelectPDF.Click += new System.EventHandler(this.btnSelectPDF_Click);
            // 
            // btnSelectOutputFolder
            // 
            this.btnSelectOutputFolder.Location = new System.Drawing.Point(12, 48);
            this.btnSelectOutputFolder.Name = "btnSelectOutputFolder";
            this.btnSelectOutputFolder.Size = new System.Drawing.Size(130, 30);
            this.btnSelectOutputFolder.TabIndex = 2;
            this.btnSelectOutputFolder.Text = "Chọn Thư Mục Lưu";
            this.btnSelectOutputFolder.UseVisualStyleBackColor = true;
            this.btnSelectOutputFolder.Click += new System.EventHandler(this.btnSelectOutputFolder_Click);
            // 
            // btnSplitPDF
            // 
            this.btnSplitPDF.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnSplitPDF.Location = new System.Drawing.Point(12, 440);
            this.btnSplitPDF.Name = "btnSplitPDF";
            this.btnSplitPDF.Size = new System.Drawing.Size(630, 40);
            this.btnSplitPDF.TabIndex = 9;
            this.btnSplitPDF.Text = "TÁCH FILE PDF";
            this.btnSplitPDF.UseVisualStyleBackColor = true;
            this.btnSplitPDF.Click += new System.EventHandler(this.btnSplitPDF_Click);
            // 
            // lblInputFiles
            // 
            this.lblInputFiles.AutoSize = true;
            this.lblInputFiles.Location = new System.Drawing.Point(148, 21);
            this.lblInputFiles.Name = "lblInputFiles";
            this.lblInputFiles.Size = new System.Drawing.Size(120, 13);
            this.lblInputFiles.TabIndex = 1;
            this.lblInputFiles.Text = "Chưa chọn file PDF nào";
            // 
            // lblOutputFolder
            // 
            this.lblOutputFolder.AutoSize = true;
            this.lblOutputFolder.Location = new System.Drawing.Point(148, 57);
            this.lblOutputFolder.Name = "lblOutputFolder";
            this.lblOutputFolder.Size = new System.Drawing.Size(117, 13);
            this.lblOutputFolder.TabIndex = 3;
            this.lblOutputFolder.Text = "Chưa chọn thư mục lưu";
            // 
            // txtOutputPrefix
            // 
            this.txtOutputPrefix.Location = new System.Drawing.Point(101, 85);
            this.txtOutputPrefix.Name = "txtOutputPrefix";
            this.txtOutputPrefix.Size = new System.Drawing.Size(200, 20);
            this.txtOutputPrefix.TabIndex = 5;
            this.txtOutputPrefix.Text = "PDFSplit";
            // 
            // lblOutputPrefix
            // 
            this.lblOutputPrefix.AutoSize = true;
            this.lblOutputPrefix.Location = new System.Drawing.Point(12, 88);
            this.lblOutputPrefix.Name = "lblOutputPrefix";
            this.lblOutputPrefix.Size = new System.Drawing.Size(80, 13);
            this.lblOutputPrefix.TabIndex = 4;
            this.lblOutputPrefix.Text = "Tên file xuất ra:";
            // 
            // rbSplitEvenPages
            // 
            this.rbSplitEvenPages.AutoSize = true;
            this.rbSplitEvenPages.Location = new System.Drawing.Point(20, 30);
            this.rbSplitEvenPages.Name = "rbSplitEvenPages";
            this.rbSplitEvenPages.Size = new System.Drawing.Size(128, 17);
            this.rbSplitEvenPages.TabIndex = 0;
            this.rbSplitEvenPages.Text = "Tách theo trang chẵn";
            this.rbSplitEvenPages.UseVisualStyleBackColor = true;
            // 
            // rbSplitOddPages
            // 
            this.rbSplitOddPages.AutoSize = true;
            this.rbSplitOddPages.Location = new System.Drawing.Point(20, 55);
            this.rbSplitOddPages.Name = "rbSplitOddPages";
            this.rbSplitOddPages.Size = new System.Drawing.Size(112, 17);
            this.rbSplitOddPages.TabIndex = 1;
            this.rbSplitOddPages.Text = "Tách theo trang lẻ";
            this.rbSplitOddPages.UseVisualStyleBackColor = true;
            // 
            // rbSplitCustomPages
            // 
            this.rbSplitCustomPages.AutoSize = true;
            this.rbSplitCustomPages.Checked = true;
            this.rbSplitCustomPages.Location = new System.Drawing.Point(20, 80);
            this.rbSplitCustomPages.Name = "rbSplitCustomPages";
            this.rbSplitCustomPages.Size = new System.Drawing.Size(140, 17);
            this.rbSplitCustomPages.TabIndex = 2;
            this.rbSplitCustomPages.TabStop = true;
            this.rbSplitCustomPages.Text = "Tách theo trang tự chọn";
            this.rbSplitCustomPages.UseVisualStyleBackColor = true;
            this.rbSplitCustomPages.CheckedChanged += new System.EventHandler(this.rbSplitCustomPages_CheckedChanged);
            // 
            // txtCustomPages
            // 
            this.txtCustomPages.Location = new System.Drawing.Point(166, 80);
            this.txtCustomPages.Name = "txtCustomPages";
            this.txtCustomPages.Size = new System.Drawing.Size(95, 20);
            this.txtCustomPages.TabIndex = 3;
            this.txtCustomPages.Text = "2";
            // 
            // lblCustomPagesHelp
            // 
            this.lblCustomPagesHelp.AutoSize = true;
            this.lblCustomPagesHelp.Location = new System.Drawing.Point(267, 83);
            this.lblCustomPagesHelp.Name = "lblCustomPagesHelp";
            this.lblCustomPagesHelp.Size = new System.Drawing.Size(268, 13);
            this.lblCustomPagesHelp.TabIndex = 4;
            this.lblCustomPagesHelp.Text = "Số trang trong mỗi file (vd: 2 trang thì 20 trang ra 10 file)";
            // 
            // statusStrip
            // 
            this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.statusLabel,
            this.progressBar});
            this.statusStrip.Location = new System.Drawing.Point(0, 654);
            this.statusStrip.Name = "statusStrip";
            this.statusStrip.Size = new System.Drawing.Size(654, 22);
            this.statusStrip.TabIndex = 11;
            this.statusStrip.Text = "statusStrip1";
            // 
            // statusLabel
            // 
            this.statusLabel.Name = "statusLabel";
            this.statusLabel.Size = new System.Drawing.Size(54, 17);
            this.statusLabel.Text = "Sẵn sàng";
            // 
            // progressBar
            // 
            this.progressBar.Name = "progressBar";
            this.progressBar.Size = new System.Drawing.Size(200, 16);
            // 
            // lvResults
            // 
            this.lvResults.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.colOutFileName,
            this.colOutFilePath,
            this.colPages});
            this.lvResults.FullRowSelect = true;
            this.lvResults.GridLines = true;
            this.lvResults.HideSelection = false;
            this.lvResults.Location = new System.Drawing.Point(12, 490);
            this.lvResults.Name = "lvResults";
            this.lvResults.Size = new System.Drawing.Size(630, 150);
            this.lvResults.TabIndex = 10;
            this.lvResults.UseCompatibleStateImageBehavior = false;
            this.lvResults.View = System.Windows.Forms.View.Details;
            // 
            // colOutFileName
            // 
            this.colOutFileName.Text = "Tên File";
            this.colOutFileName.Width = 150;
            // 
            // colOutFilePath
            // 
            this.colOutFilePath.Text = "Đường Dẫn";
            this.colOutFilePath.Width = 380;
            // 
            // colPages
            // 
            this.colPages.Text = "Số Trang";
            this.colPages.Width = 80;
            // 
            // groupBoxSplitOptions
            // 
            this.groupBoxSplitOptions.Controls.Add(this.rbSplitEvenPages);
            this.groupBoxSplitOptions.Controls.Add(this.rbSplitOddPages);
            this.groupBoxSplitOptions.Controls.Add(this.rbSplitCustomPages);
            this.groupBoxSplitOptions.Controls.Add(this.txtCustomPages);
            this.groupBoxSplitOptions.Controls.Add(this.lblCustomPagesHelp);
            this.groupBoxSplitOptions.Location = new System.Drawing.Point(12, 270);
            this.groupBoxSplitOptions.Name = "groupBoxSplitOptions";
            this.groupBoxSplitOptions.Size = new System.Drawing.Size(630, 120);
            this.groupBoxSplitOptions.TabIndex = 8;
            this.groupBoxSplitOptions.TabStop = false;
            this.groupBoxSplitOptions.Text = "Tùy chọn tách";
            // 
            // lvInputFiles
            // 
            this.lvInputFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.colFileName,
            this.colFilePath,
            this.colPageCount});
            this.lvInputFiles.FullRowSelect = true;
            this.lvInputFiles.GridLines = true;
            this.lvInputFiles.HideSelection = false;
            this.lvInputFiles.Location = new System.Drawing.Point(12, 120);
            this.lvInputFiles.Name = "lvInputFiles";
            this.lvInputFiles.Size = new System.Drawing.Size(630, 100);
            this.lvInputFiles.TabIndex = 6;
            this.lvInputFiles.UseCompatibleStateImageBehavior = false;
            this.lvInputFiles.View = System.Windows.Forms.View.Details;
            // 
            // colFileName
            // 
            this.colFileName.Text = "Tên File";
            this.colFileName.Width = 150;
            // 
            // colFilePath
            // 
            this.colFilePath.Text = "Đường Dẫn";
            this.colFilePath.Width = 380;
            // 
            // colPageCount
            // 
            this.colPageCount.Text = "Số Trang";
            this.colPageCount.Width = 80;
            // 
            // btnRemoveFiles
            // 
            this.btnRemoveFiles.Enabled = false;
            this.btnRemoveFiles.Location = new System.Drawing.Point(12, 226);
            this.btnRemoveFiles.Name = "btnRemoveFiles";
            this.btnRemoveFiles.Size = new System.Drawing.Size(130, 30);
            this.btnRemoveFiles.TabIndex = 7;
            this.btnRemoveFiles.Text = "Xóa File Đã Chọn";
            this.btnRemoveFiles.UseVisualStyleBackColor = true;
            this.btnRemoveFiles.Click += new System.EventHandler(this.btnRemoveFiles_Click);
            // 
            // btnOpenInputFolder
            // 
            this.btnOpenInputFolder.Enabled = false;
            this.btnOpenInputFolder.Location = new System.Drawing.Point(512, 12);
            this.btnOpenInputFolder.Name = "btnOpenInputFolder";
            this.btnOpenInputFolder.Size = new System.Drawing.Size(130, 30);
            this.btnOpenInputFolder.TabIndex = 12;
            this.btnOpenInputFolder.Text = "Mở Thư Mục Nhập";
            this.btnOpenInputFolder.UseVisualStyleBackColor = true;
            this.btnOpenInputFolder.Click += new System.EventHandler(this.btnOpenInputFolder_Click);
            // 
            // groupBoxOutputOptions
            // 
            this.groupBoxOutputOptions.Controls.Add(this.rbSeparateFolders);
            this.groupBoxOutputOptions.Controls.Add(this.rbSingleFolder);
            this.groupBoxOutputOptions.Location = new System.Drawing.Point(12, 396);
            this.groupBoxOutputOptions.Name = "groupBoxOutputOptions";
            this.groupBoxOutputOptions.Size = new System.Drawing.Size(630, 38);
            this.groupBoxOutputOptions.TabIndex = 13;
            this.groupBoxOutputOptions.TabStop = false;
            this.groupBoxOutputOptions.Text = "Tùy chọn xuất";
            // 
            // rbSeparateFolders
            // 
            this.rbSeparateFolders.AutoSize = true;
            this.rbSeparateFolders.Location = new System.Drawing.Point(276, 15);
            this.rbSeparateFolders.Name = "rbSeparateFolders";
            this.rbSeparateFolders.Size = new System.Drawing.Size(196, 17);
            this.rbSeparateFolders.TabIndex = 1;
            this.rbSeparateFolders.Text = "Tạo thư mục riêng cho từng file PDF";
            this.rbSeparateFolders.UseVisualStyleBackColor = true;
            // 
            // rbSingleFolder
            // 
            this.rbSingleFolder.AutoSize = true;
            this.rbSingleFolder.Checked = true;
            this.rbSingleFolder.Location = new System.Drawing.Point(20, 15);
            this.rbSingleFolder.Name = "rbSingleFolder";
            this.rbSingleFolder.Size = new System.Drawing.Size(175, 17);
            this.rbSingleFolder.TabIndex = 0;
            this.rbSingleFolder.TabStop = true;
            this.rbSingleFolder.Text = "Xuất tất cả file vào một thư mục";
            this.rbSingleFolder.UseVisualStyleBackColor = true;
            // 
            // FormMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(654, 676);
            this.Controls.Add(this.groupBoxOutputOptions);
            this.Controls.Add(this.btnOpenInputFolder);
            this.Controls.Add(this.statusStrip);
            this.Controls.Add(this.lvResults);
            this.Controls.Add(this.btnSplitPDF);
            this.Controls.Add(this.groupBoxSplitOptions);
            this.Controls.Add(this.btnRemoveFiles);
            this.Controls.Add(this.lvInputFiles);
            this.Controls.Add(this.txtOutputPrefix);
            this.Controls.Add(this.lblOutputPrefix);
            this.Controls.Add(this.lblOutputFolder);
            this.Controls.Add(this.btnSelectOutputFolder);
            this.Controls.Add(this.lblInputFiles);
            this.Controls.Add(this.btnSelectPDF);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "FormMain";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Ứng Dụng Tách File PDF";
            this.statusStrip.ResumeLayout(false);
            this.statusStrip.PerformLayout();
            this.groupBoxSplitOptions.ResumeLayout(false);
            this.groupBoxSplitOptions.PerformLayout();
            this.groupBoxOutputOptions.ResumeLayout(false);
            this.groupBoxOutputOptions.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        private System.Windows.Forms.Button btnSelectPDF;
        private System.Windows.Forms.Button btnSelectOutputFolder;
        private System.Windows.Forms.Button btnSplitPDF;
        private System.Windows.Forms.Label lblInputFiles;
        private System.Windows.Forms.Label lblOutputFolder;
        private System.Windows.Forms.TextBox txtOutputPrefix;
        private System.Windows.Forms.Label lblOutputPrefix;
        private System.Windows.Forms.RadioButton rbSplitEvenPages;
        private System.Windows.Forms.RadioButton rbSplitOddPages;
        private System.Windows.Forms.RadioButton rbSplitCustomPages;
        private System.Windows.Forms.TextBox txtCustomPages;
        private System.Windows.Forms.Label lblCustomPagesHelp;
        private System.Windows.Forms.GroupBox groupBoxSplitOptions;
        private System.Windows.Forms.StatusStrip statusStrip;
        private System.Windows.Forms.ToolStripStatusLabel statusLabel;
        private System.Windows.Forms.ToolStripProgressBar progressBar;
        private System.Windows.Forms.ListView lvResults;
        private System.Windows.Forms.ColumnHeader colOutFileName;
        private System.Windows.Forms.ColumnHeader colOutFilePath;
        private System.Windows.Forms.ColumnHeader colPages;
        private System.Windows.Forms.ListView lvInputFiles;
        private System.Windows.Forms.ColumnHeader colFileName;
        private System.Windows.Forms.ColumnHeader colFilePath;
        private System.Windows.Forms.ColumnHeader colPageCount;
        private System.Windows.Forms.Button btnRemoveFiles;
        private System.Windows.Forms.Button btnOpenInputFolder;
        private System.Windows.Forms.GroupBox groupBoxOutputOptions;
        private System.Windows.Forms.RadioButton rbSeparateFolders;
        private System.Windows.Forms.RadioButton rbSingleFolder;
        private void btnSelectPDF_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
                openFileDialog.Title = "Chọn File PDF";
                openFileDialog.Multiselect = true; // Cho phép chọn nhiều file
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // Nếu đây là lần chọn file đầu tiên, lưu thư mục đầu vào
                    if (string.IsNullOrEmpty(inputFolderPath) && openFileDialog.FileNames.Length > 0)
                    {
                        inputFolderPath = Path.GetDirectoryName(openFileDialog.FileNames[0]);
                        btnOpenInputFolder.Enabled = true;
                    }
                    foreach (string fileName in openFileDialog.FileNames)
                    {
                        if (!selectedPDFPaths.Contains(fileName))
                        {
                            selectedPDFPaths.Add(fileName);
                            // Thêm vào ListView
                            ListViewItem item = new ListViewItem(Path.GetFileName(fileName));
                            item.SubItems.Add(fileName);
                            item.SubItems.Add(GetPDFPageCount(fileName).ToString());
                            lvInputFiles.Items.Add(item);
                        }
                    }
                    // Cập nhật status và label
                    lblInputFiles.Text = $"Đã chọn {selectedPDFPaths.Count} file PDF";
                    statusLabel.Text = $"Đã chọn {selectedPDFPaths.Count} file PDF";
                    // Nếu là file đầu tiên, đặt tên xuất ra mặc định
                    if (selectedPDFPaths.Count > 0 && txtOutputPrefix.Text == "PDFSplit")
                    {
                        txtOutputPrefix.Text = Path.GetFileNameWithoutExtension(selectedPDFPaths[0]);
                        if (selectedPDFPaths.Count > 1)
                        {
                            txtOutputPrefix.Text = "PDFSplit";
                        }
                    }
                    btnRemoveFiles.Enabled = selectedPDFPaths.Count > 0;
                }
            }
        }
        private void btnOpenInputFolder_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(inputFolderPath) && Directory.Exists(inputFolderPath))
            {
                System.Diagnostics.Process.Start("explorer.exe", inputFolderPath);
            }
        }
        private void btnSelectOutputFolder_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "Chọn thư mục lưu file tách";
                folderBrowserDialog.ShowNewFolderButton = true;
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    outputFolderPath = folderBrowserDialog.SelectedPath;
                    lblOutputFolder.Text = outputFolderPath;
                    statusLabel.Text = "Đã chọn thư mục lưu: " + outputFolderPath;
                }
            }
        }
        private void btnRemoveFiles_Click(object sender, EventArgs e)
        {
            if (lvInputFiles.SelectedItems.Count > 0)
            {
                // Tạo danh sách tạm để xóa (tránh lỗi khi xóa trực tiếp)
                List<ListViewItem> itemsToRemove = new List<ListViewItem>();
                List<string> pathsToRemove = new List<string>();
                foreach (ListViewItem item in lvInputFiles.SelectedItems)
                {
                    itemsToRemove.Add(item);
                    pathsToRemove.Add(item.SubItems[1].Text);
                }
                // Xóa từ ListView và danh sách đường dẫn
                foreach (ListViewItem item in itemsToRemove)
                {
                    lvInputFiles.Items.Remove(item);
                }
                foreach (string path in pathsToRemove)
                {
                    selectedPDFPaths.Remove(path);
                }
                lblInputFiles.Text = $"Đã chọn {selectedPDFPaths.Count} file PDF";
                statusLabel.Text = $"Đã chọn {selectedPDFPaths.Count} file PDF";
                btnRemoveFiles.Enabled = selectedPDFPaths.Count > 0;
                // Disable nút mở thư mục nếu không còn file nào
                if (selectedPDFPaths.Count == 0)
                {
                    btnOpenInputFolder.Enabled = false;
                    inputFolderPath = string.Empty;
                }
            }
        }
        private int GetPDFPageCount(string pdfPath)
        {
            try
            {
                using (PdfReader reader = new PdfReader(pdfPath))
                {
                    return reader.NumberOfPages;
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }
        private void rbSplitCustomPages_CheckedChanged(object sender, EventArgs e)
        {
            txtCustomPages.Enabled = rbSplitCustomPages.Checked;
        }
        private async void btnSplitPDF_Click(object sender, EventArgs e)
        {
            if (selectedPDFPaths.Count == 0)
            {
                MessageBox.Show("Vui lòng chọn ít nhất một file PDF cần tách!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(outputFolderPath))
            {
                MessageBox.Show("Vui lòng chọn thư mục lưu kết quả!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(txtOutputPrefix.Text))
            {
                MessageBox.Show("Vui lòng nhập tên file xuất ra!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string mainOutputFolder;
            // Tạo thư mục chính cho kết quả
            if (rbSingleFolder.Checked)
            {
                mainOutputFolder = Path.Combine(outputFolderPath,
                    txtOutputPrefix.Text + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
                Directory.CreateDirectory(mainOutputFolder);
            }
            else
            {
                mainOutputFolder = outputFolderPath;
            }
            // Disable controls during processing
            SetControlsEnabled(false);
            lvResults.Items.Clear();
            progressBar.Value = 0;
            try
            {
                statusLabel.Text = "Đang xử lý...";
                // Tạo biến tổng số file để xử lý progressBar
                int totalFiles = selectedPDFPaths.Count;
                int filesProcessed = 0;
                // Xử lý từng file PDF trong danh sách
                foreach (string pdfPath in selectedPDFPaths)
                {
                    string baseName = Path.GetFileNameWithoutExtension(pdfPath);
                    string fileOutputFolder;
                    // Xác định thư mục xuất cho file hiện tại
                    if (rbSeparateFolders.Checked)
                    {
                        fileOutputFolder = Path.Combine(mainOutputFolder, baseName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
                        Directory.CreateDirectory(fileOutputFolder);
                    }
                    else
                    {
                        fileOutputFolder = mainOutputFolder;
                    }
                    // Cập nhật status
                    this.Invoke((MethodInvoker)delegate {
                        statusLabel.Text = $"Đang xử lý file {baseName}... ({filesProcessed + 1}/{totalFiles})";
                    });
                    await Task.Run(() => {
                        // Tách file
                        SplitPDF(pdfPath, fileOutputFolder, baseName);
                    });
                    filesProcessed++;
                    // Cập nhật progressBar tổng thể
                    this.Invoke((MethodInvoker)delegate {
                        progressBar.Value = Math.Min((filesProcessed * 100) / totalFiles, 100);
                    });
                }
                statusLabel.Text = "Hoàn thành tách file PDF!";
                progressBar.Value = 100;
                string outputMessage = rbSingleFolder.Checked
                     ? $"Đã tách {totalFiles} file PDF thành công!
Các file đã được lưu vào thư mục:
{mainOutputFolder}"
                     : $"Đã tách {totalFiles} file PDF thành công!
Các file đã được lưu vào các thư mục con trong:
{mainOutputFolder}";
                MessageBox.Show(outputMessage, "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // Mở thư mục xuất
                System.Diagnostics.Process.Start("explorer.exe", mainOutputFolder);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Có lỗi xảy ra: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                statusLabel.Text = "Tách file thất bại!";
            }
            finally
            {
                // Re-enable controls
                SetControlsEnabled(true);
            }
        }
        private void SetControlsEnabled(bool enabled)
        {
            btnSelectPDF.Enabled = enabled;
            btnSelectOutputFolder.Enabled = enabled;
            btnSplitPDF.Enabled = enabled;
            btnRemoveFiles.Enabled = enabled && selectedPDFPaths.Count > 0;
            btnOpenInputFolder.Enabled = enabled && !string.IsNullOrEmpty(inputFolderPath);
            txtOutputPrefix.Enabled = enabled;
            rbSplitEvenPages.Enabled = enabled;
            rbSplitOddPages.Enabled = enabled;
            rbSplitCustomPages.Enabled = enabled;
            txtCustomPages.Enabled = enabled && rbSplitCustomPages.Checked;
            lvInputFiles.Enabled = enabled;
            rbSingleFolder.Enabled = enabled;
            rbSeparateFolders.Enabled = enabled;
        }
        private void SplitPDF(string pdfPath, string outputFolder, string baseName)
        {
            // Mở file PDF nguồn
            PdfReader reader = new PdfReader(pdfPath);
            int totalPages = reader.NumberOfPages;
            List<int> pagesToExtract = new List<int>();
            // Xác định trang cần tách theo tùy chọn
            if (rbSplitEvenPages.Checked)
            {
                // Tách trang chẵn
                for (int i = 2; i <= totalPages; i += 2)
                {
                    pagesToExtract.Add(i);
                }
            }
            else if (rbSplitOddPages.Checked)
            {
                // Tách trang lẻ
                for (int i = 1; i <= totalPages; i += 2)
                {
                    pagesToExtract.Add(i);
                }
            }
            else if (rbSplitCustomPages.Checked)
            {
                // Tách theo số trang tự chọn
                int pagesPerFile = 1;
                // Invoke để lấy giá trị từ UI thread
                this.Invoke((MethodInvoker)delegate {
                    if (!int.TryParse(txtCustomPages.Text, out pagesPerFile) || pagesPerFile < 1)
                    {
                        pagesPerFile = 1;
                    }
                });
                // Tạo các file với số trang được chỉ định
                int currentPage = 1;
                int fileCount = 0;
                while (currentPage <= totalPages)
                {
                    fileCount++;
                    string outputFilePath = Path.Combine(outputFolder, $"{baseName}_{fileCount}.pdf");
                    Document document = new Document(reader.GetPageSizeWithRotation(1));
                    PdfCopy copy = new PdfCopy(document, new FileStream(outputFilePath, FileMode.Create));
                    document.Open();
                    int pageCount = 0;
                    while (currentPage <= totalPages && pageCount < pagesPerFile)
                    {
                        copy.AddPage(copy.GetImportedPage(reader, currentPage));
                        currentPage++;
                        pageCount++;
                    }
                    document.Close();
                    copy.Close();
                    // Update UI
                    int filePageCount = pageCount;
                    string fileName = Path.GetFileName(outputFilePath);
                    this.Invoke((MethodInvoker)delegate {
                        ListViewItem item = new ListViewItem(fileName);
                        item.SubItems.Add(outputFilePath);
                        item.SubItems.Add(filePageCount.ToString());
                        lvResults.Items.Add(item);
                    });
                }
                reader.Close();
                return;
            }
            // Xử lý tách theo trang chẵn hoặc lẻ
            if (pagesToExtract.Count > 0)
            {
                int fileCount = 0;
                foreach (int pageNum in pagesToExtract)
                {
                    fileCount++;
                    string outputFilePath = Path.Combine(outputFolder, $"{baseName}_{fileCount}.pdf");
                    Document document = new Document(reader.GetPageSizeWithRotation(pageNum));
                    PdfCopy copy = new PdfCopy(document, new FileStream(outputFilePath, FileMode.Create));
                    document.Open();
                    copy.AddPage(copy.GetImportedPage(reader, pageNum));
                    document.Close();
                    copy.Close();
                    // Update UI
                    string fileName = Path.GetFileName(outputFilePath);
                    this.Invoke((MethodInvoker)delegate {
                        ListViewItem item = new ListViewItem(fileName);
                        item.SubItems.Add(outputFilePath);
                        item.SubItems.Add("1");
                        lvResults.Items.Add(item);
                    });
                }
            }
            reader.Close();
        }
    }
}Thanks for watching!

![[C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[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](https://laptrinhvb.net/uploads/source/new_image_baiviet/tool_image_downloader.png)
![[C#] Chụp hình và quay video từ camera trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/thumb_recordvideo_camera_csharp.png)
![[C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn](https://laptrinhvb.net/uploads/source/new_image_baiviet/split_pdf_tool_thumb.jpg)


![[C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF](https://laptrinhvb.net/uploads/source/new_image_baiviet/pdf_counter_tool.png)
![[C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng](https://laptrinhvb.net/uploads/source/new_image_baiviet/hwid_csharp.png)
![[C#] Hướng dẫn tạo mã QRcode Style trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/demo_qrcode_style.png)
![[C#] Hướng dẫn sử dụng temp mail service api trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/temp_mail_service.png)
![[C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/vietqr_image.jpg)
![[C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf](https://laptrinhvb.net/uploads/source/new_image_baiviet/topsheft_windowsservices.png)
![[C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/google_sheet_thumb.png)
![[C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/momo_danang.png)
![[C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/my_scheduler_app.png)
![[C#] Hướng dẫn download file từ Minio Server Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/Minio_demo.png)
![[C#] Hướng dẫn đăng nhập zalo login sử dụng API v4 trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/zalo-api-la-gi.jpg)
![[C#] Việt hóa Text Button trên MessageBox Dialog Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/change_text_button_messagebox.png)
![[POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code](https://laptrinhvb.net/uploads/source/new_image_baiviet/zalo_instance_csharp.png)
![[C#] Chia sẻ code lock và unlock user trong domain Window](https://laptrinhvb.net/uploads/source/new_image_baiviet/lock_unlock_user_domain.png)
![[C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/microsoft-authenticator-cover.jpg)
![[C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/HTML_TO_PDF_NETCORE.png)
![[C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/1_AsXti9JBcuEGIODbisEAYw.jpg)


![[C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform](https://laptrinhvb.net/uploads/source/auto_it_thumb.png)
