KeepSourceFormattingозначает скопировать все требуемые стили в целевой документ, при необходимости сгенерировать уникальные имена стилей.
Например, целевой документ содержит стиль "Green" (Calibri, FontSize = 20, Green, Underline).
И исходный документ также содержит аналогичный стиль с тем же именем "Green" (Calibri, FontSize = 20, Green, Underline).
Давайте импортируем 1-й параграф из исходного документа.
Абзац содержит текст "Shrek" выделенный одинаковым стилем с тем же названием "Green" (Calibri,
FontSize = 20, Green, Underline).
Стиль "Green" будет импортирован и переименован в "Green1".
После импорта все импортированные элементы будут переназначены на стиль "Green1".
Полный код
using SautinSoft.Document;
using SautinSoft.Document.Tables;
using System.Linq;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Get your free 100-day key here:
// https://sautinsoft.com/start-for-free/
ImportKeepSourceFormatting();
}
/// <summary>
/// Import an Element with Styles from another document. Mode: KeepSourceFormatting.
/// </summary>
/// <remarks>
/// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/import-element-keep-source-formatting.php
/// </remarks>
private static void ImportKeepSourceFormatting()
{
// Mode: KeepSourceFormatting.
// 'KeepSourceFormatting' means to copy all required styles to the destination document,
// generate unique style names if needed.
// For example, a destination document contains a style "Green" (Calibri, FontSize = 20, Green, Underline).
// And a source document also contains an equal style with the same name "Green" (Calibri, FontSize = 20, Green, Underline).
// The style "Green" will be imported and renamed to "Green1".
// All imported elements linked to style "Green" will be remapped to style "Green1".
DocumentCore source = DocumentCore.Load(@"..\..\..\SourceStyles.docx");
DocumentCore dest = new DocumentCore();
// Let's create a style "Green" (Calibri, FontSize = 20, Green, Underline).
CharacterStyle chStyle = new CharacterStyle("Green");
chStyle.CharacterFormat.FontName = "Calibri";
chStyle.CharacterFormat.FontColor = Color.Green;
chStyle.CharacterFormat.Size = 20;
chStyle.CharacterFormat.UnderlineStyle = UnderlineType.Single;
dest.Styles.Add(chStyle);
dest.Content.End.Insert(new Run(dest, "This text has the style Green.", new CharacterFormat() { Style = chStyle }).Content);
// Create an ImportSession with mode 'KeepSourceFormatting'.
ImportSession session = new ImportSession(source, dest, StyleImportingMode.KeepSourceFormatting);
// Let's import a paragraph.
// The imported paragraph contains a text with style "Green" (FontSize = 20, Green, Underline).
// The style "Green" will be imported and renamed to "Green1", because we already have "Green".
// All links in imported paragraph will be remapped to the style "Green1".
Paragraph importedPar = dest.Import<Paragraph>((Paragraph)source.Sections[0].Blocks[0], true, session);
dest.Content.End.Insert(importedPar.Content);
// Save the destination document into DOCX format.
string docPath = "KeepSourceFormatting.docx";
dest.Save(docPath);
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docPath) { UseShellExecute = true });
}
}
}
Imports SautinSoft.Document
Imports SautinSoft.Document.Tables
Imports System.Linq
Namespace Sample
Friend Class Sample
Shared Sub Main(ByVal args() As String)
ImportKeepSourceFormatting()
End Sub
''' Get your free 100-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' Import an Element with Styles from another document. Mode: KeepSourceFormatting.
''' </summary>
''' <remarks>
''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/import-element-keep-source-formatting.php
''' </remarks>
Private Shared Sub ImportKeepSourceFormatting()
' Mode: KeepSourceFormatting.
' 'KeepSourceFormatting' means to copy all required styles to the destination document,
' generate unique style names if needed.
' For example, a destination document contains a style "Green" (Calibri, FontSize = 20, Green, Underline).
' And a source document also contains an equal style with the same name "Green" (Calibri, FontSize = 20, Green, Underline).
' The style "Green" will be imported and renamed to "Green1".
' All imported elements linked to style "Green" will be remapped to style "Green1".
Dim source As DocumentCore = DocumentCore.Load("..\..\..\SourceStyles.docx")
Dim dest As New DocumentCore()
' Let's create a style "Green" (Calibri, FontSize = 20, Green, Underline).
Dim chStyle As New CharacterStyle("Green")
chStyle.CharacterFormat.FontName = "Calibri"
chStyle.CharacterFormat.FontColor = Color.Green
chStyle.CharacterFormat.Size = 20
chStyle.CharacterFormat.UnderlineStyle = UnderlineType.Single
dest.Styles.Add(chStyle)
dest.Content.End.Insert((New Run(dest, "This text has the style Green.", New CharacterFormat() With {.Style = chStyle})).Content)
' Create an ImportSession with mode 'KeepSourceFormatting'.
Dim session As New ImportSession(source, dest, StyleImportingMode.KeepSourceFormatting)
' Let's import a paragraph.
' The imported paragraph contains a text with style "Green" (FontSize = 20, Green, Underline).
' The style "Green" will be imported and renamed to "Green1", because we already have "Green".
' All links in imported paragraph will be remapped to the style "Green1".
Dim importedPar As Paragraph = dest.Import(Of Paragraph)(CType(source.Sections(0).Blocks(0), Paragraph), True, session)
dest.Content.End.Insert(importedPar.Content)
' Save the destination document into DOCX format.
Dim docPath As String = "KeepSourceFormatting.docx"
dest.Save(docPath)
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docPath) With {.UseShellExecute = True})
End Sub
End Class
End Namespace
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: