Hello @manasahr
I broke up my answer and remove some of the code you can figure it out yourself, just to focus in you latests questions. This is the code just to find and modify an specific heading.
public void MainCode()
{
string partialPath = @"Headings\FindingSpecificHeading"; // Location of your input/output files
LoadLicence(); // Load your License
var doc = DocumentGet(partialPath); // Load your document
Logic(doc);
doc.Save($"{partialPath}_output.docx");
}
private void Logic(Document doc)
{
// This is whatever heading you are looking for
string headingToFind= "PURPOSE AND SCOPE";
var list = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in list)
{
paragraph.JoinRunsWithSameFormatting();
foreach (Run run in paragraph.Runs)
{
// This finds the heading
if (run.Text.Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
{
paragraph.ListFormat.ListLevelNumber = 0; // this will break level of Heading2, if you do not want it remove this line.
paragraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; // this changes the style
}
}
}
}
Here is the input file you provided with the respective output.
FindingSpecificHeading_input.docx (24.1 KB)
FindingSpecificHeading_output.docx (20.3 KB)
If you need to do any other particular changes to it, you can follow the documentation for ParagraphFormat.
Regarding your second question, here is another code same to use as base to add a new heading.
public void MainCode()
{
string partialPath = @"Headings\AddingAnSpecificHeadingNotFound"; // Location of your input/output files
LoadLicence(); // Load your License
var doc = DocumentGet(partialPath); // Load your document
Logic(doc);
doc.Save($"{partialPath}_output.docx");
}
private void Logic(Document doc)
{
// I added some extra text so there is not match
string headingToFind = "PURPOSE AND SCOPE AND SOME EXTRA WORDS";
bool headingIsMissing = true;
var list = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in list)
{
paragraph.JoinRunsWithSameFormatting();
foreach (Run run in paragraph.Runs)
{
// search for the heading
if (run.Text.Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
{
headingIsMissing = false; // we change the flag because we found the heading
}
}
}
// Solves Problem 2
if (headingIsMissing)
{
RemoveEmptyPagesOrParagraphsAtTheEnd(doc);
Paragraph lastParagraph = (Paragraph)list.LastOrDefault();
Paragraph newHeading = new Paragraph(doc);
newHeading.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
newHeading.ListFormat.ListLevelNumber = 0;
newHeading.AppendChild(new Run(doc, headingToFind));
lastParagraph.ParentNode.InsertAfter(newHeading, lastParagraph);
}
}
protected void RemoveEmptyPagesOrParagraphsAtTheEnd(Document doc)
{
// 1. Remove empty sections if persists.
while ((doc.Sections.Count > 0)
&& (doc.LastSection.Body.GetChildNodes(NodeType.Run, true).Count == 0)
&& (doc.LastSection.Body.GetChildNodes(NodeType.Shape, true).Count == 0))
doc.LastSection.Remove();
// 2. Remove empty paragraphs at the end of the document.
while ((doc.LastSection.Body.LastChild.NodeType == NodeType.Paragraph) && !doc.LastSection.Body.LastParagraph.HasChildNodes)
doc.LastSection.Body.LastParagraph.Remove();
// 3. Set Window/Orphan control option for the last paragraph.
if (doc.LastSection.Body.LastChild.NodeType == NodeType.Paragraph)
doc.LastSection.Body.LastParagraph.ParagraphFormat.WidowControl = true;
// 4. Enable Keep with next option if the last node is table.
if (doc.LastSection.Body.LastChild.NodeType == NodeType.Table)
{
Table lastTable = (Table)doc.LastSection.Body.LastChild;
NodeCollection rowParagraphs = lastTable.LastRow.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in rowParagraphs)
para.ParagraphFormat.KeepWithNext = true;
}
}
Here is the input file you provided with the respective output.
AddingAnSpecificHeadingNotFound_input.docx (24.1 KB)
AddingAnSpecificHeadingNotFound_output.docx (20.3 KB)
I hope this can help you out to achive what you wanted.