Example #1
0
        internal override void PrepareForSave()
        {
            base.PrepareForSave();
            //set or update the default appearance stream
            string textAppearanceStream = string.Format("/{0} {1:0.##} Tf", Font.FamilyName, Font.Size);

            string colorStream = string.Empty;

            switch (ForeColor.ColorSpace)
            {
            case XColorSpace.Rgb:
                colorStream = string.Format("{0:0.#} {1:0.#} {2:0.#} rg", ForeColor.R / 255d, ForeColor.G / 255d, ForeColor.B / 255);
                break;

            case XColorSpace.GrayScale:
                colorStream = string.Format("{0:0.#} g", ForeColor.GS);
                break;
            }

            var mk = Elements.GetDictionary(PdfWidgetAnnotation.Keys.MK);


            if (mk == null && (BorderColor != XColor.Empty || BackColor != XColor.Empty))
            {
                mk = new PdfDictionary(_document);
                Elements.SetObject(PdfWidgetAnnotation.Keys.MK, mk);
            }

            if (BorderColor != XColor.Empty)
            {
                var bc = BorderColor.ToArray();
                mk.Elements.SetObject("/BC", bc);
            }
            else if (mk != null && mk.Elements.ContainsKey("/BC"))
            {
                mk.Elements.Remove("/BC");
            }

            if (BackColor != XColor.Empty)
            {
                var bg = BackColor.ToArray();
                mk.Elements.SetObject("/BG", bg);
            }
            else if (mk != null && mk.Elements.ContainsKey("/BG"))
            {
                mk.Elements.Remove("/BG");
            }

            Elements.SetString(Keys.DA, textAppearanceStream + " " + colorStream);
        }