Word Image can beautify the document to attract more readers. Generally speaking, images in Word are related to contents. For example, the image will be places of interest if the document focuses on introducing one country. Sometimes, image can describe some of contents more clearly, like using chart to show data changes in a period.

Spire.Doc for .NET, a professional .NET word component to fast generate, open, modify and save Word documents without using MS Office Automation, enables users to insert image in Word and set its size according to page by using C#, VB.NET. This guide introduces an easy method how to insert image via Spire.Doc for .NET.

At first, create new Word document and add section, page for this document. Then, use p.AppendPicture(Image) method to insert image in the new created paragraph. Set height and width property for image to format its size. Download and Install Spire.Doc for .NET. Use the following code to insert image in Word by using C#, VB.NET.

Insert Word Image

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

namespace WordImage
{
    class ImageinWord
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();
            Section s = document.AddSection();
            Paragraph p = s.AddParagraph();

            //Insert Image and Set Its Size
            DocPicture Pic = p.AppendPicture(Image.FromFile(@"E:\Work\Documents\SampleImage\Sample.jpg"));
            Pic.Width = 750;
            Pic.Height = 468;

            //Save and Launch
            document.SaveToFile("Image.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Image.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace WordImage
    Friend Class ImageinWord
        Shared Sub Main(ByVal args() As String)
            'Create Document
            Dim document As New Document()
            Dim s As Section = document.AddSection()
            Dim p As Paragraph = s.AddParagraph()

            'Insert Image and Set Its Size
            Dim Pic As DocPicture = p.AppendPicture(Image.FromFile("E:\Work\Documents\SampleImage\Sample.jpg"))
            Pic.Width = 750
            Pic.Height = 468

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

Spire.Doc, an easy-to-use .NET Word component, enables users to fast generate, open, save and modify Word document on .NET, WPF and Silverlight applications without Word automation and any other third party add-ins.

Published in Image and Shape
Thursday, 16 December 2010 06:35

Edit Word Document in C#, VB.NET

In order to correct wrong spellings or add some new contents in a Word document, users need to edit an existing Word document. This guide demonstrates a solution to edit Word document in C# and VB.NET.

Spire.Doc for .NET, wonderful .NET Word component, offers a Paragraph class, which enables users to edit contents in paragraphs through set its properties. In this example, the title is updated and new text is added in paragraph two (Title is paragraph one). The editing result is shown as following screenshot.

Edit Word Document

Firstly, declare a Paragraph instance and its value is set as paragraph one (title). Set its Text property to update the original contents. Secondly, declare another Paragraph instance and its value is set as Paragraph two. Invoke Paragraph.AppendText method to add new contents for this paragraph. The overload passed to this method is string text. For distinguishing new contents and existing contents, new contents are formatted in this example. Declare a TextRange instance and set its value as new added contents. Set CharacterFormat properties for this TextRange, including FontName, FontSize and TextColor. Download and install Spire.Doc for .NET and follow the code below to edit Word document.

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

namespace EidtWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");

            //Update Text of Title
            Section section = document.Sections[0];
            Paragraph para1 = section.Paragraphs[0];
            para1.Text = "Spire.Doc for .NET Introduction";

            //Add New Text
            Paragraph para2 = section.Paragraphs[1];
            TextRange tr=para2.AppendText("Spire.Doc for .NET is stand-alone"
            +"to enables developers to operate Word witout Microsoft Word installed.");
            tr.CharacterFormat.FontName = "Cataneo BT";
            tr.CharacterFormat.FontSize=12;
            tr.CharacterFormat.TextColor = Color.YellowGreen;
           
            //Save and Launch
            document.SaveToFile("Edit Word.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Edit Word.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace EidtWord
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")

            'Update Text of Title
            Dim section As Section = document.Sections(0)
            Dim para1 As Paragraph = section.Paragraphs(0)
            para1.Text = "Spire.Doc for .NET Introduction"

            'Add New Text
            Dim para2 As Paragraph = section.Paragraphs(1)
            Dim tr As TextRange = para2.AppendText("Spire.Doc for .NET is stand-alone" &
                                                   "to enables developers to operate Word witout Microsoft Word installed.")
            tr.CharacterFormat.FontName = "Cataneo BT"
            tr.CharacterFormat.FontSize = 12
            tr.CharacterFormat.TextColor = Color.YellowGreen

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

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.

Published in Document Operation
Thursday, 09 December 2010 04:10

Solutions to Open Word in C#, VB.NET

No matter what users want to do on Word document, they should open it. This guide demonstrates several solutions to open Word in C# and VB.NET via Spire.Doc for .NET.

Open Existing Word

Spire.Doc for .NET provides a Document(String) constructor to enable users to initialize a new instance of Document class from the specified existing document.

[C#]
  Document document = new Document(@"E:\Work\Documents\Spire.Doc for .NET.docx");
[VB.NET]
Dim document As New Document("E:\Work\Documents\Spire.Doc for .NET.docx")

Spire.Doc for .NET also provides Document.LoadFromFile(String) method of Document class to open a Word document. The Word document can be .doc(Word 97-2003), .docx(Word 2007 and 2010) and .docm(Word with macro).

[C#]
  Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\Spire.Doc for .NET.docx");
[VB.NET]
 Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\Spire.Doc for .NET.docx")
          

Open Word in Read Mode

Spire.Doc for .NET provides Document.LoadFromFileInReadMode(String, FileFormat) method of Document class to load Word in Read-Only mode.

[C#]
            Document document = new Document();
            document.LoadFromFileInReadMode(@"E:\Work\Documents\Spire.Doc for .NET.docx",FileFormat.Docx);
[VB.NET]
            Dim document As New Document()
            document.LoadFromFileInReadMode("E:\Work\Documents\Spire.Doc for .NET.docx", FileFormat.Docx)

Load Word from Stream

Spire.Doc for .NET provides the constructor Document(Stream) to initialize a new instance of Document class from specified data stream and the method Document.LoadFromStream(Stream, FileFormat) to open document from Stream in XML or Microsoft Word document.

[C#]
            Stream stream = File.OpenRead(@"E:\Work\Documents\Spire.Doc for .NET.docx");
            Document document = new Document(stream);
OR
            Stream stream = File.OpenRead(@"E:\Work\Documents\Spire.Doc for .NET.docx"); 
            Document document = new Document();
            document.LoadFromStream(stream, FileFormat.Docx);
[VB.NET]
            Dim stream As Stream = File.OpenRead("E:\Work\Documents\Spire.Doc for .NET.docx")
            Dim document As New Document(stream)
OR
            Dim stream As Stream = File.OpenRead("E:\Work\Documents\Spire.Doc for .NET.docx")
            Dim document As New Document()
            document.LoadFromStream(stream, FileFormat.Docx)

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.

Published in Document Operation
Thursday, 21 October 2010 14:19

Word Page Setup in C#, VB.NET

The sample demonstrates how to work with Word page setup.

Published in Setup
Thursday, 21 October 2010 14:16

Word document setup in C#, VB.NET

The sample demonstrates how to set document properties.

Published in Setup
Thursday, 21 October 2010 13:51

Word merge event handler in C#, VB.NET

The sample demonstrates how to handle merge event.

Published in Mail Merge
Thursday, 21 October 2010 13:47

Word to pdf in C#, VB.NET

The sample demonstrates how to export doc document to PDF file.

Published in Convertors
Thursday, 21 October 2010 13:44

Word to xml in C#, VB.NET

The sample demonstrates how to export doc document to XML file.

 

Published in Convertors
Thursday, 21 October 2010 13:34

Word to Tiff image in C#, VB.NET

The sample demonstrates how to export doc document to TIFF image.

Published in Convertors
Thursday, 21 October 2010 13:25

Word table in C#, VB.NET

The sample demonstrates how to create table in word document.

Published in Context
Page 3 of 4