Resetting font does not seem to persist throughout document build

Hi there,

I’ve used the Aspose Words component for a few tasks, mainly for automated mail merges outputted as PDF’s.

I have a specific tasks at the moment that requires me to assemble a Word document from scratch as it has dynamic sections.

I’ve become somewhat familiar with the DocumentBuilder and have successfully tackled the complexities I require, however I have been yet to tackle a frustrating quirk I keep coming across.

My “Normal” style in the blank source document is font size 11pt. I have a document that is made up 8 sections, each section has a cover page which is landscape, followed by its contents in portrait.

I set the font size to 48pt on the cover page, then call the DocumentBuilder.Font.ClearFormatting() method to reset to the “Normal” style.

I then include a section break and change my orientation back to portrait and draw a table, followed by a paragraph, followed by another table. My first table correctly uses the Normal" style (11pt), however the paragraph break (between tables) and the second tables cells all use font size 48pt.

Do you have any idea why it successfully uses the “Normal” style for the first table then magically transforms back to the 48pt?

I’ve attached a sample Program to demonstrate the problem. I have also attached the “base” document I’m working from and a sample generated output file (docx).

I’m using Aspose.Words version 13.3.0.0.

Regards,

Ben

For what its worth, I’ve also tried calling the ClearFormatting method on the ParagraphFormat property too with no success.

Any thoughts?

Thanks,

Ben

Hi Ben,

Thanks for your inquiry.

Please accept my apologies for late response. ParagraphFormat.ClearFormatting method resets to default paragraph formatting. Default paragraph formatting is Normal style, left aligned, no indentation, no spacing, no borders and no shading.

Font.ClearFormatting method resets to default font formatting. The method removes all font formatting specified explicitly on the object from which Font was obtained so the font formatting will be inherited from the appropriate parent.

DocumentBuilder.Writeln method (String) inserts a string and a paragraph break into the document. Current font and paragraph formatting specified by the Font and ParagraphFormat properties are used.

DocumentBuilder.InsertParagraph method inserts a paragraph break into the document. Current paragraph formatting specified by the ParagraphFormat property is used. Breaks the current paragraph in two. After inserting the paragraph, the cursor is placed at the beginning of the new paragraph.

Moreover, the Font class contains font attributes (font name, font size, color, and so on) for an object. You do not create instances of the Font class directly. You just use Font to access the font properties of the various objects such as Run, Paragraph, Style, DocumentBuilder. Please read about ‘Font Formatting’ and ‘Paragraph Formatting’ from here:
https://docs.aspose.com/words/net/applying-formatting/
https://docs.aspose.com/words/net/document-builder-overview/

In your case, I suggest you please call DocumentBuilder.Font.ClearFormatting method before creating tables. Hope this answers your query.

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hi Tahir,

Thanks for your response.

Unfortunately, using the DocumentBuilder.Font.ClearFormatting doesn’t seem to reset the font to default font formatting.

If I add a call to DocumentBuilder.Font.ClearFormatting before creating my second table the text is in the correct font/style (Calibri 11pt).

However, the empty paragraph (between the tables) is somehow reverting back to the previous specified font style (48pt/bold), no matter if I call DocumentBuilder.Font.ClearFormatting DocumentBuilder.Font.ClearFormatting or explicitly override the Font size manually (DocumentBuilder.Font.Size = 11) it reverts to the previous manually configured font size (DocumentBuilder.Font.Size = 11)(DocumentBuilder.Font.Size = 48 / DocumentBuilder.Font.Bold = true).

Here’s my example code (also attached if this isn’t legible on this post), annotated with comments of what I think should be happening, clarified by your explanation of what each of the ClearFormatting methods do.

// Open Base Document
var doc = new Document(@"c:\Temp\SampleBase.docx");

var docBuilder = new DocumentBuilder(doc);

// Setup a Landscape Cover page
docBuilder.PageSetup.PaperSize = PaperSize.A4;
docBuilder.PageSetup.Orientation = Orientation.Landscape;

// Dump out some text to demonstrate we start with the "Normal" style
docBuilder.Writeln("Sample Document");

// Tweak our font settings and render the cover page title
docBuilder.Font.Size = 48;
docBuilder.Font.Bold = true;
docBuilder.Writeln("Test Large Text");

// Insert a section break
docBuilder.InsertBreak(BreakType.SectionBreakNewPage);

