We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Applying custom styles

Hi there,

I am applying the custom styles(named as SpecText 1-9) to document dynamically and applying these styles to content of the document based on the heading style paragraph, like:

1 Heading1
content of Heading1 // applying SpecText 1 style
1.1 Heading2
content of Heading 2 // applying SpecText 2 style
1.1.1 Heading 3
content of Heading3 // applying SpecText 3 style
1.2 Heading2
content of Heading2 // applying SpecText 2 style
2 Heading1
content of Heading 1 // applying SpecText 1 style
2.1 Heading2
content of Heading 2 // applying SpecText 2 style

To applying custom styles, i am using the attached code.

I am adding SpecText 1-9 styles only to the document and applying these styles to the document based on Heading style. but here document applying SpecText 1_0 insted of SpecText 1 and SpecText 2_0 insted of SpecText 2 and soon…

Could you please suggest me how to overcome this…

Thanks,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. This can occur if you concatenate documents or import nodes from one document to another with ImportFormatMode.KeepSourceFormatting. In this case, if the destination document already contains such style, name of the style from the source document is copied into the destination document with different name.

Could you please create simple application, which will allow me to reproduce the problem on my side? I will check the issue and provide you more information.

Best regards.

Hi Alexey,

Thanks for your response. here is the sample document to reproduce this issue and i attached the expected document also. Could you please check and suggest me on this, why it is applying style SpecText 1+Calibri insted of SpecText 1.

Document doc = new Document(@“D:\Final\src.doc”);
AddStylesToDocument(doc); // these 2 methods attached in previous thread
ApplyStylesToDocument(doc);
doc.Save(@“D:\Final\out.doc”);

Thanks,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. MS Word shows name of the style as “SpecText 1+Calibri” because all Runs in this paragraph has this font specified explicitly. If you need that font of the text inherits formatting of the paragraph style, you should clear formatting. Please see the following code:

// Clear formating of the paragraph

para.ParagraphBreakFont.ClearFormatting();

para.ParagraphFormat.ClearFormatting();

// Clear formating of the children of the paragraph.

foreach(Run run in para.Runs)

run.Font.ClearFormatting();

para.ParagraphFormat.Style = oStyle;

Hope this helps.

Best regards.

Hi Alexey,

The code what you given above is works fine for me, but i got some here issue like, if the paragraph is a hyperlink paragraph, that format also clearing. So I need to avoid ClearFormatting() on hyperlink paragraphs.

I didn’t fount any property on paragraph to identify whether the para is an hyperlink para or not? Is there any way to identify hyperlink paragraph, if not is there any workaround to fix this?

Thanks,

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. I think in this case you can try using DocumentVisitor. Please see the following code example:

Document doc = new Document("src.doc");

ClearFormatting clear = new ClearFormatting();

doc.Accept(clear);

doc.Save("out.doc");

class ClearFormatting : DocumentVisitor

{

private bool _clear = true;

public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)

{

if (fieldStart.FieldType == Aspose.Words.Fields.FieldType.FieldHyperlink)

_clear = false;

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)

{

if (fieldSeparator.FieldType == Aspose.Words.Fields.FieldType.FieldHyperlink)

_clear = false;

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)

{

if (fieldEnd.FieldType == Aspose.Words.Fields.FieldType.FieldHyperlink)

_clear = true;

return VisitorAction.Continue;

}

public override VisitorAction VisitRun(Run run)

{

if (_clear)

run.Font.ClearFormatting();

return VisitorAction.Continue;

}

public override VisitorAction VisitParagraphStart(Paragraph paragraph)

{

if (_clear)

{

paragraph.ParagraphBreakFont.ClearFormatting();

paragraph.ParagraphFormat.ClearFormatting();

paragraph.ParagraphFormat.Style = oStyle;

}

return VisitorAction.Continue;

}

}

Hope this helps.

Best regards,