Импортирование и копирование – в чем разница. Что такое ImportSession.


Экземпляры элементов из одного документа не могут быть вставлены в экземпляр другого документа. Сначала их необходимо импортировать в другой документ, а уже после этого их можно вставлять. В следующем примере показан процесс импорта:

Полный код

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


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



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

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