Манипулирование содержимым (ContentRange)

  1. Добавьте SautinSoft.Document из Nuget.
  2. Создайте новый документ.
  3. Добавьте абзац, используя свойство Content.
  4. Добавьте абзац, используя коллекцию блоков.
  5. Сохраните документ в формате DOCX.

Полный код

using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using SautinSoft.Document.Tables;
using System.Linq;

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

            ContentRangeManipulation();
        }
        /// <summary>
        /// Adds two paragraphs by different ways: using ContentRange and as Element.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/contentrange-manipulation.php
        /// </remarks>
        static void ContentRangeManipulation()
        {
            string filePath = @"Result.docx";

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

            // Way 1: Add new paragraph using the property Content (class ContentRange).
            Paragraph par = new Paragraph(dc, "This is paragraph. ");
            par.ParagraphFormat.BackgroundColor = Color.Yellow;
            // Note, our Paragraph will be cloned and the clone will be inserted. 
            // The property Content (class ContentRange) each time clones the inserting object.
            dc.Content.End.Insert(par.Content);

            // Way 2: Add paragraph into the Block collection as Element.
            // Note, you can't insert the one Element two times.
            // The cloning does not occur here.
            par.ParagraphFormat.BackgroundColor = Color.Red;
            dc.Sections[0].Blocks.Add(par);

            // Again Way 1: Change Background color to blue and insert 
            // the copy (clone) of our paragraph using Content (class ContentRange).
            par.ParagraphFormat.BackgroundColor = Color.Blue;
            dc.Content.End.Insert(par.Content);

            // Change background to 'Green' for the paragraph.
            // This affect only to the paragraph inserted by 'Way 2' - as Element.
            // Because in 'Way 1' we added clones using ContentRange.
            par.ParagraphFormat.BackgroundColor = Color.Green;

            // Save our document.
            dc.Save(filePath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
        }
    }
}

Download

Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports SautinSoft.Document.Tables
Imports System.Linq

Module Sample
    Sub Main()
        ContentRangeManipulation()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Adds two paragraphs by different ways: using ContentRange and as Element.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/contentrange-manipulation.php
    ''' </remarks>
    Sub ContentRangeManipulation()
        Dim filePath As String = "Result.docx"

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

        ' Way 1: Add new paragraph using the property Content (class ContentRange).
        Dim par As New Paragraph(dc, "This is paragraph. ")
        par.ParagraphFormat.BackgroundColor = Color.Yellow
        ' Note, our Paragraph will be cloned and the clone will be inserted. 
        ' The property Content (class ContentRange) each time clones the inserting object.
        dc.Content.End.Insert(par.Content)

        ' Way 2: Add paragraph into the Block collection as Element.
        ' Note, you can't insert the one Element two times.
        ' The cloning does not occur here.
        par.ParagraphFormat.BackgroundColor = Color.Red
        dc.Sections(0).Blocks.Add(par)

        ' Again Way 1: Change Background color to blue and insert 
        ' the copy (clone) of our paragraph using Content (class ContentRange).
        par.ParagraphFormat.BackgroundColor = Color.Blue
        dc.Content.End.Insert(par.Content)

        ' Change background to 'Green' for the paragraph.
        ' This affect only to the paragraph inserted by 'Way 2' - as Element.
        ' Because in 'Way 1' we added clones using ContentRange.
        par.ParagraphFormat.BackgroundColor = Color.Green

        ' Save our document.
        dc.Save(filePath)

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

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



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

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