Hi,
We are using Aspose words for a quite a long time.
Recently got into a problem with one of our clients requirement.
After the document is generated, our client wants to open the document and change the style using the modify option, inside one of styles (right click on a style, and select modify).
the expectation is the current style should be applied across the document for a selected style on a line pointed by cursor.
image.png (59.8 KB)
Thank you,
Regards,
Kamaraju.
@kamaraju2407 Could you please attach your document here for testing and provide code you are using for document generation? The problem might occur because an explicit formatting is applied to text. This formatting overrides formatting set in the style.
Hi,
Sorry for the late reply.
Can you please explain bit more, on explicit formatting?
Please find attached the document.
FramsiktExport (84).docx (22.2 KB)
Also attached the code snippets.
step 1.JPG (114.9 KB)
step 2.JPG (146.9 KB)
step 3.JPG (111.9 KB)
Formatting Font across the dodument with this option.png (210.1 KB)
Thank you.
Regards,
Kamaraju.
@kamaraju2407 In your code you apply formatting using DocumentBuilder.Font
property. In this case formatting is applied to the inserted text explicitly. In your case you should use styles to apply formatting. For example see the following simple code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create style forbold text.
Style bold = doc.Styles.Add(StyleType.Paragraph, "BoldParagraph");
bold.Font.Bold = true;
// Not insert text with bold and normal style.
builder.ParagraphFormat.Style = bold;
builder.Writeln("This is bold paragraph.");
// Set normal style.
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Write("This is normal style text");
doc.Save(@"C:\Temp\out.docx");
Hi Alexey,
Thank you for your response.
I will try the change you have suggested and get back to you.
Thank you.
Regards,
Kamaraju.
1 Like
Hi Alexey,
We are still trying out the above solutions which you have suggested.
We have got one more doubt.
How can i create a new style with style based on Normal, like shown in below image?
image.png (59.3 KB)
Appreciate you help in this regard.
Thank you.
Regards,
Kamaraju.
@kamaraju2407 You can use Style.BaseStyleName property to achieve this. For example see the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a style.
Style myStyle = doc.Styles.Add(StyleType.Paragraph, "MyCustomStyle");
myStyle.BaseStyleName = doc.Styles[StyleIdentifier.Normal].Name;
myStyle.Font.Bold = true;
builder.ParagraphFormat.Style = myStyle;
builder.Write("This paragraph has a custom style based on normal style.");
doc.Save(@"C:\Temp\out.docx");
Thank you Alexey. Will try it out.
Thank you.
Regards,
Kamaraju.
1 Like
Hi Alexey,
I am trying to create a table in CK editor, as shown in the below screenshot-
image.png (43.5 KB)
I need to apply custom style to the table inside the Ck editor.
I have tried with some of your previous code snippets, with builder.inserthtml(…) at last, but could not achieve.
image.png (47.7 KB)
Please guide me on how to do it.
Thank you.
Regards,
Kamaraju.
Forgot to mention, trying to get content from CK editor into word doc.
Thank you,
Regards,
Kamaraju.
@kamaraju2407 You can apply table style using Table.Style property. Please, see the code example in our documentation.
Hi Alexy,
I have gone through the table styles as you have mentioned.
the problem that we have is, we want to modify the styles after the html table is rendered in the document.
I have attached step by step process below-
1.jpg (256.3 KB)
2.jpg (289.5 KB)
3.jpg (424.5 KB)
Kindly assist us to proceed further.
Thank you.
Regards,
Kamaraju.
@kamaraju2407 Here is simple code example and the result of applying the table style:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a table style.
TableStyle tableStyle = (TableStyle)doc.Styles.Add(StyleType.Table, "MyTableStyle1");
tableStyle.AllowBreakAcrossPages = true;
tableStyle.Bidi = true;
tableStyle.CellSpacing = 5;
tableStyle.BottomPadding = 20;
tableStyle.LeftPadding = 5;
tableStyle.RightPadding = 10;
tableStyle.TopPadding = 20;
tableStyle.Shading.BackgroundPatternColor = Color.AntiqueWhite;
tableStyle.Borders.Color = Color.Blue;
tableStyle.Borders.LineStyle = LineStyle.DotDash;
tableStyle.VerticalAlignment = CellVerticalAlignment.Center;
// Inset a table using InsertHtml.
builder.InsertHtml("<table><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table>");
// Get tables and apply the style.
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table t in tables)
{
t.Style = tableStyle;
}
doc.Save(@"C:\Temp\out.docx");
![](https://forum.aspose.com/uploads/default/61561)
Hi Alexey,
Thanks for the quick response.
Want to know how can i change the font and size of the cell content?
Thank you.
Regards,
Kamaraju.
@kamaraju2407 You can define font formatting in the table style, like shown in the following code:
// Define table style font formatting
tableStyle.Font.Bold = true;
tableStyle.Font.Color = Color.Red;
But you should not tat when you insert a table from HTML, text might have an explicit formatting, which overrides the table style formatting. To work this around, you can clear formatting of runs in the table. In this case font formatting from table style will be applied:
// Get tables and apply the style.
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table t in tables)
{
t.Style = tableStyle;
// clear formatting of runs in the table, so the table style formatting is applied to runs from HTML.
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
foreach (Run r in runs)
r.Font.ClearFormatting();
}
Thanks Alexey, I will try this out.
Thank you.
Regards,
Kamaraju.
1 Like
Hi Alexey,
I have tried with the change you have suggested, but it did not give the desired results.
Kindly find the attached screenshots-
Actual Document Screen shot.JPG (242.2 KB)
Document Screen shot after code changes.jpg (282.5 KB)
I am not sure where am i doing wrong.
The intention is to just change the font size without disturbing anything else.
I am attaching the complete documents-
Actual Document.docx (68.8 KB)
Document after code changes.docx (56.0 KB)
I tried something like below-
foreach (Table t in tables)
{
foreach (var r in t.Rows)
{
foreach (var c in ((Aspose.Words.Tables.Row)r).Cells)
{
((((Aspose.Words.Tables.Cell)c).FirstParagraph).ParagraphFormat).Style.Font.Size = 5;
}
}
}
this also did not help.
Kindly assist.
Thank you.
Regards,
Kamaraju.
@kamaraju2407 In your input document formatting of text in the table is specified explicitly in the runs. So if you need to change font size, you should also change in in the runs. For example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table t in tables)
{
NodeCollection tableRuns = t.GetChildNodes(NodeType.Run, true);
foreach (Run r in tableRuns)
r.Font.Size = 8;
}
doc.Save(@"C:\Temp\out.docx");
Thanks Alexey, it worked out.
Thank you.
Regards,
Kamaraju
1 Like
Hi Alexey,
I have 2 tables in my document. One is from web in Ck editor, and other one is normal.
The table style is applied to CK editor table but not to other one.
image.png (37.8 KB)
Below is my code-
![](https://forum.aspose.com/uploads/default/61849)
Your help will be highly appreciated.
Thank you.
Regards,
Kamaraju.