Вставка элемента управления содержимым обычного текста в C# и .NET


Элемент управления содержимым в виде обычного текста ограничен простым текстом в одном или нескольких параграфах. Он не может содержать другие элементы, такие как таблицы, изображения или другие элементы управления содержимым.

Полный код

using System;
using System.Text;
using SautinSoft.Document;
using SautinSoft.Document.CustomMarkups;
using System.IO;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free trial key here:   
            // https://sautinsoft.com/start-for-free/

            InsertPlainText();
        }
        /// <summary>
        /// Inserting a plain text content control.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-plain-text-net-csharp-vb.php
        /// </remarks>

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

            // Create a plain text content control.
            BlockContentControl pt = new BlockContentControl(dc, ContentControlType.PlainText);

            // Add a new section.
            dc.Sections.Add(new Section(dc, pt));

            // Add the content control properties.
            pt.Properties.Title = "Title";
            pt.Properties.Multiline = true;
            pt.Properties.Color = Color.Blue;
            pt.Document.DefaultCharacterFormat.FontColor = Color.Orange;

            // Add new paragraph with formatted text.
            pt.Blocks.Add(new Paragraph(dc,
            new Run(dc, "This is first paragraph with symbols added on a new line."),
            new SpecialCharacter(dc, SpecialCharacterType.LineBreak),
            new Run(dc, "This is a new line in the first paragraph."),
            new SpecialCharacter(dc, SpecialCharacterType.LineBreak),
            new Run(dc, "Insert the \"Wingdings\" font family with formatting."),
            new Run(dc, "\xFC" + "\xF0" + "\x32") { CharacterFormat = { FontName = "Wingdings", FontColor = Color.Black, Size = 48 }}));

            // 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 });
        }
    }
}

Download

Imports System
Imports System.Text
Imports SautinSoft.Document
Imports SautinSoft.Document.CustomMarkups
Imports System.IO

Namespace Example
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            InsertPlainText()
        End Sub
        ''' Get your free trial key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' Inserting a plain text content control.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/content-controls-insert-plain-text-net-csharp-vb.php
        ''' </remarks>

        Private Shared Sub InsertPlainText()
            ' Let's create a simple document.
            Dim dc As New DocumentCore()

            ' Create a plain text content control.
            Dim pt As New BlockContentControl(dc, ContentControlType.PlainText)

            ' Add a new section.
            dc.Sections.Add(New Section(dc, pt))

            ' Add the content control properties.
            pt.Properties.Title = "Title"
            pt.Properties.Multiline = True
            pt.Properties.Color = Color.Blue
            pt.Document.DefaultCharacterFormat.FontColor = Color.Orange

            ' Add new paragraph with formatted text.
            pt.Blocks.Add(New Paragraph(dc, New Run(dc, "This is first paragraph with symbols added on a new line."),
            New SpecialCharacter(dc, SpecialCharacterType.LineBreak),
            New Run(dc, "This is a new line in the first paragraph."),
            New SpecialCharacter(dc, SpecialCharacterType.LineBreak),
            New Run(dc, "Insert the ""Wingdings"" font family with formatting."),
            New Run(dc, ChrW(&HFC).ToString() & ChrW(&HF0).ToString() & ChrW(&H32).ToString(),
            New CharacterFormat() With
                 {
                    .FontName = "Wingdings",
                    .FontColor = New Color("#000000"),
                    .Size = 48
                })))

            ' Save our document into DOCX format.
            Dim resultPath As String = "result.docx"
            dc.Save(resultPath, New DocxSaveOptions())

            ' Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.