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


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

Полный код

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

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

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

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

            // Create a rich text content control.
            BlockContentControl rt = new BlockContentControl(dc, ContentControlType.RichText);
            dc.Sections.Add(new Section(dc, rt));

            // Add the content control properties.
            rt.Properties.Title = "Rich text content control.";
            rt.Properties.Color = Color.Blue;
            rt.Properties.LockDeleting = true;
            rt.Properties.LockEditing = true;
            rt.Document.DefaultCharacterFormat.FontColor = Color.Orange;

            // Add new paragraphs with formatted text.
            rt.Blocks.Add(new Paragraph(dc, "Line 1"));
            rt.Blocks.Add(new Paragraph(dc, "Line 2"));
            rt.Blocks.Add(new Paragraph(dc, "Line 3"));

            // Add a picture and shape to the block.
            string pictPath = @"..\..\..\banner_sautinsoft.jpg";
            Picture pict = new Picture(dc, InlineLayout.Inline(new Size(400, 100)), pictPath);
            Shape shp = new Shape(dc, Layout.Inline(new Size(4, 1, LengthUnit.Centimeter)));
            Paragraph im = new Paragraph(dc);
            rt.Blocks.Add(im);
            im.Inlines.Add(pict);
            Paragraph sh = new Paragraph(dc);
            rt.Blocks.Add(sh);
            sh.Inlines.Add(shp);

            rt.Blocks.Add(new Paragraph(dc, "Line 4"));
            
            // 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 SautinSoft.Document.Drawing
Imports System.IO

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

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

			' Create a rich text content control.
			Dim rt As New BlockContentControl(dc, ContentControlType.RichText)
			dc.Sections.Add(New Section(dc, rt))

			' Add the content control properties.
			rt.Properties.Title = "Rich text content control."
			rt.Properties.Color = Color.Blue
			rt.Properties.LockDeleting = True
			rt.Properties.LockEditing = True
			rt.Document.DefaultCharacterFormat.FontColor = Color.Orange

			' Add new paragraphs with formatted text.
			rt.Blocks.Add(New Paragraph(dc, "Line 1"))
			rt.Blocks.Add(New Paragraph(dc, "Line 2"))
			rt.Blocks.Add(New Paragraph(dc, "Line 3"))

			' Add a picture and shape to the block.
			Dim pictPath As String = "..\..\..\banner_sautinsoft.jpg"
			Dim pict As New Picture(dc, InlineLayout.Inline(New Size(400, 100)), pictPath)
			Dim shp As New Shape(dc, Layout.Inline(New Size(4, 1, LengthUnit.Centimeter)))
			Dim im As New Paragraph(dc)
			rt.Blocks.Add(im)
			im.Inlines.Add(pict)
			Dim sh As New Paragraph(dc)
			rt.Blocks.Add(sh)
			sh.Inlines.Add(shp)

			rt.Blocks.Add(New Paragraph(dc, "Line 4"))

			' 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


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



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

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