Spire.Doc for .NET

C#/VB.NET: Convert Word to EPUB

2022-07-22 03:32:00 Written by Koohji

EPUB is a standard file format for publishing eBooks or other electronic documents. The content in an EPUB file is reflowable, which means that the content automatically adjusts itself to fit the screen it is being displayed on. People who want to publish their eBooks may need to convert their works stored in Word documents to EPUB files. In this article, you will learn how to programmatically achieve this task 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

Convert Word to EPUB

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Save the document to EPUB using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

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

            //Load a sample Word document
            document.LoadFromFile("demo.docx");

            //Convert the Word document to EPUB
            document.SaveToFile("ToEpub.epub", FileFormat.EPub);
        }
    }
}

C#/VB.NET: Convert Word to EPUB

Convert Word to EPUB with a Cover Image

The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Create a DocPicture instance, and then load an image using DocPicture.LoadImage() method.
  • Save the Word document to EPUB with cover image using Document.SaveToEpub(String, DocPicture) method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;

namespace ConvertWordToEpubWithCoverImage
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a Document instance
            Document doc = new Document();

            //Load a sample Word document
            doc.LoadFromFile("demo.docx");

            //Create a DocPicture instance
            DocPicture picture = new DocPicture(doc);

            //Load an image
            picture.LoadImage(Image.FromFile("CoverImage.png"));

            //Save the Word document to EPUB with cover image
            doc.SaveToEpub("ToEpubWithCoverImage.epub", picture);
        }
    }
}

C#/VB.NET: Convert Word to EPUB

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.

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 40