How to delete or remove pages from PDF files Using C# Windows application

 In this article, I will explain to you, how to delete or Remove a PDF page using the C# Windows Application.

This small application features.

a.    This application deletes the pdf page by selecting a page number.

b.      This is very useful when real-time work in any workstation, The Client needs this type of requirement or he wants to print pdf documents but inside pdf documents blank pages. So this situation needs to delete a blank page because if every PDF has one or two pages blank it’s cost-effective. So for this purpose, I developed this application.

c.      This application also has a feature to delete multiple PDF document blank pages in one click.

     Steps.

  1.   Create a new Windows application project.
  2.   Drag and drop controls textbox, Combobox, and buttons.
  3.   Install FreeSpire.PDF NuGet Package.
  4.  Include the following namespace in .cs program file.

 

 

 

 

Delete PDF Pages Using C#

To Delete PDF pages using C# here’s the below example.

Source code

 

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 Spire.Pdf;

 

namespace PDFbuild

{

    public partial class Form1: Form

    {

        public Form1()

        {

            InitializeComponent();

        } 

        private voidbutton1_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog folderDlg = newFolderBrowserDialog();

            folderDlg.ShowNewFolderButton = true;

            // Show the FolderBrowserDialog. 

            DialogResult result = folderDlg.ShowDialog();

            if(result == DialogResult.OK)

            {

                textBox1.Text = folderDlg.SelectedPath;

                Environment.SpecialFolder root = folderDlg.RootFolder;

            } 

        }

 

        private voidbutton2_Click(object sender, EventArgs e)

        {

             if(textBox1.Text == “”)

            {

                MessageBox.Show(“Please select source path”);

                return;

            }

            if(textBox2.Text == “”)

            {

                MessageBox.Show(“Please select destination path “);

                return;

            }

            if(comboBox1.SelectedIndex == 0)

            {

                MessageBox.Show(“Please select page”);

                return;

            } 

            stringroot = textBox1.Text;

            stringdestroot = textBox2.Text;

            string[] fileEntries = Directory.GetFiles(root);      

             intpage=-0;

            if(comboBox1.Text == “1”)

            {

                page = 0;

            }

            if(comboBox1.Text == “2”)

            {

                page = 1;

            }

            if(comboBox1.Text == “3”)

            { page = 2;

            }            

            foreach(string fileName in fileEntries)

            { 

                PdfDocument document = new PdfDocument(); 

                document.LoadFromFile(fileName); 

                document.Pages.RemoveAt(page); 

                stringfilen = Path.GetFileName(fileName);

                document.SaveToFile(destroot + “\”+ filen); 

            }

             MessageBox.Show(“Done”);

        }

 

        private voidbutton3_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog folderDlg = new FolderBrowserDialog();

            folderDlg.ShowNewFolderButton = true;

            // Show the FolderBrowserDialog. 

            DialogResult result = folderDlg.ShowDialog();

            if(result == DialogResult.OK)

            {

                textBox2.Text = folderDlg.SelectedPath;

                Environment.SpecialFolder root = folderDlg.RootFolder;

            }

        }

         private voidForm1_Load(object sender, EventArgs e)

        {

            comboBox1.SelectedIndex = 0;

        }       

    }

} 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *