SetFieldFormat.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.AcroForms;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Pdf.Actions;

namespace DsPdfWeb.Demos
{
    // This example shows how to use the helper methods SetPercentValue, SetNumberValue,
    // SetDateValue, SetTimeValue, SetSpecialFormatValue on TextField, CombTextField and ComboBoxField
    // to set the fields' format/formatted values.
    // Note that setting any property affecting the appearance of a field causes any formatting
    // set using these methods to be lost, so these methods should be called after any adjustments
    // to the field's appearance are done.
    public class SetFieldFormat
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var p = doc.NewPage();
            var g = p.Graphics;

            g.DrawString("SetPercentFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 10));
            AddPercentFormat(p, new RectangleF(10, 40, 60, 20), 0.2f, 2, TextField.NumberSeparatorStyle.Comma);
            AddPercentFormat(p, new RectangleF(80, 40, 60, 20), 1, 0, TextField.NumberSeparatorStyle.CommaDot);
            AddPercentFormat(p, new RectangleF(150, 40, 60, 20), -0.1f, 3, TextField.NumberSeparatorStyle.DotComma);
            AddPercentFormat(p, new RectangleF(220, 40, 60, 20), 2f, 2, TextField.NumberSeparatorStyle.Dot);
            AddPercentFormat(p, new RectangleF(290, 40, 60, 20), 0.5f, 2, TextField.NumberSeparatorStyle.ApostropheDot);

            g.DrawString("SetNumberFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 70));
            AddNumberFormat(p, new RectangleF(10, 100, 60, 20), 12345.67f, 2, TextField.NumberSeparatorStyle.Comma,
                TextField.NumberNegativeStyle.None, null, TextField.CurrencySymbolStyle.BeforeWithSpace);
            AddNumberFormat(p, new RectangleF(80, 100, 60, 20), -12.7f, 0, TextField.NumberSeparatorStyle.CommaDot,
                TextField.NumberNegativeStyle.UseRedText, "R", TextField.CurrencySymbolStyle.BeforeWithSpace);
            AddNumberFormat(p, new RectangleF(150, 100, 60, 20), 0.123f, 3, TextField.NumberSeparatorStyle.DotComma,
                TextField.NumberNegativeStyle.ShowParentheses, "\u20ac", TextField.CurrencySymbolStyle.AfterWithSpace);
            AddNumberFormat(p, new RectangleF(220, 100, 60, 20), 12345, 2, TextField.NumberSeparatorStyle.Dot,
                TextField.NumberNegativeStyle.ShowParentheses, "TL", TextField.CurrencySymbolStyle.AfterNoSpace);
            AddNumberFormat(p, new RectangleF(290, 100, 60, 20), -5674.344f, 2, TextField.NumberSeparatorStyle.ApostropheDot,
                TextField.NumberNegativeStyle.ShowParentheses | TextField.NumberNegativeStyle.UseRedText, "TL", TextField.CurrencySymbolStyle.AfterWithSpace);

            g.DrawString("SetDateFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 130));
            AddDateFormat(p, new RectangleF(10, 160, 60, 20), Common.Util.TimeNow(), "dd-mm-yyyy");
            AddDateFormat(p, new RectangleF(80, 160, 60, 20), Common.Util.TimeNow(), "d-m-yy");
            AddDateFormat(p, new RectangleF(150, 160, 60, 20), Common.Util.TimeNow(), "yyyy/mmmm/dd");

            g.DrawString("SetTimeFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 200));
            var dt = new DateTime(2000, 1, 1, 1, 2, 3);
            AddTimeFormat(p, new RectangleF(10, 230, 60, 20), dt, "HH:MM");
            AddTimeFormat(p, new RectangleF(80, 230, 60, 20), dt, "h:MM tt");
            AddTimeFormat(p, new RectangleF(150, 230, 60, 20), dt, "HH:MM:ss");

            g.DrawString("Special Format", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 270));
            AddSpecialFormat(p, new RectangleF(10, 300, 60, 20), "35004", TextField.SpecialFormat.ZipCode);
            AddSpecialFormat(p, new RectangleF(80, 300, 60, 20), "84606-6580", TextField.SpecialFormat.ZipCode4);
            AddSpecialFormat(p, new RectangleF(150, 300, 60, 20), "(123) 456-7890", TextField.SpecialFormat.Phone);
            AddSpecialFormat(p, new RectangleF(220, 300, 60, 20), "123-45-6789", TextField.SpecialFormat.SSN);

            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }

        private static TextField AddPercentFormat(Page p,
            RectangleF rect,
            float v,
            int decimalPlaces,
            TextField.NumberSeparatorStyle separatorStyle)
        {
            var result = new TextField();
            p.Doc.AcroForm.Fields.Add(result);

            result.Widget.Page = p;
            result.Widget.Rect = rect;
            result.Widget.Border.Width = 1;
            result.SetPercentValue(v, decimalPlaces, separatorStyle);
            return result;
        }

        private static TextField AddNumberFormat(Page p,
            RectangleF rect,
            float v,
            int decimalPlaces,
            TextField.NumberSeparatorStyle separatorStyle,
            TextField.NumberNegativeStyle negativeStyle,
            string currencySymbol,
            TextField.CurrencySymbolStyle currencySymbolStyle)
        {
            var result = new TextField();
            p.Doc.AcroForm.Fields.Add(result);

            result.Widget.Page = p;
            result.Widget.Rect = rect;
            result.Widget.Border.Width = 1;
            result.Widget.Justification = VariableTextJustification.RightJustified;
            result.SetNumberValue(v, decimalPlaces, separatorStyle, negativeStyle, currencySymbol, currencySymbolStyle);
            return result;
        }

        private static TextField AddDateFormat(Page p,
            RectangleF rect,
            DateTime v,
            string format)
        {
            var result = new TextField();
            p.Doc.AcroForm.Fields.Add(result);

            result.Widget.Page = p;
            result.Widget.Rect = rect;
            result.Widget.Border.Width = 1;
            result.SetDateValue(v, format);
            return result;
        }

        private static TextField AddTimeFormat(Page p,
            RectangleF rect,
            DateTime v,
            string format)
        {
            var result = new TextField();
            p.Doc.AcroForm.Fields.Add(result);

            result.Widget.Page = p;
            result.Widget.Rect = rect;
            result.Widget.Border.Width = 1;
            result.SetTimeValue(v, format);
            return result;
        }

        private static TextField AddSpecialFormat(Page p,
            RectangleF rect,
            string v,
            TextField.SpecialFormat format)
        {
            var result = new TextField();
            p.Doc.AcroForm.Fields.Add(result);

            result.Widget.Page = p;
            result.Widget.Rect = rect;
            result.Widget.Border.Width = 1;
            result.SetSpecialFormatValue(v, format);
            return result;
        }
    }
}