Манипуляции с элементом(ами)


Полный код

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/

            ElementManipulation();
        }
        /// <summary>
        /// Create a document and add paragraphs as content and as element.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/element-manipulation.php
        /// </remarks>
        static void ElementManipulation()
        {
            string filePath = @"Result.docx";

            // Let's create a new document.
            DocumentCore dc = new DocumentCore();
            Paragraph par = new Paragraph(dc, "This is the first paragraph.");

            // Insert the clone of our Paragraph using ContentRange.
            dc.Content.End.Insert(par.Content);

            // Add our Paragraph in Block collection as Element.
            dc.Sections[0].Blocks.Add(par);

            // Again, insert the clone of our Paragraph using ContentRange.
            dc.Content.End.Insert(par.Content);

            // Change text in our Paragraph
            (par.Inlines[0] as Run).Text = "Now we are in the second paragraph.";

            // Find 3rd paragraph and change text in it.
            ((par.NextSibling as Paragraph).Inlines[0] as Run).Text = "This is the third paragraph.";

            // 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()
        ElementManipulation()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Create a document and add paragraphs as content and as element.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/element-manipulation.php
    ''' </remarks>
    Sub ElementManipulation()
        Dim filePath As String = "Result.docx"

        ' Let's create a new document.
        Dim dc As New DocumentCore()
        Dim par As New Paragraph(dc, "This is the first paragraph.")

        ' Insert the clone of our Paragraph using ContentRange.
        dc.Content.End.Insert(par.Content)

        ' Add our Paragraph in Block collection as Element.
        dc.Sections(0).Blocks.Add(par)

        ' Again, insert the clone of our Paragraph using ContentRange.
        dc.Content.End.Insert(par.Content)

        ' Change text in our Paragraph
        TryCast(par.Inlines(0), Run).Text = "Now we are in the second paragraph."

        ' Find 3rd paragraph and change text in it.
        TryCast((TryCast(par.NextSibling, Paragraph)).Inlines(0), Run).Text = "This is the third paragraph."

        ' 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


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



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

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