Click or drag to resize

ImportSession Class

Represents an import session that maps styles (and other referenced objects) between two different DocumentCore instances.
Inheritance Hierarchy
SystemObject
  SautinSoft.DocumentImportSession

Namespace: SautinSoft.Document
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2024.10.24
Syntax
public sealed class ImportSession

The ImportSession type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleImportSession(DocumentCore, DocumentCore) Initializes a new instance of the ImportSession class.
Public methodCode exampleImportSession(DocumentCore, DocumentCore, StyleImportingMode) Initializes a new instance of the ImportSession class.
Top
Properties
 NameDescription
Public propertyDestinationDocument Gets the destination document.
Public propertyCode exampleImportMode Gets or sets style importing mode.
Public propertyMapping Gets the dictionary that contains styles mapping. Key is style in source document. Value is style in destination document.
Public propertySourceDocument Gets the source document.
Top
Example

See Developer Guide: How to merge two documents: DOCX and PDF

How to merge two documents: DOCX and PDF in C#
using System;
using System.IO;
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {

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

            MergeTwoDocuments();
        }

        /// <summary>
        /// How to merge two documents: DOCX and PDF.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/split-and-merge-content-net-csharp-vb.php
        /// </remarks>
        public static void MergeTwoDocuments()
        {
            // Path to our combined document.
            string singleFilePath = "Single.docx";

            string[] supportedFiles = new string[] { @"..\..\..\example.docx", @"..\..\..\example.pdf" };

            // Create single document.
            DocumentCore dcSingle = new DocumentCore();

            foreach (string file in supportedFiles)
            {
                DocumentCore dc = DocumentCore.Load(file);

                Console.WriteLine("Adding: {0}...", Path.GetFileName(file));

                // Create import session.
                ImportSession session = new ImportSession(dc, dcSingle, StyleImportingMode.KeepSourceFormatting);

                // Loop through all sections in the source document.
                foreach (Section sourceSection in dc.Sections)
                {
                    // Because we are copying a section from one document to another,
                    // it is required to import the Section into the destination document.
                    // This adjusts any document-specific references to styles, bookmarks, etc.
                    // 
                    // Importing a element creates a copy of the original element, but the copy
                    // is ready to be inserted into the destination document.
                    Section importedSection = dcSingle.Import<Section>(sourceSection, true, session);

                    // First section start from new page.
                    if (dc.Sections.IndexOf(sourceSection) == 0)
                        importedSection.PageSetup.SectionStart = SectionStart.NewPage;

                    // Now the new section can be appended to the destination document.
                    dcSingle.Sections.Add(importedSection);
                }
            }

            // Save single document to a file.
            dcSingle.Save(singleFilePath);

            // Open the result for demonstration purposes.
           System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singleFilePath) { UseShellExecute = true });
        }
    }
}
How to merge two documents: DOCX and PDF in VB.Net
Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        MergeTwoDocuments()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How to merge two documents: DOCX and PDF.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/split-and-merge-content-net-csharp-vb.php
    ''' </remarks>
    Sub MergeTwoDocuments()
        ' Path to our combined document.
        Dim singleFilePath As String = "Single.docx"

        Dim supportedFiles() As String = {"..\..\..\example.docx", "..\..\..\example.pdf"}

        ' Create single document.
        Dim dcSingle As New DocumentCore()

        For Each file As String In supportedFiles
            Dim dc As DocumentCore = DocumentCore.Load(file)

            Console.WriteLine("Adding: {0}...", Path.GetFileName(file))

            ' Create import session.
            Dim session As New ImportSession(dc, dcSingle, StyleImportingMode.KeepSourceFormatting)

            ' Loop through all sections in the source document.
            For Each sourceSection As Section In dc.Sections
                ' Because we are copying a section from one document to another,
                ' it is required to import the Section into the destination document.
                ' This adjusts any document-specific references to styles, bookmarks, etc.
                '
                ' Importing a element creates a copy of the original element, but the copy
                ' is ready to be inserted into the destination document.
                Dim importedSection As Section = dcSingle.Import(Of Section)(sourceSection, True, session)

                ' First section start from new page.
                If dc.Sections.IndexOf(sourceSection) = 0 Then
                    importedSection.PageSetup.SectionStart = SectionStart.NewPage
                End If

                ' Now the new section can be appended to the destination document.
                dcSingle.Sections.Add(importedSection)
            Next sourceSection
        Next file

        ' Save single document to a file.
        dcSingle.Save(singleFilePath)

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