Element instances from the one document cannot be inserted into another document instance. They must first be imported to another document, and after then you can insert them. The following example shows the importing process:
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)
{
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 });
}
}
}
Option Infer On
Imports System
Imports SautinSoft.Document
Imports System.IO
Imports System.Linq
Imports System.Text
Module Sample
Sub Main()
ImportingElement()
End Sub
''' <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
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) or use the Form below: