Как установить формат символов

  1. Добавьте SautinSoft.Document из Nuget.
  2. Создайте новый документ.
  3. Добавьте немного текста.
  4. Примените название шрифта, размер, цвет и другое форматирование.

Полный код

using SautinSoft.Document;

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

            CharacterFormatting();
        }

		/// <summary>
        /// This sample shows how to set character format. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/character-format.php
        /// </remarks>		
        public static void CharacterFormatting()
        {
            string documentPath = @"CharacterFormat.pdf";

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

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

            // Add a paragraph.
            Paragraph p = new Paragraph(dc);
            p.ParagraphFormat.Alignment = HorizontalAlignment.Left;
            dc.Sections[0].Blocks.Add(p);

            // Create a formatted text (Run element) and add it into paragraph.
            Run run1 = new Run(dc, "It\'s wide formatted text.");
            run1.CharacterFormat.AllCaps = true;
            run1.CharacterFormat.BackgroundColor = Color.Pink;
            run1.CharacterFormat.FontName = "Verdana";
            run1.CharacterFormat.Size = 26f;
            run1.CharacterFormat.FontColor = new Color("#FFFFFF");

            p.Inlines.Add(run1);

            // Create another Run element (container for characters).
            Run run2 = new Run(dc, "Hi from SautinSoft!");
            run2.CharacterFormat.FontColor = Color.DarkGreen;
            run2.CharacterFormat.UnderlineStyle = UnderlineType.Dashed;
            run2.CharacterFormat.UnderlineColor = Color.Gray;

            // Add another formatted text into the paragraph.
            p.Inlines.Add(run2);

            // Add new paragraph with formatted text.
            // We are using ContentRange to insert text.
            dc.Content.Start.Insert("This is the first paragraph.\n", new CharacterFormat() { FontName = "Calibri", Size = 16.0, FontColor = Color.Orange, Bold = true });
            (dc.Sections[0].Blocks[0] as Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center;

            dc.Content.End.Insert("Bold", new CharacterFormat() { Bold = true, FontName = "Times New Roman", Size = 11.0 });
            dc.Content.End.Insert(" Italic ", new CharacterFormat() { Italic = true, FontName = "Calibri", Size = 11.0 });
            dc.Content.End.Insert("Underline", new CharacterFormat() { UnderlineStyle = UnderlineType.Single, FontName = "Calibri", Size = 11.0 });
            dc.Content.End.Insert(" ", new CharacterFormat() { Bold = true, FontName = "Segoe UI", Size = 11.0 });
            dc.Content.End.Insert("Strikethrough", new CharacterFormat() { Strikethrough = true, FontName = "Calibri", Size = 11.0 });

            // Save our document into PDF format.
            dc.Save(documentPath, new PdfSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        CharacterFormatting()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' This sample shows how to set character format. 
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/character-format.php
    ''' </remarks>
    Sub CharacterFormatting()
        Dim documentPath As String = "CharacterFormat.pdf"

        ' Let's create a simple document.
        Dim dc As New DocumentCore()

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

        ' Add a paragraph.
        Dim p As New Paragraph(dc)
        p.ParagraphFormat.Alignment = HorizontalAlignment.Left
        dc.Sections(0).Blocks.Add(p)

        ' Create a formatted text (Run element) and add it into paragraph.
        Dim run1 As New Run(dc, "It's wide formatted text.")
        run1.CharacterFormat.AllCaps = True
        run1.CharacterFormat.BackgroundColor = Color.Pink
        run1.CharacterFormat.FontName = "Verdana"
        run1.CharacterFormat.Size = 26.0F
        run1.CharacterFormat.FontColor = New Color("#FFFFFF")

        p.Inlines.Add(run1)

        ' Create another Run element (container for characters).
        Dim run2 As New Run(dc, "Hi from SautinSoft!")
        run2.CharacterFormat.FontColor = Color.DarkGreen
        run2.CharacterFormat.UnderlineStyle = UnderlineType.Dashed
        run2.CharacterFormat.UnderlineColor = Color.Gray

        ' Add another formatted text into the paragraph.
        p.Inlines.Add(run2)

        ' Add new paragraph with formatted text.
        ' We are using ContentRange to insert text.
        dc.Content.Start.Insert("This is the first paragraph." & vbLf, New CharacterFormat() With {
                .FontName = "Calibri",
                .Size = 16.0,
                .FontColor = Color.Orange,
                .Bold = True
            })
        TryCast(dc.Sections(0).Blocks(0), Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center

        dc.Content.End.Insert("Bold", New CharacterFormat() With {
                .Bold = True,
                .FontName = "Times New Roman",
                .Size = 11.0
            })
        dc.Content.End.Insert(" Italic ", New CharacterFormat() With {
                .Italic = True,
                .FontName = "Calibri",
                .Size = 11.0
            })
        dc.Content.End.Insert("Underline", New CharacterFormat() With {
                .UnderlineStyle = UnderlineType.Single,
                .FontName = "Calibri",
                .Size = 11.0
            })
        dc.Content.End.Insert(" ", New CharacterFormat() With {
                .Bold = True,
                .FontName = "Segoe UI",
                .Size = 11.0
            })
        dc.Content.End.Insert("Strikethrough", New CharacterFormat() With {
                .Strikethrough = True,
                .FontName = "Calibri",
                .Size = 11.0
            })

        ' Save our document into PDF format.
        dc.Save(documentPath, New PdfSaveOptions())

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

Download


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



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

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