DefaultStyle.cs :  » PDF » PDF-Clown » it » stefanochizzolini » clown » documents » interaction » forms » styles » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » PDF Clown 
PDF Clown » it » stefanochizzolini » clown » documents » interaction » forms » styles » DefaultStyle.cs
/*
  Copyright 2008 Stefano Chizzolini. http://clown.stefanochizzolini.it

  Contributors:
    * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it)

  This file should be part of the source code distribution of "PDF Clown library"
  (the Program): see the accompanying README files for more info.

  This Program is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later version.

  This Program is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY, either expressed or implied; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.

  You should have received a copy of the GNU General Public License along with this
  Program (see README files); if not, go to the GNU website (http://www.gnu.org/).

  Redistribution and use, with or without modification, are permitted provided that such
  redistributions retain the above copyright notice, license and disclaimer, along with
  this list of conditions.
*/

using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents.colorSpaces;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.fonts;
using it.stefanochizzolini.clown.documents.contents.xObjects;
using it.stefanochizzolini.clown.documents.interaction.annotations;
using it.stefanochizzolini.clown.objects;

using System;
using System.Drawing;

namespace it.stefanochizzolini.clown.documents.interaction.forms.styles{
  /**
    <summary>Default field appearance style.</summary>
  */
  public class DefaultStyle
    : FieldStyle
  {
    #region dynamic
    #region constructors
    public DefaultStyle(
      )
    {
      BackColor = new DeviceRGBColor(.9,.9,.9);
    }
    #endregion

    #region interface
    #region public
    public override void Apply(
      Field field
      )
    {
      if(field is PushButton)
      {Apply((PushButton)field);}
      else if(field is CheckBox)
      {Apply((CheckBox)field);}
      else if(field is TextField)
      {Apply((TextField)field);}
      else if(field is ComboBox)
      {Apply((ComboBox)field);}
      else if(field is ListBox)
      {Apply((ListBox)field);}
      else if(field is RadioButton)
      {Apply((RadioButton)field);}
    }

    private void Apply(
      CheckBox field
      )
    {
      Document document = field.Document;
      foreach(Widget widget in field.Widgets)
      {
        {
          PdfDictionary widgetDataObject = widget.BaseDataObject;
          widgetDataObject[PdfName.DA] = new PdfString("/ZaDb 0 Tf 0 0 0 rg");
          widgetDataObject[PdfName.MK] = new PdfDictionary(
            new PdfName[]
            {
              PdfName.BG,
              PdfName.BC,
              PdfName.CA
            },
            new PdfDirectObject[]
            {
              new PdfArray(new PdfDirectObject[]{new PdfReal(0.9412),new PdfReal(0.9412),new PdfReal(0.9412)}),
              new PdfArray(new PdfDirectObject[]{new PdfInteger(0),new PdfInteger(0),new PdfInteger(0)}),
              new PdfString("4")
            }
            );
          widgetDataObject[PdfName.BS] = new PdfDictionary(
            new PdfName[]
            {
              PdfName.W,
              PdfName.S
            },
            new PdfDirectObject[]
            {
              new PdfReal(0.8),
              PdfName.S
            }
            );
          widgetDataObject[PdfName.H] = PdfName.P;
        }

        Appearance appearance = widget.Appearance;
        if(appearance == null)
        {widget.Appearance = appearance = new Appearance(document);}

        AppearanceStates normalAppearance = appearance.Normal;
        FormXObject onState = new FormXObject(document);
        normalAppearance[PdfName.Yes] = onState;

  //TODO:verify!!!
  //   appearance.getRollover().put(PdfName.Yes,onState);
  //   appearance.getDown().put(PdfName.Yes,onState);
  //   appearance.getRollover().put(PdfName.Off,offState);
  //   appearance.getDown().put(PdfName.Off,offState);

        SizeF size = widget.Size;
        RectangleF frame = new RectangleF(0,0,size.Width,size.Height);
        {
          onState.Size = new Size((int)size.Width,(int)size.Height);

          PrimitiveFilter builder = new PrimitiveFilter(onState);

          builder.BeginLocalState();
          builder.SetFillColor(BackColor);
          builder.SetStrokeColor(ForeColor);
          builder.DrawRectangle(frame);
          builder.FillStroke();
          builder.End();

          BlockFilter blockFilter = new BlockFilter(builder);
          blockFilter.Begin(frame,AlignmentXEnum.Center,AlignmentYEnum.Middle);
          builder.SetFillColor(ForeColor);
          builder.SetFont(
            new StandardType1Font(
              document,
              StandardType1Font.FamilyNameEnum.ZapfDingbats,
              true,
              false
              ),
            size.Height * 0.8
            );
          blockFilter.ShowText(new String(new char[]{CheckSymbol}));
          blockFilter.End();

          builder.Flush();
        }

        FormXObject offState = new FormXObject(document);
        normalAppearance[PdfName.Off] = offState;
        {
          offState.Size = new Size((int)size.Width,(int)size.Height);

          PrimitiveFilter builder = new PrimitiveFilter(offState);

          builder.BeginLocalState();
          builder.SetFillColor(BackColor);
          builder.SetStrokeColor(ForeColor);
          builder.DrawRectangle(frame);
          builder.FillStroke();
          builder.End();

          builder.Flush();
        }
      }
    }

    private void Apply(
      RadioButton field
      )
    {
      Document document = field.Document;
      foreach(Widget widget in field.Widgets)
      {
        {
          PdfDictionary widgetDataObject = widget.BaseDataObject;
          widgetDataObject[PdfName.DA] = new PdfString("/ZaDb 0 Tf 0 0 0 rg");
          widgetDataObject[PdfName.MK] = new PdfDictionary(
            new PdfName[]
            {
              PdfName.BG,
              PdfName.BC,
              PdfName.CA
            },
            new PdfDirectObject[]
            {
              new PdfArray(new PdfDirectObject[]{new PdfReal(0.9412),new PdfReal(0.9412),new PdfReal(0.9412)}),
              new PdfArray(new PdfDirectObject[]{new PdfInteger(0),new PdfInteger(0),new PdfInteger(0)}),
              new PdfString("l")
            }
            );
          widgetDataObject[PdfName.BS] = new PdfDictionary(
            new PdfName[]
            {
              PdfName.W,
              PdfName.S
            },
            new PdfDirectObject[]
            {
              new PdfReal(0.8),
              PdfName.S
            }
            );
          widgetDataObject[PdfName.H] = PdfName.P;
        }

        Appearance appearance = widget.Appearance;
        if(appearance == null)
        {widget.Appearance = appearance = new Appearance(document);}

        AppearanceStates normalAppearance = appearance.Normal;
        FormXObject onState = normalAppearance[new PdfName(((DualWidget)widget).WidgetName)];

  //TODO:verify!!!
  //   appearance.getRollover().put(new PdfName(...),onState);
  //   appearance.getDown().put(new PdfName(...),onState);
  //   appearance.getRollover().put(PdfName.Off,offState);
  //   appearance.getDown().put(PdfName.Off,offState);

        SizeF size = widget.Size;
        RectangleF frame = new RectangleF(1,1,size.Width-2,size.Height-2);
        {
          onState.Size = new Size((int)size.Width,(int)size.Height);;

          PrimitiveFilter builder = new PrimitiveFilter(onState);

          builder.BeginLocalState();
          builder.SetFillColor(BackColor);
          builder.SetStrokeColor(ForeColor);
          builder.DrawEllipse(frame);
          builder.FillStroke();
          builder.End();

          BlockFilter blockFilter = new BlockFilter(builder);
          blockFilter.Begin(frame,AlignmentXEnum.Center,AlignmentYEnum.Middle);
          builder.SetFillColor(ForeColor);
          builder.SetFont(
            new StandardType1Font(
              document,
              StandardType1Font.FamilyNameEnum.ZapfDingbats,
              true,
              false
              ),
            size.Height * 0.8
            );
          blockFilter.ShowText(new String(new char[]{RadioSymbol}));
          blockFilter.End();

          builder.Flush();
        }

        FormXObject offState = new FormXObject(document);
        normalAppearance[PdfName.Off] = offState;
        {
          offState.Size = new Size((int)size.Width,(int)size.Height);;

          PrimitiveFilter builder = new PrimitiveFilter(offState);

          builder.BeginLocalState();
          builder.SetFillColor(BackColor);
          builder.SetStrokeColor(ForeColor);
          builder.DrawEllipse(frame);
          builder.FillStroke();
          builder.End();

          builder.Flush();
        }
      }
    }

    private void Apply(
      PushButton field
      )
    {
      Document document = field.Document;
      Widget widget = field.Widgets[0];

      Appearance appearance = widget.Appearance;
      if(appearance == null)
      {widget.Appearance = appearance = new Appearance(document);}

      FormXObject normalAppearanceState = new FormXObject(document);
      {
        SizeF size = widget.Size;
        normalAppearanceState.Size = new Size((int)size.Width,(int)size.Height);;
        PrimitiveFilter builder = new PrimitiveFilter(normalAppearanceState);

        builder.BeginLocalState();
        double lineWidth = 1;
        builder.SetLineWidth(lineWidth);
        builder.SetFillColor(BackColor);
        builder.SetStrokeColor(ForeColor);
        RectangleF frame = new RectangleF((float)lineWidth/2,(float)lineWidth/2,(float)(size.Width-lineWidth),(float)(size.Height-lineWidth));
        builder.DrawRectangle(frame,5);
        builder.FillStroke();
        builder.End();

        string caption = (string)field.Value;
        if(caption != null)
        {
          BlockFilter blockFilter = new BlockFilter(builder);
          blockFilter.Begin(frame,AlignmentXEnum.Center,AlignmentYEnum.Middle);
          builder.SetFillColor(ForeColor);
          builder.SetFont(
            new StandardType1Font(
              document,
              StandardType1Font.FamilyNameEnum.Helvetica,
              true,
              false
              ),
            size.Height * 0.5
            );
          blockFilter.ShowText(caption);
          blockFilter.End();
        }

        builder.Flush();
      }
      appearance.Normal[null] = normalAppearanceState;
    }

    private void Apply(
      TextField field
      )
    {
      Document document = field.Document;
      Widget widget = field.Widgets[0];

      Appearance appearance = widget.Appearance;
      if(appearance == null)
      {widget.Appearance = appearance = new Appearance(document);}

      widget.BaseDataObject[PdfName.DA] = new PdfString("/Helv " + FontSize + " Tf 0 0 0 rg");

      FormXObject normalAppearanceState = new FormXObject(document);
      {
        SizeF size = widget.Size;
        normalAppearanceState.Size = new Size((int)size.Width,(int)size.Height);;
        PrimitiveFilter builder = new PrimitiveFilter(normalAppearanceState);

        builder.BeginLocalState();
        double lineWidth = 1;
        builder.SetLineWidth(lineWidth);
        builder.SetFillColor(BackColor);
        builder.SetStrokeColor(ForeColor);
        RectangleF frame = new RectangleF((float)lineWidth/2,(float)lineWidth/2,(float)(size.Width-lineWidth),(float)(size.Height-lineWidth));
        builder.DrawRectangle(frame,5);
        builder.FillStroke();
        builder.End();

        builder.BeginMarkedContent(PdfName.Tx);
        builder.SetFont(
          new StandardType1Font(
            document,
            StandardType1Font.FamilyNameEnum.Helvetica,
            false,
            false
            ),
          FontSize
          );
        builder.ShowText(
          (string)field.Value,
          new PointF(0,size.Height/2),
          AlignmentXEnum.Left,
          AlignmentYEnum.Middle,
          0
          );
        builder.End();

        builder.Flush();
      }
      appearance.Normal[null] = normalAppearanceState;
    }

    private void Apply(
      ComboBox field
      )
    {
      Document document = field.Document;
      Widget widget = field.Widgets[0];

      Appearance appearance = widget.Appearance;
      if(appearance == null)
      {widget.Appearance = appearance = new Appearance(document);}

      widget.BaseDataObject[PdfName.DA] = new PdfString("/Helv " + FontSize + " Tf 0 0 0 rg");

      FormXObject normalAppearanceState = new FormXObject(document);
      {
        SizeF size = widget.Size;
        normalAppearanceState.Size = new Size((int)size.Width,(int)size.Height);;
        PrimitiveFilter builder = new PrimitiveFilter(normalAppearanceState);

        builder.BeginLocalState();
        double lineWidth = 1;
        builder.SetLineWidth(lineWidth);
        builder.SetFillColor(BackColor);
        builder.SetStrokeColor(ForeColor);
        RectangleF frame = new RectangleF((float)lineWidth/2,(float)lineWidth/2,(float)(size.Width-lineWidth),(float)(size.Height-lineWidth));
        builder.DrawRectangle(frame,5);
        builder.FillStroke();
        builder.End();

        builder.BeginMarkedContent(PdfName.Tx);
        builder.SetFont(
          new StandardType1Font(
            document,
            StandardType1Font.FamilyNameEnum.Helvetica,
            false,
            false
            ),
          FontSize
          );
        builder.ShowText(
          (string)field.Value,
          new PointF(0,size.Height/2),
          AlignmentXEnum.Left,
          AlignmentYEnum.Middle,
          0
          );
        builder.End();

        builder.Flush();
      }
      appearance.Normal[null] = normalAppearanceState;
    }

    private void Apply(
      ListBox field
      )
    {
      Document document = field.Document;
      Widget widget = field.Widgets[0];

      Appearance appearance = widget.Appearance;
      if(appearance == null)
      {widget.Appearance = appearance = new Appearance(document);}

      {
        PdfDictionary widgetDataObject = widget.BaseDataObject;
        widgetDataObject[PdfName.DA] = new PdfString("/Helv " + FontSize + " Tf 0 0 0 rg");
        widgetDataObject[PdfName.MK] = new PdfDictionary(
          new PdfName[]
          {
            PdfName.BG,
            PdfName.BC
          },
          new PdfDirectObject[]
          {
            new PdfArray(new PdfDirectObject[]{new PdfReal(.9),new PdfReal(.9),new PdfReal(.9)}),
            new PdfArray(new PdfDirectObject[]{new PdfInteger(0),new PdfInteger(0),new PdfInteger(0)})
          }
          );
      }

      FormXObject normalAppearanceState = new FormXObject(document);
      {
        SizeF size = widget.Size;
        normalAppearanceState.Size = new Size((int)size.Width,(int)size.Height);;
        PrimitiveFilter builder = new PrimitiveFilter(normalAppearanceState);

        builder.BeginLocalState();
        double lineWidth = 1;
        builder.SetLineWidth(lineWidth);
        builder.SetFillColor(BackColor);
        builder.SetStrokeColor(ForeColor);
        RectangleF frame = new RectangleF((float)lineWidth/2,(float)lineWidth/2,(float)(size.Width-lineWidth),(float)(size.Height-lineWidth));
        builder.DrawRectangle(frame,5);
        builder.FillStroke();
        builder.End();

        builder.BeginLocalState();
        builder.DrawRectangle(frame,5);
        builder.Clip(); // Ensures that the visible content is clipped within the rounded frame.

        builder.BeginMarkedContent(PdfName.Tx);
        builder.SetFont(
          new StandardType1Font(
            document,
            StandardType1Font.FamilyNameEnum.Helvetica,
            false,
            false
            ),
          FontSize
          );
        double y = 3;
        foreach(ChoiceItem item in field.Items)
        {
          builder.ShowText(
            item.Text,
            new PointF(0,(float)y)
            );
          y += FontSize * 1.175;
          if(y > size.Height)
            break;
        }
        builder.End();
        builder.End();

        builder.Flush();
      }
      appearance.Normal[null] = normalAppearanceState;
    }
    #endregion
    #endregion
    #endregion
  }
}
w___w__w.___j__a__v_a__2_s.___co_m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.