// Revert out orientation back to portrait
docBuilder.PageSetup.Orientation = Orientation.Portrait;

// Clear Font formatting (I expect font to return to "Normal" style - 11pt / Regular)
docBuilder.Font.ClearFormatting();

// Draw Table 1
docBuilder.StartTable();
docBuilder.InsertCell();
docBuilder.Write("Tbl 1 - Col 1"); // <-- This is rendered in "Normal" style (11pt)
docBuilder.InsertCell();
docBuilder.Write("Tbl 1 - Col 2"); // <-- This is rendered in "Normal" style (11pt)
docBuilder.EndRow();
docBuilder.EndTable();

// Explicitly set the font size…
docBuilder.Font.Size = 11;

// Insert Paragraph to break tables up
docBuilder.InsertParagraph(); // <-- This ignores the ClearFormatting calls and explicit Font.Size setting and sets the paragraph to 48pt / Bold

// Clear Font formatting (I expect font to return to "Normal" style - 11pt / Regular)
docBuilder.Font.ClearFormatting();

// Table 2
docBuilder.StartTable();
docBuilder.InsertCell();
docBuilder.Write("Tbl 2 - Col 1"); // <-- This is now rendered in "Normal" style (11pt) thanks to our Font.ClearFormatting call above, 
// otherwise it’d have been rendered in 48pt / Bold for some crazy reason
docBuilder.InsertCell();
docBuilder.Write("Tbl 2 - Col 2"); // <-- This is now rendered in "Normal" style (11pt) thanks to our Font.ClearFormatting call above, 
// otherwise it’d have been rendered in 48pt / Bold for some crazy reason
docBuilder.EndRow();
docBuilder.EndTable();

// Save our document…
doc.Save(@"C:\Temp\Output.doc", SaveOptions.CreateSaveOptions(SaveFormat.Docx));

I have attached the output generated by the above code (AsposeOutput.docx) and also recreated what I’d expected manaully in Word (ExpectedOutput.docx).

If you inspect the paragraph font sizes on the second page between the tables and at the end of the document you will see that the Aspose output is 48pt, when my manually generated one is 11pt. Why is the Aspose Words component reverting to an old setting, even when its been cleared and explicitly overridden?

Thanks,

Ben

Hi Ben,

Thanks for your inquiry. I have managed to reproduce the same issue at my side. I have logged this issue as WORDSNET-8087 in our issue tracking system. 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.

Hi Ben,

Further to my last post, please call Font.ClearFormatting method before inserting the section break as shown in following code snippet. This will solve your problem. Hope this helps you.

Document doc = new Document(MyDir + @"SampleBase.docx");
DocumentBuilder docBuilder = new DocumentBuilder(doc);
docBuilder.PageSetup.PaperSize = Aspose.Words.PaperSize.A4;
docBuilder.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
docBuilder.Writeln("Sample Document");
docBuilder.Font.Size = 48;
docBuilder.Font.Bold = true;
docBuilder.Writeln("Test Large Text");
docBuilder.Font.ClearFormatting();
docBuilder.InsertBreak(BreakType.SectionBreakNewPage);
docBuilder.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;
// Table 1
docBuilder.StartTable();
docBuilder.InsertCell();
docBuilder.Write("Tbl 1 - Col 1"); // <-- This is "Normal" style (11pt)
docBuilder.InsertCell();
docBuilder.Write("Tbl 1 - Col 2"); // <-- This is "Normal" style (11pt)
docBuilder.EndRow();
docBuilder.EndTable();
// Insert Paragraph to break tables up
docBuilder.InsertParagraph(); // <-- This is a 48 point empty paragraph, should be default?
// Table 2
docBuilder.StartTable();
docBuilder.InsertCell();
docBuilder.Write("Tbl 2 - Col 1"); // <-- This is also 48 point
docBuilder.InsertCell();
docBuilder.Write("Tbl 2 - Col 2"); // <-- This is a 48 point
docBuilder.EndRow();
docBuilder.EndTable();
doc.Save(MyDir + "out.docx");

Hi Tahir,

Apologies for the delay, I have been out of the office for a few days at a conference.

Something as simple as that - I’d assumed from reading the documentation I could make that call anywhere.

Kind Regards,

Ben Lewis

Hi Ben,

I hope the shared workaround helps you. We will update you via this forum thread once the issue WORDSNET-8087 is resolved. Please let us know if you have any more queries.

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

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