JoinRunsWithSameFormatting

JoinRunsWithSameFormatting()

Joins runs with the same formatting in the paragraph.

public int JoinRunsWithSameFormatting()

Return Value

Number of joins performed. When N adjacent runs are being joined they count as N - 1 joins.

Examples

Shows how to simplify paragraphs by merging superfluous runs.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert four runs of text into the paragraph.
builder.Write("Run 1. ");
builder.Write("Run 2. ");
builder.Write("Run 3. ");
builder.Write("Run 4. ");

// If we open this document in Microsoft Word, the paragraph will look like one seamless text body.
// However, it will consist of four separate runs with the same formatting. Fragmented paragraphs like this
// may occur when we manually edit parts of one paragraph many times in Microsoft Word.
Paragraph para = builder.CurrentParagraph;

Assert.That(para.Runs.Count, Is.EqualTo(4));

// Change the style of the last run to set it apart from the first three.
para.Runs[3].Font.StyleIdentifier = StyleIdentifier.Emphasis;

// We can run the "JoinRunsWithSameFormatting" method to optimize the document's contents
// by merging similar runs into one, reducing their overall count.
// This method also returns the number of runs that this method merged.
// These two merges occurred to combine Runs #1, #2, and #3,
// while leaving out Run #4 because it has an incompatible style.
Assert.That(para.JoinRunsWithSameFormatting(), Is.EqualTo(2));

// The number of runs left will equal the original count
// minus the number of run merges that the "JoinRunsWithSameFormatting" method carried out.
Assert.That(para.Runs.Count, Is.EqualTo(2));
Assert.That(para.Runs[0].Text, Is.EqualTo("Run 1. Run 2. Run 3. "));
Assert.That(para.Runs[1].Text, Is.EqualTo("Run 4. "));

See Also


JoinRunsWithSameFormatting(JoinRunsOptions)

Joins runs with the same formatting in the paragraph.

public int JoinRunsWithSameFormatting(JoinRunsOptions options)
ParameterTypeDescription
optionsJoinRunsOptionsAdditional options

Return Value

Number of joins performed. When N adjacent runs are being joined they count as N - 1 joins.

Examples

Shows how to join runs with the same formatting while ignoring redundant and insignificant attributes.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create runs with identical visible formatting but some internal differences.
builder.Font.Name = "Arial";
builder.Font.Size = 12;
builder.Write("Hello ");
builder.Write("world");

// Verify runs before join.
Assert.That(doc.FirstSection.Body.FirstParagraph.Runs.Count, Is.EqualTo(2));
Assert.That(doc.FirstSection.Body.FirstParagraph.Runs[0].Text, Is.EqualTo("Hello "));
Assert.That(doc.FirstSection.Body.FirstParagraph.Runs[1].Text, Is.EqualTo("world"));

// Configure options to ignore redundant and insignificant attributes during join.
JoinRunsOptions options = new JoinRunsOptions();
options.IgnoreRedundant = true; // Ignore redundant run properties that don't affect appearance.
options.IgnoreInsignificant = true; // Ignore insignificant differences like whitespace-only runs.

// Join runs that have the same visible formatting using the extended options.
doc.FirstSection.Body.FirstParagraph.JoinRunsWithSameFormatting(options);

// Verify that runs were successfully joined.
Assert.That(doc.FirstSection.Body.FirstParagraph.Runs.Count, Is.EqualTo(1));
Assert.That(doc.FirstSection.Body.FirstParagraph.Runs[0].Text, Is.EqualTo("Hello world"));

doc.Save(ArtifactsDir + "Paragraph.JoinRunsWithSameFormattingWithOptions.docx");

See Also