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.7.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 which contains adjacent runs of text with identical formatting
// This can, for example, occur if we edit one paragraph many times
Document doc = new Document(MyDir + "Rendering.docx");

// Get the number of runs our document contains
Assert.AreEqual(317, doc.GetChildNodes(NodeType.Run, true).Count);

// We can merge all nearby runs with the same formatting to reduce that number by calling JoinRunsWithSameFormatting()
// This method will also notify us of the number of run joins that took place
Assert.AreEqual(121, doc.JoinRunsWithSameFormatting());

// Get the number of runs after joining, which, together with the number of joins should add up to the original number of runs
Assert.AreEqual(196, doc.GetChildNodes(NodeType.Run, true).Count);

See Also