Форматирование параграфа

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

Полный код

using System;
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/

            ParagraphFormatting();
        }

		/// <summary>
        /// This sample shows how to specify a paragraph formatting. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/paragraph-format.php
        public static void ParagraphFormatting()
        {
            string documentPath = @"ParagraphFormatting.docx";

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

            // Add new section
            Section s = new Section(dc);
            dc.Sections.Add(s);

            // Paragraph 1.
            Paragraph p = new Paragraph(dc, "First paragraph!");
            dc.Sections[0].Blocks.Add(p);

            p.ParagraphFormat.Alignment = HorizontalAlignment.Left;
            p.ParagraphFormat.SpaceAfter = 20.0;
            p.ParagraphFormat.Borders.Add(MultipleBorderTypes.All, BorderStyle.Single, Color.Orange, 2f);

            // Paragraph 2.
            dc.Content.End.Insert("\nThis is a second paragraph.", new CharacterFormat() { FontName = "Calibri", Size = 16.0, FontColor = Color.Black, Bold = true });
            (dc.Sections[0].Blocks[1] as Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center;

            // Create multiple paragraphs.
            for (int i = 0; i < 10; i++)
            {
                Paragraph pN = new Paragraph(dc, String.Format("Paragraph {0}. ", i + 1));
                dc.Sections[0].Blocks.Add(pN);

                pN.Content.End.Insert(new SpecialCharacter(dc, SpecialCharacterType.Tab).Content);
                Run run = new Run(dc, "Hello!");
                run.CharacterFormat.FontColor = Color.White;
                pN.Content.End.Insert(run.Content);

                pN.ParagraphFormat.BackgroundColor = new Color((int)(0xFF358CCB * (i + 1)));
                pN.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(1, LengthUnit.Centimeter, LengthUnit.Point);
                pN.ParagraphFormat.SpaceAfter = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point);
            }

            // Save our document into DOCX format.
            dc.Save(documentPath);

            // 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()
        ParagraphFormatting()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' This sample shows how to specify a paragraph formatting. 
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/paragraph-format.php
    ''' </remarks>
    Sub ParagraphFormatting()
        Dim documentPath As String = "ParagraphFormatting.docx"

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

        ' Add new section
        Dim s As New Section(dc)
        dc.Sections.Add(s)

        ' Paragraph 1.
        Dim p As New Paragraph(dc, "First paragraph!")
        dc.Sections(0).Blocks.Add(p)

        p.ParagraphFormat.Alignment = HorizontalAlignment.Left
        p.ParagraphFormat.SpaceAfter = 20.0
        p.ParagraphFormat.Borders.Add(MultipleBorderTypes.All, BorderStyle.Single, Color.Orange, 2.0F)

        ' Paragraph 2.
        dc.Content.End.Insert(vbLf & "This is a second paragraph.", New CharacterFormat() With {
            .FontName = "Calibri",
            .Size = 16.0,
            .FontColor = Color.Black,
            .Bold = True
        })
        TryCast(dc.Sections(0).Blocks(1), Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center

        ' Create multiple paragraphs.
        For i As Integer = 0 To 9
            Dim pN As New Paragraph(dc, String.Format("Paragraph {0}. ", i + 1))
            dc.Sections(0).Blocks.Add(pN)

            pN.Content.End.Insert((New SpecialCharacter(dc, SpecialCharacterType.Tab)).Content)
            Dim run As New Run(dc, "Hello!")
            run.CharacterFormat.FontColor = Color.White
            pN.Content.End.Insert(run.Content)

            pN.ParagraphFormat.BackgroundColor = New Color(CInt(&HFF358CCB * (i + 1)))
            pN.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(1, LengthUnit.Centimeter, LengthUnit.Point)
            pN.ParagraphFormat.SpaceAfter = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point)
        Next i

        ' Save our document into DOCX format.
        dc.Save(documentPath)

        ' 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.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



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

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