AcroFormCreationSample.cs :  » PDF » PDF-Clown » it » stefanochizzolini » clown » samples » 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 » samples » AcroFormCreationSample.cs
using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.entities;
using it.stefanochizzolini.clown.documents.contents.fonts;
using it.stefanochizzolini.clown.documents.contents.xObjects;
using it.stefanochizzolini.clown.documents.interaction.actions;
using it.stefanochizzolini.clown.documents.interaction.annotations;
using it.stefanochizzolini.clown.documents.interaction.forms;
using it.stefanochizzolini.clown.documents.interaction.forms.styles;
using it.stefanochizzolini.clown.files;

using System;
using System.Collections.Generic;
using System.Drawing;

namespace it.stefanochizzolini.clown.samples{
  /**
    <summary>This sample demonstrates how to insert AcroForm fields into a PDF document.</summary>
  */
  public class AcroFormCreationSample
    : ISample
  {
    #region dynamic
    #region interface
    #region public
    #region ISample
    public void Run(
      SampleLoader loader
      )
    {
      // 1. PDF file instantiation.
      File file = new File();
      Document document = file.Document;

      // 2. Content creation.
      Populate(document);

      // (boilerplate metadata insertion -- ignore it)
      loader.BuildAccessories(document,this.GetType(),"AcroForm","inserting AcroForm fields");

      // 3. Serialize the PDF file (again, boilerplate code -- see the SampleLoader class source code)!
      loader.Serialize(file,this.GetType().Name,false);
    }
    #endregion
    #endregion

    #region private
    private void Populate(
      Document document
      )
    {
      /*
        NOTE: In order to insert a field into a document, you have to follow these steps:
        1. Define the form fields collection that will gather your fields (NOTE: the form field collection is global to the document);
        2. Define the pages where to place the fields;
        3. Define the appearance style to render your fields;
        4. Create each field of yours:
          4.1. instantiate your field into the page;
          4.2. apply the appearance style to your field;
          4.3. insert your field into the fields collection.
      */

      // 1. Define the form fields collection!
      Form form = new Form(document);
      document.Form = form;
      Fields fields = form.Fields;

      // 2. Define the page where to place the fields!
      Page page = new Page(document);
      document.Pages.Add(page);

      // 3. Define the appearance style to apply to the fields!
      DefaultStyle fieldStyle = new DefaultStyle();
      fieldStyle.FontSize = 12;

      PrimitiveFilter builder = new PrimitiveFilter(page);
      builder.SetFont(
        new StandardType1Font(
          document,
          StandardType1Font.FamilyNameEnum.Courier,
          true,
          false
          ),
        14
        );

      // 4. Field creation.
      // 4.a. Push button.
      {
        builder.ShowText(
          "PushButton:",
          new PointF(140, 68),
          AlignmentXEnum.Right,
          AlignmentYEnum.Middle,
          0
          );

        Widget fieldWidget = new Widget(
          page,
          new RectangleF(150, 50, 136, 36)
          );
        WidgetActions fieldWidgetActions = new WidgetActions(fieldWidget);
        fieldWidget.Actions = fieldWidgetActions;
        fieldWidgetActions.OnActivate = new JavaScript(
          document,
          "app.alert(\"Radio button currently selected: '\" + this.getField(\"myRadio\").value + \"'.\",3,0,\"Activation event\");"
          );
        PushButton field = new PushButton(
          "okButton",
          fieldWidget,
          "Push" // Current value.
          ); // 4.1. Field instantiation.
        fields.Add(field); // 4.2. Field insertion into the fields collection.
        fieldStyle.Apply(field); // 4.3. Appearance style applied.

        {
          BlockFilter blockFilter = new BlockFilter(builder);
          blockFilter.Begin(new RectangleF(296,50,page.Size.Value.Width-336,36),AlignmentXEnum.Left,AlignmentYEnum.Middle);
          builder.SetFont(builder.State.Font,7);
          blockFilter.ShowText("If you click this push button, a javascript action should prompt you an alert box responding to the activation event triggered by your PDF viewer.");
          blockFilter.End();
        }
      }

      // 4.b. Check box.
      {
        builder.ShowText(
          "CheckBox:",
          new PointF(140, 118),
          AlignmentXEnum.Right,
          AlignmentYEnum.Middle,
          0
          );
        CheckBox field = new CheckBox(
          "myCheck",
          new Widget(
            page,
            new RectangleF(150, 100, 36, 36)
            ),
          true // Current value.
          ); // 4.1. Field instantiation.
        fieldStyle.Apply(field);
        fields.Add(field);
        field = new CheckBox(
          "myCheck2",
          new Widget(
            page,
            new RectangleF(200, 100, 36, 36)
            ),
          true // Current value.
          ); // 4.1. Field instantiation.
        fieldStyle.Apply(field);
        fields.Add(field);
        field = new CheckBox(
          "myCheck3",
          new Widget(
            page,
            new RectangleF(250, 100, 36, 36)
            ),
          false // Current value.
          ); // 4.1. Field instantiation.
        fields.Add(field); // 4.2. Field insertion into the fields collection.
        fieldStyle.Apply(field); // 4.3. Appearance style applied.
      }

      // 4.c. Radio button.
      {
        builder.ShowText(
          "RadioButton:",
          new PointF(140, 168),
          AlignmentXEnum.Right,
          AlignmentYEnum.Middle,
          0
          );
        RadioButton field = new RadioButton(
          "myRadio",
          /*
            NOTE: A radio button field typically combines multiple alternative widgets.
          */
          new DualWidget[]
          {
            new DualWidget(
              page,
              new RectangleF(150, 150, 36, 36),
              "first"
              ),
            new DualWidget(
              page,
              new RectangleF(200, 150, 36, 36),
              "second"
              ),
            new DualWidget(
              page,
              new RectangleF(250, 150, 36, 36),
              "third"
              )
          },
          "second" // Selected item (it MUST correspond to one of the available widgets' names).
          ); // 4.1. Field instantiation.
        fields.Add(field); // 4.2. Field insertion into the fields collection.
        fieldStyle.Apply(field); // 4.3. Appearance style applied.
      }

      // 4.d. Text field.
      {
        builder.ShowText(
          "TextField:",
          new PointF(140, 218),
          AlignmentXEnum.Right,
          AlignmentYEnum.Middle,
          0
          );
        TextField field = new TextField(
          "myText",
          new Widget(
            page,
            new RectangleF(150, 200, 200, 36)
            ),
          "Carmen Consoli" // Current value.
          ); // 4.1. Field instantiation.
        field.SpellChecked = false; // Avoids text spell check.
        FieldActions fieldActions = new FieldActions(document);
        field.Actions = fieldActions;
        fieldActions.OnValidate = new JavaScript(
          document,
          "app.alert(\"Text '\" + this.getField(\"myText\").value + \"' has changed!\",3,0,\"Validation event\");"
          );
        fields.Add(field); // 4.2. Field insertion into the fields collection.
        fieldStyle.Apply(field); // 4.3. Appearance style applied.

        {
          BlockFilter blockFilter = new BlockFilter(builder);
          blockFilter.Begin(new RectangleF(360,200,page.Size.Value.Width-400,36),AlignmentXEnum.Left,AlignmentYEnum.Middle);
          builder.SetFont(builder.State.Font,7);
          blockFilter.ShowText("If you leave this text field after changing its content, a javascript action should prompt you an alert box responding to the validation event triggered by your PDF viewer.");
          blockFilter.End();
        }
      }

      // 4.e. Choice fields.
      {
        // Preparing the item list that we'll use for choice fields (a list box and a combo box (see below))...
        ChoiceItems items = new ChoiceItems(document);
        items.Add(new ChoiceItem("Joan Baez")); // NOTE: Explicitly-created item.
        items.Add("Tracy Chapman"); // NOTE: Implicitly-created item (syntactic sugar for lazy guys...).
        items.Add("Carmen Consoli");
        items.Add("Cristina Dona'");
        items.Add("Billie Holiday");
        items.Add("Janis Joplin");
        items.Add("Alanis Morissette");
        items.Add("Yael Naim");
        items.Add("Sinead O'Connor");
        items.Add("Brisa Roche'");
        items.Add("Skin");
        items.Add("Fatima Spar");
        items.Add("Paola Turci");

        // 4.e1. List box.
        {
          builder.ShowText(
            "ListBox:",
            new PointF(140, 268),
            AlignmentXEnum.Right,
            AlignmentYEnum.Middle,
            0
            );
          ListBox field = new ListBox(
            "myList",
            new Widget(
              page,
              new RectangleF(150, 250, 200, 70)
              )
            ); // 4.1. Field instantiation.
          field.Items = items; // List items assignment.
          field.MultiSelect = false; // Multiple items may not be selected simultaneously.
          field.Value = "Carmen Consoli"; // Selected item.
          fields.Add(field); // 4.2. Field insertion into the fields collection.
          fieldStyle.Apply(field); // 4.3. Appearance style applied.
        }

        // 4.e2. Combo box.
        {
          builder.ShowText(
            "ComboBox:",
            new PointF(140, 350),
            AlignmentXEnum.Right,
            AlignmentYEnum.Middle,
            0
            );
          ComboBox field = new ComboBox(
            "myCombo",
            new Widget(
              page,
              new RectangleF(150, 334, 200, 36)
              )
            ); // 4.1. Field instantiation.
          field.Items = items; // Combo items assignment.
          field.Editable = true; // Text may be edited.
          field.SpellChecked = false; // Avoids text spell check.
          field.Value = "Carmen Consoli"; // Selected item.
          fields.Add(field); // 4.2. Field insertion into the fields collection.
          fieldStyle.Apply(field); // 4.3. Appearance style applied.
        }
      }

      builder.Flush();
    }
    #endregion
    #endregion
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.