Following example can be found and executed in GemBox.Document Sample Explorer under category 'Basic Features' and name 'Writing'.
Example
Creating document content programmatically with GemBox.Document is easy and straightforward by composing document elements through constructors.
GemBox.Document supports writing document to a file or a stream. Document file format is specified through SaveOptions derived classes.
This sample shows how to programmatically create new document, how to use non-Latin Unicode characters and how to write a document to a file.
using System; using System.Linq; using System.Text; using GemBox.Document; using GemBox.Document.Tables; class Sample { [STAThread] static void Main(string[] args) { // If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); // If sample exceeds Free version limitations then continue as trial version. ComponentInfo.FreeLimitReached += (sender, e) => e.ContinueAsTrial = true; DocumentModel document = new DocumentModel(); document.Sections.Add( new Section(document, new Paragraph(document, new Run(document, "English: Hello"), new SpecialCharacter(document, SpecialCharacterType.LineBreak), new Run(document, "Russian: "), new Run(document, new string(new char[] { '\u0417', '\u0434', '\u0440', '\u0430', '\u0432', '\u0441', '\u0442', '\u0432', '\u0443', '\u0439', '\u0442', '\u0435' })), new SpecialCharacter(document, SpecialCharacterType.LineBreak), new Run(document, "Chinese: "), new Run(document, new string(new char[] { '\u4f60', '\u597d' }))), new Paragraph(document, "In order to see Russian and Chinese characters you need to have appropriate fonts on your machine."))); string outPath = @"C:\Users\User\AppData\Local\GBDSampleExplorer\Writing.docx"; document.Save(outPath, DocxSaveOptions.DocxDefault); } }
Imports System Imports System.Linq Imports System.Text Imports GemBox.Document Imports GemBox.Document.Tables Module Samples Sub Main() ' If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY") ' If sample exceeds Free version limitations then continue as trial version. AddHandler ComponentInfo.FreeLimitReached, Sub(sender, e) e.ContinueAsTrial = True Dim document As DocumentModel = New DocumentModel document.Sections.Add( New Section(document, New Paragraph(document, New Run(document, "English: Hello"), New SpecialCharacter(document, SpecialCharacterType.LineBreak), New Run(document, "Russian: "), New Run(document, New String(New Char() {ChrW(&H417), ChrW(&H434), ChrW(&H440), ChrW(&H430), ChrW(&H432), ChrW(&H441), ChrW(&H442), ChrW(&H432), ChrW(&H443), ChrW(&H439), ChrW(&H442), ChrW(&H435)})), New SpecialCharacter(document, SpecialCharacterType.LineBreak), New Run(document, "Chinese: "), New Run(document, New String(New Char() {ChrW(&H4F60), ChrW(&H597D)}))), New Paragraph(document, "In order to see Russian and Chinese characters you need to have appropriate fonts on your machine."))) Dim outPath As String = "C:\Users\User\AppData\Local\GBDSampleExplorer\Writing.docx" document.Save(outPath, DocxSaveOptions.DocxDefault) End Sub End Module