Skip to main content

Section Class

A section in a document.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.RichEdit.v24.1.dll

NuGet Package: DevExpress.Blazor.RichEdit

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 canceled. */
        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>, CancellationToken) 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 canceled. */
        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; }

    protected override Task OnAfterRenderAsync(bool firstRender) {
        if (firstRender)
            InitializeDocument();
        return base.OnAfterRenderAsync(firstRender);
    }

    async void InitializeDocument() {
    /* 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 canceled. */
        try {
            Document 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, CancellationToken) 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 canceled. */
        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, CancellationToken) method and specify a section interval as a parameter to remove the section.

The Page Setup Dialog

The Rich Text Editor invokes this dialog when a user selects the Page LayoutMarginsCustom Margins or Page LayoutSizeMore Paper Sizes… ribbon command. The Page Setup dialog allows users to change page settings of the selected sections.

Page Setup Dialog

Inheritance

See Also