/// <summary>
 /// Iterates into paragraph items.
 /// </summary>
 /// <param name="paragraph">The paragraph.</param>
 /// <param name="fieldType">Type of field.</param>
 private void RemoveFieldCodesInParagraph(ParagraphItemCollection paraItems)
 {
     for (int i = 0; i < paraItems.Count; i++)
     {
         if (paraItems[i] is WField)
         {
             WField field = paraItems[i] as WField;
             field.Unlink();
         }
         else if (paraItems[i] is WTextBox)
         {
             //If paragraph item is textbox, iterates into textbody of textbox.
             WTextBox textBox = paraItems[i] as WTextBox;
             RemoveFieldCodesInTextBody(textBox.TextBoxBody);
         }
         else if (paraItems[i] is Shape)
         {
             //If paragraph item is shape, iterates into textbody of shape.
             Shape shape = paraItems[i] as Shape;
             RemoveFieldCodesInTextBody(shape.TextBody);
         }
         else if (paraItems[i] is InlineContentControl)
         {
             //If paragraph item is inline content control, iterates into its item.
             InlineContentControl inlineContentControl = paraItems[i] as InlineContentControl;
             RemoveFieldCodesInParagraph(inlineContentControl.ParagraphItems);
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Inserting a Check box content control.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-checkbox-net-csharp-vb.php
        /// </remarks>

        static void InsertCheckBox()
        {
            DocumentCore dc = new DocumentCore();

            InlineContentControl checkbox = new InlineContentControl(dc, ContentControlType.CheckBox);

            // Set the checkbox properties.
            checkbox.Properties.Title                = "Click me";
            checkbox.Properties.Checked              = true;
            checkbox.Properties.LockDeleting         = true;
            checkbox.Properties.CharacterFormat.Size = 24;

            // Override default checkbox appearance.
            checkbox.Properties.CheckedSymbol.FontName    = "Courier New";
            checkbox.Properties.CheckedSymbol.Character   = 'X';
            checkbox.Properties.UncheckedSymbol.FontName  = "Courier New";
            checkbox.Properties.UncheckedSymbol.Character = 'O';

            dc.Sections.Add(new Section(dc, new Paragraph(dc, new Run(dc, "Click me => ",
                                                                      new CharacterFormat()
            {
                Size = 24, FontColor = new Color("#3399FF")
            }), checkbox)));

            // Save our document into DOCX format.
            string resultPath = @"result.docx";

            dc.Save(resultPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath)
            {
                UseShellExecute = true
            });
        }
Beispiel #3
0
        /// <summary>
        /// Inserting a Combo Box content control.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-combobox-net-csharp-vb.php
        /// </remarks>

        static void InsertCombobox()
        {
            // Let's create a simple document.
            DocumentCore dc = new DocumentCore();

            // Create a Combo Box content control.
            InlineContentControl combobox = new InlineContentControl(dc, ContentControlType.ComboBox);

            dc.Sections.Add(new Section(dc, new Paragraph(dc, new Run(dc, "Combo Box "), combobox)));

            // Set common the content control properties.
            combobox.Properties.Title        = "Combo Box";
            combobox.Properties.LockDeleting = true;
            combobox.Properties.CharacterFormat.FontColor = Color.Blue;

            // Add combox's list items.
            combobox.Properties.ListItems.Add(new ContentControlListItem("One", "1"));
            combobox.Properties.ListItems.Add(new ContentControlListItem("Two", "2"));
            combobox.Properties.ListItems.Add(new ContentControlListItem("Three", "3"));
            combobox.Properties.ListItems.Add(new ContentControlListItem("Four", "4"));

            // Set a default selected item.
            combobox.Properties.SelectedListItem = combobox.Properties.ListItems[2];

            // Save our document into DOCX format.
            string resultPath = @"result.docx";

            dc.Save(resultPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath)
            {
                UseShellExecute = true
            });
        }
    static void Example1()
    {
        var document = new DocumentModel();

        var section = new Section(document);

        document.Sections.Add(section);

        // Create locked Rich Text Content Control.
        var richTextControl = new BlockContentControl(document, ContentControlType.RichText,
                                                      new Paragraph(document, "This text is inside Rich Text Content Control."),
                                                      new Paragraph(document, "It cannot be deleted or edited."));

        richTextControl.Properties.LockEditing  = true;
        richTextControl.Properties.LockDeleting = true;
        section.Blocks.Add(richTextControl);

        // Create named Plain Text Content Control.
        var plainTextControl = new BlockContentControl(document, ContentControlType.PlainText,
                                                       new Paragraph(document, "Plain Text Content Control with tag and title."));

        plainTextControl.Properties.Tag   = "Plain Text Name";
        plainTextControl.Properties.Title = "Plain Text Title";
        section.Blocks.Add(plainTextControl);

        // Create CheckBox Content Control.
        var checkBoxControl = new InlineContentControl(document, ContentControlType.CheckBox,
                                                       new Run(document, "☒")
        {
            CharacterFormat = { FontName = "MS Gothic" }
        });

        checkBoxControl.Properties.Checked = true;

        // Create ComboBox Content Control.
        var comboBoxControl = new InlineContentControl(document, ContentControlType.ComboBox,
                                                       new Run(document, "<Select GemBox Component>"));

        comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("<Select GemBox Component>", "NONE"));
        comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Spreadsheet", "GBS"));
        comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Document", "GBD"));
        comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Pdf", "GBA"));
        comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Presentation", "GBP"));
        comboBoxControl.Properties.ListItems.Add(new ContentControlListItem("GemBox.Email", "GBE"));

        section.Blocks.Add(new Paragraph(document,
                                         checkBoxControl,
                                         new SpecialCharacter(document, SpecialCharacterType.LineBreak),
                                         comboBoxControl));

        document.Save("Content Controls.docx");
    }
        public override void AddContentControls(Control control)
        {
            try
            {
                InlineContentControl contentControl = new InlineContentControl(this);

                contentControl.ID                      = "inlinecontent";
                contentControl.DefaultContent          = GetContentText();
                contentControl.DefaultAnonymousContent = GetAnonymousContentText();

                if (DynamicName)
                {
                    contentControl.InlineContentName = PublicApi.Url.CurrentContext.PageName + "_";
                }

                if (ContextualMode == ContextMode.GroupContext || ContextualMode == ContextMode.Context)
                {
                    //This allows the control to have several instances on a single page
                    if (!string.IsNullOrWhiteSpace(InlineContentName))
                    {
                        contentControl.InlineContentName += InlineContentName + "_";
                    }

                    ContextualItem(a =>
                    {
                        contentControl.InlineContentName += a.ApplicationId.ToString();
                    }, c =>
                    {
                        if (c != null && c.ContainerId != PublicApi.Groups.Root.ContainerId)
                        {
                            contentControl.InlineContentName += c.ContainerId.ToString();
                        }
                        else
                        {
                            contentControl.InlineContentName += "SiteRoot";
                        }
                    }, ta =>
                    {
                        contentControl.InlineContentName += GetHashString(string.Join("", ta.OrderBy(s => s)));
                    });
                }
                else
                {
                    contentControl.InlineContentName += InlineContentName;
                }

                control.Controls.Add(contentControl);
            }
            catch (CSException csEx)
            {
                csEx.Log();
                control.Controls.Add(new LiteralControl(csEx.Message));
            }
            catch (Exception ex)
            {
                CSException wrappedEx = new CSException(CSExceptionType.UnknownError, "Inline Content Exception", ex);
                wrappedEx.Log();

                control.Controls.Add(new LiteralControl(ex.Message));
            }
        }