Объектная модель документа (DOM) и базовые классы


1. Tree structure of DocumentCore

In the SautinSoft.Document library a document is formed basing on a tree structure. The root node is DocumentCore class, which is represent a document itself.

(BlockCollection)
Run

All elements have a specific fill color:

Note, all elements are derived from base abstract class Element.

2. Base classes

As you already know, each node including DocumentCore is a child of base abstract class Element. Thus you may iterate through the whole tree and treat each node as Element class.

Most of elements which have a content are divided using their content type: Block and Inline.

Block elements are: Paragraph, Table and TableOfEntries.

Inline elements are: Run, Hyperlink, BookmarkStart, BookmarkEnd, SpecialCharacter, ShapeBase and so forth.

From other hand, there are some content elements that are directly derived from Element. For example, HeaderFooter and TableRow.

Shape, ShapeGroup and Picture are children of abstract class ShapeBase which is also derived from abstract class Inline.

3. Collections to store content

If element stores a some content inside (for example, Table contains a collection of Rows) such element implements interface IContentElement.

The IContentElement interface enables your to get the one or more collections with the element's content. For example, element Section has two collections: with all Block element and all HeaderFooter elements specified for this section.

Each collection (BlockCollection, InlineCollection, TableRowCollection and so forth) is derived from base abstract class ElementCollection.

By way of examples:

Each Paragraph has InlineCollection which stores all Run, Hyperlink, SpecialCharacter and other Inline elements nested in this paragraph.

Each Table has TableRowCollection where stored TableRow elements, and each TableRow has own TableCellCollection.

 

How to work with collections

using System;
using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            WorkingWithCollections();
        }

        static void WorkingWithCollections()
        {
            // Create new document.
            DocumentCore dc = new DocumentCore();

            // Create and add new section.
            Section s = new Section(dc)
            {
                PageSetup = new PageSetup()
                {
                    PaperType = PaperType.Letter,
                    Orientation = Orientation.Landscape
                }
            };
            // SectionCollection: add new Section.
            dc.Sections.Add(s);

            // Create new paragraph.
            Paragraph p = new Paragraph(dc);
            // BlockCollection: add new Paragraph (Block derived element).
            s.Blocks.Add(p);

            // InlineCollection: Add 3 Run and 1 SpecialCharacter elements (Inline derived).            
            p.Inlines.Add(new Run(dc, "Hello ") { CharacterFormat = { Size = 14.0 } });
            p.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            p.Inlines.Add(new Run(dc, "my") { CharacterFormat = { Size = 15.0 } });
            p.Inlines.Add(new Run(dc, " Friend!") { CharacterFormat = { Size = 16.0 } });

            // Iterate through InlineCollection (derived ElementCollection).
            foreach (Element el in p.Inlines)
            {
                if (el is Run)
                    (el as Run).CharacterFormat.Size = 20.0;
            }

            // Let's save our document as PDF.
            dc.Save(@"d:\collections.pdf", new PdfSaveOptions());

        }
    }
}
        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module ExampleVB
    Sub Main()
        WorkingWithCollections()
    End Sub
    Public Sub WorkingWithCollections()
        ' Create new document.
        Dim dc As New DocumentCore()

        ' Create and add new section.
        Dim s As New Section(dc) With {
            .PageSetup = New PageSetup() With {
                .PaperType = PaperType.Letter,
                .Orientation = Orientation.Landscape
            }
        }
        ' SectionCollection: add new Section.
        dc.Sections.Add(s)

        ' Create new paragraph.
        Dim p As New Paragraph(dc)
        ' BlockCollection: add new Paragraph (Block derived element).
        s.Blocks.Add(p)

        ' InlineCollection: Add 3 Run and 1 SpecialCharacter elements (Inline derived).
        p.Inlines.Add(New Run(dc, "Hello ") With {
            .CharacterFormat = New CharacterFormat() With
            {.Size = 14.0}})

        p.Inlines.Add(New SpecialCharacter(dc, SpecialCharacterType.LineBreak))
        p.Inlines.Add(New Run(dc, "my") With {
            .CharacterFormat = New CharacterFormat() With
            {.Size = 15.0}})

        p.Inlines.Add(New Run(dc, " Friend!") With {
            .CharacterFormat = New CharacterFormat() With
            {.Size = 16.0}})

        ' Iterate through InlineCollection (derived ElementCollection).
        For Each el As Element In p.Inlines
            If TypeOf el Is Run Then
                TryCast(el, Run).CharacterFormat.Size = 20.0
            End If
        Next el

        ' Let's save our document as PDF.
        dc.Save("d:\collections.pdf", New PdfSaveOptions())

    End Sub

End Module