Section Class

A section in a document.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.RichEdit.v21.2.dll

Declaration

public class Section :
    DocumentElementBase,
    ISectionProperties,
    SectionProperties

Remarks

A section is a part of the main sub-document that has its own page settings. A main sub-document consists of at least one section.

Each section can have a different page size (PageSize), number of columns (ColumnCount), and a set of linked headers and footers (first, primary, odd, and even).

Page header and footer

Section Headers and Footers

Call the GetHeaderAsync and GetFooterAsync methods to get sub-documents (SubDocument objects) that are the section’s header and footer. You can use the type method parameter to specify the type of the header/footer to return: first, primary, odd, or even. If the section has no header/footer of the specified type, you can set createIfNotExist to true to create the new header/footer.

<DxRichEdit @ref="richEdit" />

@code {
    DxRichEdit richEdit;
    Document documentAPI;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
            documentAPI = richEdit.DocumentAPI;
            @* ... *@
            Section section = await documentAPI.Sections.GetAsync(0);
            var footer = await section.GetFooterAsync(HeaderFooterType.Primary, true);
            await footer.AddTextAsync(DateTime.Now.Year.ToString());
            @* ... *@
            var fParagraph = await footer.Paragraphs.GetAsync(0);
            await fParagraph.ChangePropertiesAsync(properties => {
                properties.Alignment = ParagraphAlignment.Center;
            });
            await section.ChangePropertiesAsync(properties => {
                properties.FooterOffset = 700;
            });
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

A Section object contains settings for header offset (HeaderOffset), footer offset (FooterOffset), and section margin (Margins) values.

Section Margins

Change Section Properties

Pass a SectionProperties object to the ChangePropertiesAsync(Action<SectionProperties>) method to set a section’s properties. You can specify section properties manually or use the CopyFrom(Section) method to copy properties from another section.

<DxRichEdit @ref="richEdit" />

@code {
    DxRichEdit richEdit;
    Document documentAPI;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
            documentAPI = richEdit.DocumentAPI;
            @* ... *@
            var sections = await documentAPI.Sections.GetAllAsync();
            foreach (Section s in sections)
                await s.ChangePropertiesAsync(properties => {
                    if (s.PageSize.Width != UnitConverter.PointsToTwips(595))
                        properties.PageSize.Width = UnitConverter.PointsToTwips(595);
                    if (s.PageSize.Height != UnitConverter.PointsToTwips(842))
                        properties.PageSize.Height = UnitConverter.PointsToTwips(842);
                });
                @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Create a Section

Call a CreateAsync method to insert a section break and create a new section.

<DxRichEdit @bind-Selection="@selection" @ref="@richEdit" />
@code {
    DxRichEdit richEdit { get; set; }
    Selection selection { get; set; }
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
            documentAPI = richEdit.DocumentAPI;
            await documentAPI.Sections.CreateAsync(richEdit.Selection.CaretPosition, SectionBreakType.EvenPage);      
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Find a Section

Call the GetAsync(Int32) method to find a section with the specified index in the document.

<DxRichEdit @ref="richEdit" />

@code {
    DxRichEdit richEdit;
    Document documentAPI;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
            documentAPI = richEdit.DocumentAPI;
            @* ... *@
            Section firstSection = await documentAPI.Sections.GetAsync(0);
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Remove a Section

Call the RemoveAsync(Interval) method and specify a section interval as a parameter to remove the section.

Inheritance

See Also