// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
    // This sample shows how to add simple date and time fields to a Word document.
    // Note that GcWord 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 = DateTime.Now;

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

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

            var p1 = 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 });
                // GcWord does not yet support field calculation, but it does allow
                // 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));

            // Done:
            return doc;