DocumentJoinRunsWithSameFormatting Method

Joins runs with same formatting in all paragraphs of the document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.9.0


public int JoinRunsWithSameFormatting()

Return Value

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


This is an optimization method. Some documents contain adjacent runs with same formatting. Usually this occurs if a document was intensively edited manually. You can reduce the document size and speed up further processing by joining these runs.

The operation checks every Paragraph node in the document for adjacent Run nodes having identical properties. It ignores unique identifiers used to track editing sessions of run creation and modification. First run in every joining sequence accumulates all text. Remaining runs are deleted from the document.


Shows how to join runs in a document to reduce unneeded runs.
// Open a document that contains adjacent runs of text with identical formatting,
// which commonly occurs if we edit the same paragraph multiple times in Microsoft Word.
Document doc = new Document(MyDir + "Rendering.docx");

// If any number of these runs are adjacent with identical formatting,
// then the document may be simplified.
Assert.AreEqual(317, doc.GetChildNodes(NodeType.Run, true).Count);

// Combine such runs with this method, and verify the number of run joins that will take place.
Assert.AreEqual(121, doc.JoinRunsWithSameFormatting());

// The number of joins and the number of runs we have after the join
// should add up the number of runs we had initially.
Assert.AreEqual(196, doc.GetChildNodes(NodeType.Run, true).Count);

See Also