Spire.Doc
Thursday, 28 February 2013 03:32

Convert Word to EPub in C#, VB.NET

Written by support iceblue

EPub is one format of electronic book to display contents with the most appropriate reading mode. Therefore, Word document is usually converted to EPub to give readers a more wonderful reading effect. This guide demonstrates a solution to convert Word to EPub in C# and VB.NET easily with Spire.Doc for .NET. The following screenshot shows the converted EPub file from Word.

Word to EPub

Spire.Doc for .NET provides a SaveToFile(String fileName, FileFormat fileFormat) method of Document class to convert Word to EPub or other document format. When invoking this method to convert Word to EPub, extension of parameter String fileName should be .epub and enum FileFormat should be chosen as EPub among options. Download and install Spire.Doc for .NET and follow the code:

[C#]
using Spire.Doc;

namespace WordtoEPUB
{
    class Epub
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\Spire.Doc for .NET.docx");
            //Convert Word to EPub
            document.SaveToFile("ToEpub.epub", FileFormat.EPub);
            System.Diagnostics.Process.Start("ToEpub.epub");
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace WordtoEPUB
    Friend Class Epub
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\Spire.Doc for .NET.docx")
            'Convert Word to EPub
            document.SaveToFile("ToEpub.epub", FileFormat.EPub)
            System.Diagnostics.Process.Start("ToEpub.epub")
        End Sub
    End Class
End Namespace

Spire.Doc, the professional stand-alone component to manipulate MS Word document without automation, enables developers to generate, read, write, modify Word document on their .NET, WPF and Silverlight application.

To merge Word is to collect contents from several documents and then put them into one. Users can have an order to merge Word according to contents of document. This guide focuses on demonstrating the solution to merge Word in C# and VB.NET with Spire.Doc for .NET. In this example, two documents will be merged. In the merged Word, the contents of the second document follow the first one. The following screenshot presents effect after merging.

Merge Word Document

Download and install Spire.Doc for .NET and follow steps to merge Word. At first, initialize two Document instances DocOne and DocTwo from specified documents. Secondly, get each section of DocTwo and invoke Document.Sections.Add method to add a new section in DocOne with parameter section which is sections in DocTwo gotten by invoking Section.Clone() method. Use foreach statement to add all the sections from DocTwo to DocOne to merge. Finally, save the merged document. Code as following:

[C#]
using Spire.Doc;

namespace MergeDocument
{
    class DocxMerge
    {
        static void Main(string[] args)
        {
            //Load Document1 and Document2
            Document DocOne = new Document();
            DocOne.LoadFromFile(@"E:\Work\Document\welcome.docx", FileFormat.Docx);
            Document DocTwo = new Document();
            DocTwo.LoadFromFile(@"E:\Work\Document\New Zealand.docx", FileFormat.Docx);

            //Merge 
            foreach (Section sec in DocTwo.Sections)
            {
                DocOne.Sections.Add(sec.Clone());
            }

            //Save and Launch
            DocOne.SaveToFile("Merge.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Merge.docx");
        }
    }
}
          
[VB.NET]
Imports Spire.Doc

Namespace MergeDocument
    Friend Class DocxMerge
        Shared Sub Main(ByVal args() As String)
            'Load Document1 and Document2
            Dim DocOne As New Document()
            DocOne.LoadFromFile("E:\Work\Document\welcome.docx", FileFormat.Docx)
            Dim DocTwo As New Document()
            DocTwo.LoadFromFile("E:\Work\Document\New Zealand.docx", FileFormat.Docx)

            'Merge 
            For Each sec As Section In DocTwo.Sections
                DocOne.Sections.Add(sec.Clone())
            Next sec

            'Save and Launch
            DocOne.SaveToFile("Merge.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Merge.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, the professional stand-alone component to manipulate MS Word document without automation, enables developers to generate, read, write, modify Word document on their .NET, WPF and Silverlight application.

When copying Word paragraph, users can choose copy text only or copy all elements of paragraph, such as formatting, image, hyperlink etc. Solution in this guide presents how to copy Word paragraph (text and format) from one document to another in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET, a stand-alone .NET Word component, provides a method, Paragraph.Clone() to allow users to copy paragraph. The following screenshot shows the result after copying.

Copy Word Paragraph

Download and install Spire.Doc for .NET and follow the steps to copy Word paragraphs. Firstly, initialize a Document instance doc1 from specified document and a new Document instance doc2. Secondly, initialize two Paragraph instances p1 and p2 which are the title and first paragraph of doc1. Thirdly, instance a new Section instance and two Paragraph instances. Assign the result after invoking Paragraph.Clone() as value for these two instances. Then, invoke Section.Paragraph.Add method with parameter paragraph to add the copied paragraphs in new document. Code as following:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

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

            //Create New doc2
            Document doc2 = new Document();

            //Get Paragraph 1(the Title) and Paragraph 2(body) in Doc1
            Section s = doc1.Sections[0];
            Paragraph p1 = s.Paragraphs[0];
            Paragraph p2 = s.Paragraphs[1]; 

            //Copy p1 and p2 to doc2
            Section s2 = doc2.AddSection();
            Paragraph NewPara1 = (Paragraph)p1.Clone();
            s2.Paragraphs.Add(NewPara1);
            Paragraph NewPara2 = (Paragraph)p2.Clone();
            s2.Paragraphs.Add(NewPara2);

            //Add Watermark
            PictureWatermark WM = new PictureWatermark();
            WM.Picture = Image.FromFile(@"E:\Work\Document\Rose.jpg");
            doc2.Watermark = WM;

            //Save and Launch
            doc2.SaveToFile("copy.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("copy.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing

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

            'Create New doc2
            Dim doc2 As New Document()

            'Get Paragraph 1(the Title) and Paragraph 2(body) in Doc1
            Dim s As Section = doc1.Sections(0)
            Dim p1 As Paragraph = s.Paragraphs(0)
            Dim p2 As Paragraph = s.Paragraphs(1)

            'Copy p1 and p2 to doc2
            Dim s2 As Section = doc2.AddSection()
            Dim NewPara1 As Paragraph = CType(p1.Clone(), Paragraph)
            s2.Paragraphs.Add(NewPara1)
            Dim NewPara2 As Paragraph = CType(p2.Clone(), Paragraph)
            s2.Paragraphs.Add(NewPara2)

            'Add Watermark
            Dim WM As New PictureWatermark()
            WM.Picture = Image.FromFile("E:\Work\Document\Rose.jpg")
            doc2.Watermark = WM

            'Save and Launch
            doc2.SaveToFile("copy.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("copy.docx")
        End Sub
    End Class
End Namespace

Note: Because type of paragraph.Clone() is DocumentObject, cast its type as Paragraph when initializing paragraph instance in new Word document doc2.

Spire.Doc, as professional Word component, is very powerful on fast generating, loading, writing, modifying and saving Word documents in .NET, WPF, Silverlight without Word automation and any third party tools.