page 276

Delete PDF Page in WPF

2013-02-22 03:40:05 Written by Koohji

This section is designed to provide a solution for developers on how to delete PDF page for WPF via this PDF library Spire.PDF for WPF.

Spire.PDF for WPF enables users to delete any PDF page only by one line of code: RemoveAt(int index). As you see, there is only one parameter in this method. This parameter determines which page will be deleted in PDF file. Now let us see the whole task in detail.

Delete PDF Page for WPF

Step 1: Prepare a template PDF File

I pick a PDF file from my system and it has 73 pages, we can see it below:

Delete PDF Page

Step 2: Download and Install Spire.PDF for WPF

Spire.PDF for WPF is a PDF component applied in WPF applications. It enables developers to create, read, edit and handle PDF files with fast speed. Here you can download Spire.PDF for WPF. After installing it on system, Spire.PDF for WPF will run in evaluation mode. This evaluation mode has no time limit and has no bad influence on performing the task.

Step 3: Start a new Project and Add References

Start your Visual Studio and create a new project in Visual C# WPF Application or VB。NET WPF Application. Here is an example of Visual C# in WPF Application. Please do not forget to add a button in the MainWindow.

We will use Spire.PDF for WPF, so we have to add Spire.Pdf for Wdf.dll and Spire.License.dll in our project in Bin folder. The detail Path is "..\Spire.Pdf\Bin\WPF4.0\ Spire.Pdf.Wpf.dll".

Step 4: Delete PDF Page

Below is the complete code in my project:

[C#]
//Initialize a new instance of PdfDocument
PdfDocument document = new PdfDocument();
//Open a template PDF file
document.LoadFromFile(@"..\Report.pdf");
//Remove the second Page
document.Pages.RemoveAt(1);
//Save the PDF file to disk
document.SaveToFile("DeletePDFPage.pdf");
//Launch the PDF file
System.Diagnostics.Process.Start("DeletePDFPage.pdf ");
[VB.NET]
'Initialize a new instance of PdfDocument
Dim document As New PdfDocument()
'Open a template PDF file
document.LoadFromFile("..\Report.pdf")
'Remove the second Page
document.Pages.RemoveAt(1)
'Save the PDF file to disk
document.SaveToFile("DeletePDFPage.pdf")
'Launch the PDF file
System.Diagnostics.Process.Start("DeletePDFPage.pdf ")

Output PDF File

After executing above code, the second page of the template PDF file has been deleted, and there are only 72 pages in the target PDF file, we can see here:

Delete PDF Page

In this section, I have expounded the solution on how to delete PDF page in WPF application. I wish it can help you and give you some insight on your development.

Spire.PDF for WPF is a PDF component which is designed to meet customers’ requirements on programming with high efficiency and reliability.

We welcome any kind of queries, comments and advices at E-iceblue Forum. Our professionals will be there to give prompt reply.

C#/VB.NET: Merge Word Documents

2022-12-05 08:06:00 Written by Koohji

Long papers or research reports are often completed collaboratively by multiple people. To save time, each person can work on their assigned parts in separate documents and then merge these documents into one after finish editing. Apart from manually copying and pasting content from one Word document to another, this article will demonstrate the following two ways to merge Word documents programmatically using Spire.Doc for .NET .

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Merge Documents by Inserting the Entire File

The Document.InsertTextFromFile() method provided by Spire.Doc for .NET allows merging Word documents by inserting other documents entirely into a document. Using this method, the contents of the inserted document will start from a new page. The detailed steps are as follows:

  • Create a Document instance.
  • Load the original Word document using Document.LoadFromFile() method.
  • Insert another Word document entirely to the original document using Document.InsertTextFromFile() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace MergeWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance 
            Document document = new Document();

            //Load the original Word document
            document.LoadFromFile("Doc1.docx", FileFormat.Docx);

            //Insert another Word document entirely to the original document
            document.InsertTextFromFile("Doc2.docx", FileFormat.Docx);

            //Save the result document
            document.SaveToFile("MergedWord.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Merge Word Documents

Merge Documents by Cloning Contents

If you want to merge documents without starting a new page, you can clone the contents of other documents to add to the end of the original document. The detailed steps are as follows:

  • Load two Word documents.
  • Loop through the second document to get all the sections using Document.Sections property, and then loop through all the sections to get their child objects using Section.Body.ChildObjects property.
  • Get the last section of the first document using Document.LastSection property, and then add the child objects to the last section of the first document using LastSection.Body.ChildObjects.Add() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace MergeWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load two Word documents
            Document doc1 = new Document("Doc1.docx");
            Document doc2 = new Document("Doc2.docx");

            //Loop through the second document to get all the sections
            foreach (Section section in doc2.Sections)
            {

                //Loop through the sections of the second document to get their child objects
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {

                    // Get the last section of the first document
                     Section lastSection = doc1.LastSection;

                    //Add all child objects to the last section of the first document
                    lastSection.Body.ChildObjects.Add(obj.Clone());
                }
            }

            // Save the result document
            doc1.SaveToFile("MergeDocuments.docx", FileFormat.Docx);
        }
    }
} 

C#/VB.NET: Merge Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Word comments can be reviews or thoughts about part of contents or explanations or references of specified phrase, sentence or paragraph given by author. Also, the existing comments can be extracted from document and solution in this guide demonstrates how to extract Word comments and save to TXT file in C# and Visual Basic via Spire.Doc for .NET.

Spire.Doc for .NET, one easy-to-use .NET Word component to preform Word tasks, provides a Comment class to enable users to get comments in Word and paragraphs of comment body. The screenshot below shows the original documents with two comments.

Extract Word Commnet

Download and install Spire.Doc for .NET and follow the steps to extract Word comments. Firstly, initialize a StringBuilder instance to save extracted comments. Secondly, use a foreach statement to get all comments in Word and use another foreach statement to get each paragraph of body of each comment. Then, invoke StringBuilder.AppendLine method to append a copy of comment string followed by default line terminator to the end of the current StringBuilder object. The parameter passed to this method is string value which is comment paragraph text. Thirdly, invoke File.WrtieAllText method to create a new TXT file with comments text as contents. The parameters passed to this method are string path and string contents. Code as following:

[C#]
using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace ExtractComments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document doc = new Document();
            doc.LoadFromFile(@"E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx");

            //Extract Comment
            StringBuilder SB = new StringBuilder();

            foreach(Comment comment in doc.Comments)
            {
                foreach (Paragraph p in comment.Body.Paragraphs)
                {
                    SB.AppendLine(p.Text);
                }
            }

            //Save to TXT File
            File.WriteAllText("CommentExtraction.txt", SB.ToString());
            System.Diagnostics.Process.Start("CommentExtraction.txt");
        }
    }
}

 

[Visual Basic]
Imports System.Text
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace ExtractComments
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim doc As New Document()
            doc.LoadFromFile("E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx")

            'Extract Comment
            Dim SB As New StringBuilder()

            For Each comment As Comment In doc.Comments
                For Each p As Paragraph In comment.Body.Paragraphs
                    SB.AppendLine(p.Text)
                Next p
            Next comment

            'Save to TXT File
            File.WriteAllText("CommentExtraction.txt", SB.ToString())
            System.Diagnostics.Process.Start("CommentExtraction.txt")
        End Sub
    End Class
End Namespace

After debugging, the following result will be presented:

Spire.Doc, a professional Word component, enables developers/programmers to operate Word document, for example, generating, opening, saving and modifying on .NET, WPF and Silverlight applications

page 276