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

Free Support Forum - aspose.com

How to applying word's build-in style to a document?

hi everybody


now,there is so many archive document in company, may be many of them has user’s custom styles, these style is not normative,because of this, we should be normalize the document’s style at the start of a project, is aspose can do this?

thanks

we prefer use the word’s build-in style to replace the user’s custom style.


please help me, thanks
Hi Pyntia,

Thanks for your inquiry.

Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting 1) to Run nodes by using Character Styles e.g. a Glyph Style, 2) to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles) and 3) you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

Please use the ParagraphFormat.Style gets or sets the paragraph style applied as shown in following code example. If you still face problem, please share your input and expected output document. We will then provide you more information about your query along with code.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Create a paragraph style and specify some formatting for it.

Style style = doc.Styles.Add(StyleType.Paragraph, "MyStyle1");

style.Font.Size = 24;

style.Font.Name = "Verdana";

style.ParagraphFormat.SpaceAfter = 12;

// Apply the paragraph style to the current paragraph in the document and add some text.

builder.ParagraphFormat.Style = style;

builder.Writeln("Hello World: MyStyle1.");

builder.Writeln("Hello World: MyStyle1.");

builder.Writeln("Hello World: MyStyle1.");

// Change to a paragraph style that has no list formatting.

builder.ParagraphFormat.Style = doc.Styles["Normal"];

builder.Writeln("Hello World: Normal.");

//Change style of first paragraph of document

Paragraph para = (Paragraph)doc.GetChild(NodeType.Paragraph, 0, true);

para.ParagraphFormat.ClearFormatting();

para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

doc.Save(MyDir + "Out.docx");


hi, Tahir


thank you for your reply

your code can be work, another requirement: i want to delete custom listtemplate in a document, so prevent end-user to select again.

is aspose.words can do it?

when i open a document which has custom listtemplate, in word, this listtemplate can be select again(look in below picture), i want to permanent delete it.

how can i do it?

word listGalleries

Hi Pyntia,


Thanks for your inquiry. You can remove custom list style from your document using Style.Remove method as shown below. Style removal has following effects on the document model:

  • All references to the style are removed from corresponding paragraphs, runs and tables.
  • If base style is removed its formatting is moved to child styles.
  • If style to be deleted has a linked style, then both of these are deleted.

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");

doc.Styles["listtemplate"].Remove();

doc.Save(MyDir + "Out.docx");



hi, Tahir


thank you for your reply

when do delete the non-buildin styles i catch some exceptions,

exception detail

this exception appear so many times.

is my code has any wrong?

Hi Pyntia,


Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

i don't know how to upload file in this forum, please download file from here.

simple document

the newest aspose.words 14.2

Hi Pyntia,


Thanks for your inquiry. Unfortunately, I am unable to download your document. Please send your document again to us.

To upload/attach a file in forum, please edit your post and click the ‘Add’ button under ‘File attachment’ section. Please check the attached image for detail.

hi,


please see the attechment.

thanks

Hi Pyntia,


Thanks for sharing the document. Please use the following code snippet to remove the custom styles from the document. Hope this helps you. Please let us know if you have any more queries.

Document doc = new
Document(MyDir + “in.doc”);<o:p></o:p>

ArrayList nonbuiltinstyles = new ArrayList();

foreach (Style style in doc.Styles)

{

if (style.Name.EndsWith("Char")) continue;

if (!style.BuiltIn && style.StyleIdentifier == StyleIdentifier.User)

{

nonbuiltinstyles.Add(style);

}

}

foreach (Style style in nonbuiltinstyles)

{

style.Remove();

}

doc.Save(MyDir + "Out.doc");

hi, Tahir


thank you very much, i can work now.

but there may be a potential problem, if custom style’s name endwith “Char” then will not be delete.

Hi Pyntia,

Thanks for your inquiry.

I have tested the scenario and have managed to reproduce the same issue at my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-9848. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-9848) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.