How to delete the specified page in the document using C# and .NET


    This code example shows how to delete a particular page in a document. In this case, we will delete the second page and save the document in DOCX format. Also applicable to documents in PDF format.

Complete code

´╗┐using SautinSoft.Document;
using System;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            DeleteSpecifiedPageInDocument();       
        }
        /// <summary>
        /// How to delete the specified page in the document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/from-customers-delete-specified-page-in-document-net-csharp-vb.php
        /// </remarks>
        public static void DeleteSpecifiedPageInDocument()
        {
            string inpFile = @"..\..\..\example.docx";
            string outFile = @"result.docx";
            
			// Load a document into DocumentCore.
            DocumentCore dc = DocumentCore.Load(inpFile);

            // Divide the document into separate pages.
            DocumentPaginator dp = dc.GetPaginator();

            // Delete page number two.
            dp.Pages[1].Content.Delete();
            
			 // Save our result as a DOCX file.
            dc.Save(outFile);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

Imports SautinSoft.Document
Imports System

Namespace Sample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			DeleteSpecifiedPageInDocument()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' How to delete the specified page in the document.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/from-customers-delete-specified-page-in-document-net-csharp-vb.php
		''' </remarks>
		Public Shared Sub DeleteSpecifiedPageInDocument()
			Dim inpFile As String = "..\..\..\example.docx"
			Dim outFile As String = "result.docx"

			' Load a document into DocumentCore.
			Dim dc As DocumentCore = DocumentCore.Load(inpFile)

			' Divide the document into separate pages.
			Dim dp As DocumentPaginator = dc.GetPaginator()

			' Delete page number two.
			dp.Pages(1).Content.Delete()

			 ' Save our result as a DOCX file.
			dc.Save(outFile)

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.

Download