We have a requirement to set custom header styles so we created custom header styles as part of our .dotm template and we set that programmatically using below Open XML code. We first convert a .doc document to .docx and then apply our custom styles on it after attaching the template. Below code isnt working for .docx generated by ASPOSE.Words but works for .docx generated by Word Interop. Doesnt ASPOSE support custom header styles?
@hrnallap It is not quite clear why you use Open XML to process DOCX document. Why don’t you simply set style to the paragraph using Aspose.Words? Also, it would be great if you attach your problematic input document and expected output here for our reference.
Below is my code where I am setting custom header styles using openxml. Before we came across ASPOSE, we used OPEN XML to set custom header styles like below. But it is not working . Is it because ASPOSE doesn’t support custom header styles? If we can achieve the same via ASPOSE also , it’s fine but I see that current StyleIdentifier only takes Heading styles until Heading9 . Can you help with ASPOSE code to achieve below behaviour ? Here, we are trying to set our custom styles Heading 11,12 and 13 based on no. of dots in Headings via Open XML. I will need to set similar custom styles using ASPOSE as well.
//Working code for highlighting text with headings 11,12,13
foreach (DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph in body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>())
{
// Check if the paragraph text matches the section pattern
if (Regex.IsMatch(paragraph.InnerText, @"^\d+(\.\d{1})+\s*"))
{
// Apply the desired style to the paragraph without numbering
paragraph.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties(
new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId() { Val = "Heading11" }
);
}
if (Regex.IsMatch(paragraph.InnerText, @"^\d+(\.\d{1}\.\d{1})+\s*"))
{
// Apply the desired style to the paragraph without numbering
paragraph.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties(
new ParagraphStyleId() { Val = "Heading12" }
);
}
if (Regex.IsMatch(paragraph.InnerText, @"^\d+(\.\d{1}\.\d{1}\.\d{1})+\s*"))
{
// Apply the desired style to the paragraph without numbering
paragraph.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties(
new ParagraphStyleId() { Val = "Heading13" }
);
}
}
@hrnallap StyleIdentifier
enumeration contain only default aka built-in MS Word document styles. Since your style is custom and can have any name, you should refer it by it’s name. Here is an equivalent of your code using Aspose.Words:
//Working code for highlighting text with headings 11,12,13
foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
{
string paraText = paragraph.ToString(SaveFormat.Text).Trim();
// Check if the paragraph text matches the section pattern
if (Regex.IsMatch(paraText, @"^\d+(\.\d{1})+\s*"))
{
// Apply the desired style to the paragraph without numbering
paragraph.ParagraphFormat.StyleName = "Heading11";
}
if (Regex.IsMatch(paraText, @"^\d+(\.\d{1}\.\d{1})+\s*"))
{
// Apply the desired style to the paragraph without numbering
paragraph.ParagraphFormat.StyleName = "Heading12";
}
if (Regex.IsMatch(paraText, @"^\d+(\.\d{1}\.\d{1}\.\d{1})+\s*"))
{
// Apply the desired style to the paragraph without numbering
paragraph.ParagraphFormat.StyleName = "Heading13";
}
}
Thank you. Used your code and got below error. Although name matches with the style name in template, its not able to detect looks like.
@hrnallap The exception occurs because the specified style is not available in the document. If I understand correctly, the styles are defined in another document (template). You can use Document.CopyStylesFromTemplate method to copy the styles from template into the document.
Thank you so much for your suggestions. ASPOSE code worked to update my custom styling but its going and updating numbers with “.” in tables also with header styling although its not really heading. My Open XML code omits this table update. Can you pls help ?
@hrnallap Could you please attach your input, the problematic output and the expected output documents here for our reference? We will check the issue and provide you more information. Unfortunately, screenshots does not allow to analyze the issue.