DateAndTime.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Word;
using System.Globalization;

namespace DsWordWeb.Demos
{
    // This sample shows how to add simple date and time fields to a Word document.
    // Note that DsWord does not yet support field calculation. But fields' values
    // calculated in code can be supplied for a field, as this sample demonstrates.
    public class DateAndTime
    {
        public GcWordDocument CreateDocx()
        {
            var now = Util.TimeNow();

            GcWordDocument doc = new GcWordDocument();
            var section = doc.Body.Sections.First;

            var p0 = section.GetRange().Paragraphs.Add("DATE field with default formatting: ");
            p0.GetRange().SimpleFields.Add("DATE", now.ToString("d", CultureInfo.GetCultureInfo("en-US")));
            p0.GetRange().Runs.Add("\rTIME field with default formatting: ");
            p0.GetRange().SimpleFields.Add("TIME", now.ToString("t", CultureInfo.GetCultureInfo("en-US")));

            section.GetRange().Paragraphs.Add(
                "The following table demonstrates some custom date/time formats. " +
                "The left column shows the field code, right column contains the actual field using that code.");

            // Add and setup the table to contain the samples:
            var t = section.GetRange().Tables.Add(0, 0);
            t.Style = doc.Styles.Add("Table style 1", StyleType.Table);
            foreach (var border in t.Style.Table.Borders)
            {
                border.LineStyle = LineStyle.Single;
                border.LineWidth = 0.5f;
                border.Color.RGB = Color.Black;
            }
            // Sample DATE formats:
            string[] sampleDateFormats = new string[]
                {
                    @"DATE \@ ""M/d/yyyy""",
                    @"DATE \@ ""dddd, MMMM dd, yyyy""",
                    @"DATE \@ ""MMMM d, yyyy""",
                    @"DATE \@ ""M/d/yy""",
                    @"DATE \@ ""yyyy-MM-dd""",
                    @"DATE \@ ""d-MMM-yy""",
                    @"DATE \@ ""M.d.yyyy""",
                    @"DATE \@ ""MMM. d, yy""",
                    @"DATE \@ ""d MMMM yyyy""",
                    @"DATE \@ ""MMMM yy""",
                    @"DATE \@ ""MMM-yy""",
                    @"DATE \@ ""M/d/yyyy h:mm am/pm""",
                    @"DATE \@ ""M/d/yyyy h:mm:ss am/pm""",
                    @"DATE \@ ""h:mm am/pm""",
                    @"DATE \@ ""h:mm:ss am/pm""",
                    @"DATE \@ ""HH:mm""",
                    @"DATE \@ ""'Today is 'MMMM d, yyyy""",
                };

            // Add sample format strings and corresponding fields with pre-calculated values:
            foreach (string fmt in sampleDateFormats)
            {
                var r = t.Rows.Add(new string[] { fmt });
                // DsWord does not yet support field calculation, but it does allow you
                // to provide values calculated in code, so that's what we do here.
                // We use the fact that most date/time format strings are the same
                // in Word and .NET, but do replace Word's 'am/pm' with .NET 'tt':
                var f = fmt.Substring(@"DATE \@ ".Length).Trim('"').Replace("am/pm", "tt");
                r.Cells.Add().GetRange().Paragraphs.First.GetRange().SimpleFields.Add(fmt, now.ToString(f, CultureInfo.GetCultureInfo("en-US")));
            }

            // Done:
            return doc;
        }
    }
}