Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vderyushev committed Oct 4, 2024
1 parent 362be47 commit 1fda0ef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Aspose.Words;
using Aspose.Words.Fields;
Expand Down Expand Up @@ -336,7 +337,7 @@ public void KeepSourceTogether()

dstDoc.Save(ArtifactsDir + "JoinAndAppendDocuments.KeepSourceTogether.docx");
//ExEnd:KeepSourceTogether
}
}

[Test]
public void ListKeepSourceFormatting()
Expand Down Expand Up @@ -460,17 +461,31 @@ public void UseDestinationStyles()
public void SmartStyleBehavior()
{
//ExStart:SmartStyleBehavior
Document srcDoc = new Document(MyDir + "Document source.docx");
Document dstDoc = new Document(MyDir + "Northwind traders.docx");
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);

builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);

ImportFormatOptions options = new ImportFormatOptions { SmartStyleBehavior = true };
Style myStyle = builder.Document.Styles.Add(StyleType.Paragraph, "MyStyle");
myStyle.Font.Size = 14;
myStyle.Font.Name = "Courier New";
myStyle.Font.Color = Color.Blue;

builder.ParagraphFormat.StyleName = myStyle.Name;
builder.Writeln("Hello world!");

// Clone the document and edit the clone's "MyStyle" style, so it is a different color than that of the original.
// If we insert the clone into the original document, the two styles with the same name will cause a clash.
Document srcDoc = dstDoc.Clone();
srcDoc.Styles["MyStyle"].Font.Color = Color.Red;

builder.InsertDocument(srcDoc, ImportFormatMode.UseDestinationStyles, options);
builder.Document.Save(ArtifactsDir + "JoinAndAppendDocuments.SmartStyleBehavior.docx");
// When we enable SmartStyleBehavior and use the KeepSourceFormatting import format mode,
// Aspose.Words will resolve style clashes by converting source document styles.
// with the same names as destination styles into direct paragraph attributes.
ImportFormatOptions options = new ImportFormatOptions();
options.SmartStyleBehavior = true;

builder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting, options);

dstDoc.Save(ArtifactsDir + "DocumentBuilder.SmartStyleBehavior.docx");
//ExEnd:SmartStyleBehavior
}

Expand Down Expand Up @@ -520,21 +535,18 @@ public void InsertDocumentInline()
public void KeepSourceNumbering()
{
//ExStart:KeepSourceNumbering
Document srcDoc = new Document(MyDir + "Document source.docx");
Document dstDoc = new Document(MyDir + "Northwind traders.docx");

// Keep source list formatting when importing numbered paragraphs.
ImportFormatOptions importFormatOptions = new ImportFormatOptions { KeepSourceNumbering = true };

NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting,
importFormatOptions);

ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs;
foreach (Paragraph srcPara in srcParas)
{
Node importedNode = importer.ImportNode(srcPara, false);
dstDoc.FirstSection.Body.AppendChild(importedNode);
}
Document srcDoc = new Document(MyDir + "List source.docx");
Document dstDoc = new Document(MyDir + "List destination.docx");

ImportFormatOptions options = new ImportFormatOptions();
// If there is a clash of list styles, apply the list format of the source document.
// Set the "KeepSourceNumbering" property to "false" to not import any list numbers into the destination document.
// Set the "KeepSourceNumbering" property to "true" import all clashing
// list style numbering with the same appearance that it had in the source document.
options.KeepSourceNumbering = true;

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting, options);
dstDoc.UpdateListLabels();

dstDoc.Save(ArtifactsDir + "JoinAndAppendDocuments.KeepSourceNumbering.docx");
//ExEnd:KeepSourceNumbering
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@ public void RestartListAtEachSection()
{
//ExStart:RestartListAtEachSection
Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

doc.Lists.Add(ListTemplate.NumberDefault);

List list = doc.Lists[0];
list.IsRestartAtEachSection = true;

DocumentBuilder builder = new DocumentBuilder(doc);
builder.ListFormat.List = list;

for (int i = 1; i < 45; i++)
// The "IsRestartAtEachSection" property will only be applicable when
// the document's OOXML compliance level is to a standard that is newer than "OoxmlComplianceCore.Ecma376".
OoxmlSaveOptions options = new OoxmlSaveOptions
{
builder.Writeln($"List Item {i}");
Compliance = OoxmlCompliance.Iso29500_2008_Transitional
};

if (i == 15)
builder.InsertBreak(BreakType.SectionBreakNewPage);
}
builder.ListFormat.List = list;

// IsRestartAtEachSection will be written only if compliance is higher then OoxmlComplianceCore.Ecma376.
OoxmlSaveOptions options = new OoxmlSaveOptions { Compliance = OoxmlCompliance.Iso29500_2008_Transitional };
builder.Writeln("List item 1");
builder.Writeln("List item 2");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Writeln("List item 3");
builder.Writeln("List item 4");

doc.Save(ArtifactsDir + "WorkingWithList.RestartListAtEachSection.docx", options);
doc.Save(ArtifactsDir + "OoxmlSaveOptions.RestartingDocumentList.docx", options);
//ExEnd:RestartListAtEachSection
}

Expand Down

0 comments on commit 1fda0ef

Please sign in to comment.