Importing and Coping - what is the difference. What is the ImportSession using C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Load a DOCX document.
  3. Import all sections from the source document.
  4. Save the document as new.

Complete code

using System;
using SautinSoft.Document;
using System.IO;
using System.Linq;
using System.Text;

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

            ImportingElement();
        }

        /// <summary>
        /// Copy a document to other document. Supported any formats (PDF, DOCX, RTF, HTML).
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/importing-element-net-csharp-vb.php
        /// </remarks>
        static void ImportingElement()
        {
            string file1 = @"..\..\..\digitalsignature.docx";
            string file2 = @"..\..\..\Parsing.docx";
            string resultFile = "Importing.docx";

            // Load files.
            DocumentCore dc = DocumentCore.Load(file1);
            DocumentCore dc1 = DocumentCore.Load(file2);

            // New Import Session to improve performance.
            var session = new ImportSession(dc1, dc);

            // Import all sections from source document.
            foreach (Section sourceSection in dc1.Sections)
            {
                Section destinationSection = dc.Import(sourceSection, true, session);
                dc.Sections.Add(destinationSection);
            }

            // Save the result.
            dc.Save(resultFile);

            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultFile) { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports System
Imports SautinSoft.Document
Imports System.IO
Imports System.Linq
Imports System.Text
Module Sample
    Sub Main()
        ImportingElement()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Copy a document to other document. Supported any formats (PDF, DOCX, RTF, HTML).
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/importing-element-net-csharp-vb.php
    ''' </remarks>
    Sub ImportingElement()
        Dim file1 As String = "..\..\..\digitalsignature.docx"
        Dim file2 As String = "..\..\..\Parsing.docx"
        Dim resultFile As String = "Importing.docx"

        ' Load files.
        Dim dc As DocumentCore = DocumentCore.Load(file1)
        Dim dc1 As DocumentCore = DocumentCore.Load(file2)

        ' New Import Session to improve performance.
        Dim session = New ImportSession(dc1, dc)

        ' Import all sections from source document.
        For Each sourceSection As Section In dc1.Sections
            Dim destinationSection As Section = dc.Import(sourceSection, True, session)
            dc.Sections.Add(destinationSection)
        Next sourceSection

        ' Save the result.
        dc.Save(resultFile)

        ' Show the result.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultFile) With {.UseShellExecute = True})
    End Sub
End Module

Download

If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page).