@alexey.noskov in my document having some of the list like 7.1.1,7.1.2 ex… so many list are the I need to convert this list to heading 3
Please note :based on the number like 7.1.1 ,7.1.2 ,7.2.1 ,…ex based on this number i need to identify and convert that in to heading 3
Kindly update asap.
@alexey.noskov I used below code here in my run i am not getting list number but I am n getting list text ,kindly help me asap.
string pattern = @"^\d+\.\d+\.\d+\s[A-Z-a-z]*";
// Get all paragraphs in the document
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
var isHeading = "";
foreach (Run runHeading in para.Runs)
{
isHeading = isHeading + runHeading.Text;
}
// Check if the paragraph is a number list
if (para.ListFormat.IsListItem && para.ListFormat.ListLevel.NumberStyle == NumberStyle.Arabic && para.ListFormat.ListLevel.NumberFormat != "\x0000")
{
if (Regex.IsMatch(para.GetText(), pattern))
{
// Set the paragraph style to Heading 3
para.ParagraphFormat.StyleName = "Heading 3";
}
}
}
@Princeshivananjappa You should call Document.UpdateListLabels before processing the document and then use Paragraph.ListLabel.LabelString to get the list label string.
@alexey.noskov I used below code for converting number list to heading 4. in paragraphText i am getting reveres value like text after number . Kindly help me asap.
Please find the below input and expected output file.
Expected_output_doc.docx (14.7 KB)
input_doc.docx (14.9 KB)
string pattern = @"^(\d{1})(\.)(\d{1})(\.)(\d{1})(\.)(\d{1})(\s)<*([A-Za-z])*";
doc.UpdateListLabels();
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
if (para.ListFormat.IsListItem && para.ListFormat.ListLevel.NumberStyle == NumberStyle.Arabic && para.ListFormat.ListLevel.NumberFormat != "\x0000")
{
string paragraphText = para.ToString(SaveFormat.Text).Trim();
if (Regex.IsMatch(paragraphText, pattern))
{
para.ParagraphFormat.StyleName = "Heading 4";
}
}
}
@Princeshivananjappa Actually you need to check only list label, so there is no need to get text of the whole paragraph. You can simplify your code like this:
Document doc = new Document(@"C:\Temp\in.docx");
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
doc.UpdateListLabels();
string pattern = @"(\d{1})\.(\d{1})\.(\d{1})\.(\d{1})";
foreach (Paragraph para in paragraphs)
{
if (para.ListFormat.IsListItem && para.ListFormat.ListLevel.NumberStyle == NumberStyle.Arabic)
{
if (Regex.IsMatch(para.ListLabel.LabelString, pattern))
para.ParagraphFormat.StyleName = "Heading 4";
}
}
doc.Save(@"C:\Temp\out.docx");