Hello World

This example demonstrates how to write a simple text to a Word document.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;


namespace OfficeComponent.Samples
{
    class HelloWorldExample : WordExampleBase
    {
        public HelloWorldExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public HelloWorldExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            WordDocument document = new WordDocument();

            // Add a new section to the document.
            Section section = document.AddSection();

            // Adding a new paragraph to the section.
            Paragraph paragraph = section.AddParagraph();

            // Insert Text into the paragraph
            paragraph.AppendText("OfficeComponent Word - Hello World!");

            string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
            // Save the document.
            document.Save(fileName, SaveAsFormat);

            // Close the document.
            document.Close();

            // return the generate file path.
            return fileName;
        }
    }

}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;


namespace OfficeComponent.Samples
{
    class HelloWorldExample : WordExampleBase
    {
        public HelloWorldExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public HelloWorldExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            WordDocument document = new WordDocument();

            // Add a new section to the document.
            Section section = document.AddSection();

            // Adding a new paragraph to the section.
            Paragraph paragraph = section.AddParagraph();

            // Insert Text into the paragraph
            paragraph.AppendText("OfficeComponent Word - Hello World!");

            string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
            // Save the document.
            document.Save(fileName, SaveAsFormat);

            // Close the document.
            document.Close();

            // return the generate file path.
            return fileName;
        }
    }

}