Click or drag to resize

ParagraphStyle Class

Represents a paragraph style.
Inheritance Hierarchy
SystemObject
  SautinSoft.DocumentStyle
    SautinSoft.DocumentParagraphStyle

Namespace: SautinSoft.Document
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2024.10.24
Syntax
public sealed class ParagraphStyle : Style

The ParagraphStyle type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleParagraphStyle Initializes a new instance of the ParagraphStyle class.
Top
Properties
 NameDescription
Public propertyBaseStyle Gets or sets the style on which this style is based.
Public propertyCode exampleCharacterFormat Gets or sets the character format.
Public propertyCode exampleListFormat Gets or sets the list format.
Public propertyCode exampleParagraphFormat Gets or sets the paragraph format.
Public propertyStyleType Gets the type of the style.
(Overrides StyleStyleType)
Top
Methods
 NameDescription
Public methodCode exampleEquals Determines whether the specified object is equal to this ParagraphStyle instance.
(Overrides ObjectEquals(Object))
Top
Remarks
Styles provide a way to format your document in a consistent way so when you change your formatting options on a style, all document elements referencing that style will be changed.
Example

See Developer Guide: How the Styles Inheritance does work

Shows how the Styles Inheritance does work using C#
using SautinSoft.Document;

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

            StyleInheritance();
        }
        /// <summary>
        /// How the Styles Inheritance does work.
        /// </summary>
        /// <remarks>
        /// https://sautinsoft.com/products/document/help/net/developer-guide/styles-inheritance.php
        /// </remarks>
        static void StyleInheritance()
        {
            string docxPath = @"StylesInheritance.docx";

            // Let's create document.
            DocumentCore dc = new DocumentCore();
            dc.DefaultCharacterFormat.FontColor = Color.Blue; 
            Section section = new Section(dc);
            section.Blocks.Add(new Paragraph(dc, new Run(dc, "The document has Default Character Format with 'Blue' color.", new CharacterFormat() { Size = 18 })));
            dc.Sections.Add(section);

            // Create a new Paragraph and Style with 'Yellow' background.
            Paragraph par = new Paragraph(dc);
            ParagraphStyle styleYellowBg = new ParagraphStyle("YellowBackground");
            styleYellowBg.CharacterFormat.BackgroundColor = Color.Yellow;
            dc.Styles.Add(styleYellowBg);
            par.ParagraphFormat.Style = styleYellowBg;

            par.Inlines.Add(new Run(dc, "This paragraph has Style 'Yellow Background' and it inherits 'Blue Color' from the document's DefaultCharacterFormat."));
            par.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            Run run1 =  new Run(dc, "This Run doesn't have a style, but it inherits 'Yellow Background' from the paragraph style and 'Blue Color' from the document's DefaultCharacterFormat.");
            run1.CharacterFormat.Italic = true;
            par.Inlines.Add(run1);
            par.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));

            Run run2 = new Run(dc, " This run has own Style with 'Green Color'.");
            CharacterStyle styleGreenText = new CharacterStyle("GreenText");
            styleGreenText.CharacterFormat.FontColor = Color.Green;
            dc.Styles.Add(styleGreenText);
            run2.CharacterFormat.Style = styleGreenText;            
            par.Inlines.Add(run2);

            Paragraph par2 = new Paragraph(dc);
            Run run3 = new Run(dc, "This is a new paragraph without a style. This is a Run also without style. " +
                "But they both inherit 'Blue Color' from their parent - the document.");
            par2.Inlines.Add(run3);
            section.Blocks.Add(par);
            section.Blocks.Add(par2);

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

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docxPath) { UseShellExecute = true });
        }
    }
}
Shows how the Styles Inheritance does work using VB.Net
Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        StyleInheritance()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How the Styles Inheritance does work.
    ''' </summary>
    ''' <remarks>
    ''' https://sautinsoft.com/products/document/help/net/developer-guide/styles-inheritance.php
    ''' </remarks>
    Sub StyleInheritance()
        Dim docxPath As String = "StylesInheritance.docx"

        ' Let's create document.
        Dim dc As New DocumentCore()
        dc.DefaultCharacterFormat.FontColor = Color.Blue
        Dim section As New Section(dc)
        section.Blocks.Add(New Paragraph(dc, New Run(dc, "The document has Default Character Format with 'Blue' color.", New CharacterFormat() With {.Size = 18})))
        dc.Sections.Add(section)

        ' Create a new Paragraph and Style with 'Yellow' background.
        Dim par As New Paragraph(dc)
        Dim styleYellowBg As New ParagraphStyle("YellowBackground")
        styleYellowBg.CharacterFormat.BackgroundColor = Color.Yellow
        dc.Styles.Add(styleYellowBg)
        par.ParagraphFormat.Style = styleYellowBg

        par.Inlines.Add(New Run(dc, "This paragraph has Style 'Yellow Background' and it inherits 'Blue Color' from the document's DefaultCharacterFormat."))
        par.Inlines.Add(New SpecialCharacter(dc, SpecialCharacterType.LineBreak))
        Dim run1 As New Run(dc, "This Run doesn't have a style, but it inherits 'Yellow Background' from the paragraph style and 'Blue Color' from the document's DefaultCharacterFormat.")
        run1.CharacterFormat.Italic = True
        par.Inlines.Add(run1)
        par.Inlines.Add(New SpecialCharacter(dc, SpecialCharacterType.LineBreak))

        Dim run2 As New Run(dc, " This run has own Style with 'Green Color'.")
        Dim styleGreenText As New CharacterStyle("GreenText")
        styleGreenText.CharacterFormat.FontColor = Color.Green
        dc.Styles.Add(styleGreenText)
        run2.CharacterFormat.Style = styleGreenText
        par.Inlines.Add(run2)

        Dim par2 As New Paragraph(dc)
        Dim run3 As New Run(dc, "This is a new paragraph without a style. This is a Run also without style. " & "But they both inherit 'Blue Color' from their parent - the document.")
        par2.Inlines.Add(run3)
        section.Blocks.Add(par)
        section.Blocks.Add(par2)

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

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

End Module
See Also