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
Saturday, 04 December 2010 10:35

Licensing

Each product of e-iceblue provides a trial version, every registered user can download them from our site for free. The trial version product will add an extra sheet (in Spire.XLS) or paragraph (in Spire.Doc) with the Evaluation Warning to the result file. From Spire.Doc v3.6.0/Spire.XLS v5.8.0/Spire.Office 1.4.0, We deprecated the old username-key registeration method and use a new license file to instead. When you purchase a license, you will get a license file from us. After you apply it, the Evaluation Warning will disappear.

This section will show you what is the license file and how to apply the license file. It includes following topics:

License File Introduction

The license file is an XML format file that contains details such as the username&email&organization of the purchaser, licensing date, product name, product version, the number of licensed developer, the number of licensed site and so on. The license file is digitally signed, so do not modify it anyway.

You need to apply it before performing any operations with our products, but it's only required once to apply the license file in an application or process.

How to Apply the License File

Performing any operation with our products will lead the license module to check whether the license has been loaded. If not, the license module will try to load it. The license can be loaded implicitly or explicitly from a file, stream or an embedded resource, implicit is default.

Note: Whether implicity or explicity, you must apply the license file before you call any of our products.

Implicit Loading

In this mode, the license module will try to search the license file in the following locations:

  • The folder that contains the entry assembly (your assembly named .exe) in runtime.
  • An embedded resource in the assembly that calls our product.
  • The folder that contains the assembly of our product (for example: Spire.Doc.dll, Spire.XLS.dll) referenced by your assembly in runtime.
  • The folder that contains the assembly that calls our product in runtime.

When you get the license file from us, the default name of it is license.elic.xml. You can put it in any location aforementioned. The license module will load it automatically in your application. You can also change the license file name. If you do that, it's required to tell the license module the new file name before you perform any operation with our products, for example:

[C#]
//Tell the license module that you changed the license file name.
Spire.License.LicenseProvider.SetLicenseFileName("your-license-file-name");
[VB.NET]
//Tell the license module that you changed the license file name.
Spire.License.LicenseProvider.SetLicenseFileName("your-license-file-name");

Note: The class Spire.License.LicenseProvider is defined in Spire.License.dll, which is installed to the same directory as Spire.Doc.dll/Spire.XLS.dll. It's required to reference Spire.License.dll in your project if you use the class Spire.License.LicenseProvider in your code.

You can also get the license file name by which the license module search the license, for example:

[C#]
//To get the default license file name.
String fileName = Spire.License.LicenseProvider.GetLicenseFileName();
[VB.NET]
//To get the default license file name.
Dim fileName As String = Spire.License.LicenseProvider.GetLicenseFileName()

Explicit Loading

In this mode, the license module will try to load the license from a specified file or stream you provide.

Explicitly specify the license file by a full file name.

[C#]
//Specify the license file by a full file name.
Spire.License.LicenseProvider.SetLicenseFileFullPath(@"D:\myApp\license.lic");
[VB.NET]
//Specify the license file by a full file name.
Spire.License.LicenseProvider.SetLicenseFileFullPath("D:\myApp\license.lic")

Explicitly specify the license file by a FileInfo object.

[C#]
//Specify the license file by a FileInfo object.
FileInfo licenseFile = new FileInfo(@"D:\myApp\license.lic");
Spire.License.LicenseProvider.SetLicenseFile(licenseFile);
[VB.NET]
//Specify the license file by a FileInfo object.
Dim licenseFile As New FileInfo("D:\myApp\license.lic")
Spire.License.LicenseProvider.SetLicenseFile(licenseFile)

Provide a license data stream.

[C#]
//Specify the license by a license data stream.
Stream stream = File.OpenRead(@"D:\myApp\license.lic");
Spire.License.LicenseProvider.SetLicenseFileStream(stream);
[VB.NET]
//Specify the license by a license data stream.
Dim stream As Stream = File.OpenRead("D:\myApp\license.lic")
Spire.License.LicenseProvider.SetLicenseFileStream(stream)

See also: How to Apply the License by license key

How to Include the License File as an Embedded Resource

Including the license file as an embedded resource into one of the assemblies that calls our products is a good idea. It will make your release and deployment become easier. You don't need to worry about the loss of it any longer. To include the license file as an embedded resource in Visual Studio, perform the following steps:

  • In the Solution Explorer, right-click your project and click Add | Add Existing Item... menu.
  • Find your license file in the opend file browser dialog, then click the Add button to add it into your project.
  • Select the file in the Solution Explorer and set Build Action to Embedded Resource in the Properties window.
  • If your license file name is not the default file name license.elic.xml, invoke Spire.License.LicenseProvider.SetLicenseFileName to tell the real name to the license module in your code.

Embedded License

See also: How to Apply the License by license key

How to Apply the License File in a Web Site

If you want to apply the license file in a web site, just copy it into the folder Bin which contains the referenced assemblies of your web site.

Copy License

See also: How to Apply the License by license key

How to Apply the License in Silverlight

SeeHow to Apply the License by license key

How to Apply the License by license key

Sometimes, your application could not read the license file because of lack of permission or other reason. In this case, you can invoke the method Spire.License.LicenseProvider.SetLicenseKey(String key) to apply your license. The parameter key is the value of the Key attribute of the element License of your license xml file. To make sure that apply the license before any operation with our products, we recommend invoking this method in the top of your entry method.

[C#]
//Register the license key.
Spire.License.LicenseProvider.SetLicenseKey("your license key");
[VB.NET]
'Register the license key.
Spire.License.LicenseProvider.SetLicenseKey("your license key")
  • If your application is WinForm Application or Console Application, this code above should be added into the Main method.
  • If your application is Web Application, you need to add Global.asax into your project and add this method code above into the Application_Start method.
  • If your application is Silverlight, this code above should be added into the App.Application_Startup method. Here we suppose that the class App is SilverlightAppEntry.

How to Apply two licenses or more

Sometimes, you need to apply two licenses or more in your application. In this case, there are two methods you can use.

If you apply your 2 licenses or more by license key, you can invoke the following method in the top of your entry method.

[C#]
Spire.License.LicenseProvider.SetLicenseKey("license key of license1");
Spire.License.LicenseProvider.LoadLicense();  
Spire.License.LicenseProvider.SetLicenseKey("license key of license2");  
Spire.License.LicenseProvider.LoadLicense();

If you apply them by license file, you can invoke the following method:

[C#]
Spire.License.LicenseProvider.SetLicenseFileName("license1.elic.xml");  
Spire.License.LicenseProvider.LoadLicense();  
Spire.License.LicenseProvider.SetLicenseFileName("license2.elic.xml");  
Spire.License.LicenseProvider.LoadLicense();

Note: The class Spire.License.LicenseProvider is defined in Spire.License.dll, which is installed to the same directory as Spire.Doc.dll/Spire.XLS.dll. It's required to reference Spire.License.dll in your project if you use the class Spire.License.LicenseProvider in your code.

Published in Licensing
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
Page 3 of 4