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

Free Support Forum - aspose.com

Document.RemoveUnusedResources not removing unused styles in document

Hi,

We found some issues related to Document.RemoveUnusedResources, it’s not removing the unused styles from document as it should (and it did when we first used it in our application).
For reproducing the issue please use the attached Word file and the following code:

var document = new Document(“ToC.docx”);
var documentStyles = new List();

documentStyles.AddRange(document.Styles.Cast().Where(s => s.Font != null));
Console.WriteLine(“Document styles count before RemoveUnusedResources: {0}”, documentStyles.Count);

Console.WriteLine(“Executing RemoveUnusedResources…”);
document.RemoveUnusedResources();

documentStyles.Clear();
documentStyles.AddRange(document.Styles.Cast().Where(s => s.Font != null));

Console.WriteLine(“Document styles count after RemoveUnusedResources: {0}”, documentStyles.Count);

Console.WriteLine(“Searching Runs to find unused Styles…”);

var documentRuns = document.GetChildNodes(NodeType.Run, true).Cast();
var unusedStyles = (from style in documentStyles where documentRuns.All(r => r.Font.Style.Name != style.Name) select style).ToList();

Console.WriteLine(“Found {0} unused styles.”, unusedStyles.Count);
Console.WriteLine(“Enumerating:”);

foreach (var unusedStyle in unusedStyles)
{
Console.WriteLine(unusedStyle.Name);
}

Console.WriteLine(“Press any key to exit…”);
Console.ReadKey();


InternalId: 42442

Best regards,
Aurelian Iordache
Software Developer
IBM Romania

Hi,


Thanks for providing us details.

I believe you are using Aspose.Words component, so I am moving your thread to Aspose.Words forum where one of my fellow colleagues will help you soon there.

Thank you.

Yes, my mistake, thank you.

Best regards,
Aurelian Iordache
Software Developer
IBM Romania

This is a high importance item for our Aspose.Words use cases. Please treat this item with priority.

Thank you!

Hi there,

Thanks for your inquiry. The input document shared in this forum thread have 0kb file size. Could you please re-attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.

Hi Tahir,

Sure, I’m re-attaching the document here, it should have about 16 KB.

Best regards,
Aurelian Iordache
Software Developer
IBM Romania

Hi there,

Thanks for sharing the document. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-13232. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi,

In one of our business logic scenarios we need to get the used styles from a document and then create a single document from this first document and a list of other documents using the styles we gathered from the first document and NodeImporter. That’s the scenario we need this fix for.
Until this is fixed in Aspose.Words we found a temporary fix looking like this:

var firstPrintableWordDocument = AsposeReader.ReadWordDocument(firstPrintableWordObjectContent);
//Note: in ReadWordDocument we are using RemoveUnusedResources

var
firstPrintableWordObjectStyles = new List();
firstPrintableWordObjectStyles.AddRange(firstPrintableWordDocument.Styles.Cast().Where(s => s.Font != null));

var firstPrintableWordDocumentRuns = firstPrintableWordDocument.GetChildNodes(NodeType.Run, true).Cast();

var unusedStyles = (from style in firstPrintableWordObjectStyles where firstPrintableWordDocumentRuns.All(r => r.Font.Style.Name != style.Name) select style).ToList();

foreach (var unusedStyle in unusedStyles)
{
firstPrintableWordObjectStyles.Remove(unusedStyle);
}


Do you see any issue using this code for a temporary fix ? I mean could be any adverse effects in the way we are identifying and removing the unused styles in order to use the remaining styles in the final document ?

Best regards,
Aurelian Iordache
Software Developer
IBM Romania

Hi there,

Thanks for your inquiry. Yes, your code is correct. If you want to remove styles from the document, please check following highlighted code snippet. Hope this helps you.

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

var firstPrintableWordObjectStyles = new List<Style>();

firstPrintableWordObjectStyles.AddRange(firstPrintableWordDocument.Styles.Cast<Style>().Where(s => s.Font != null));

var firstPrintableWordDocumentRuns = firstPrintableWordDocument.GetChildNodes(NodeType.Run, true).Cast<Run>();

List<Style> unusedStyles = (from style in firstPrintableWordObjectStyles where firstPrintableWordDocumentRuns.All(r => r.Font.Style.Name != style.Name) select style).ToList();

foreach (var unusedStyle in unusedStyles)

{

firstPrintableWordObjectStyles.Remove(unusedStyle);

}

foreach (Style unusedStyle in unusedStyles)

{

if (!unusedStyle.Name.Contains("Char"))

firstPrintableWordDocument.Styles[unusedStyle.Name].Remove();

}

Hi,

Thank you, we will use this temporary fix until a fix is available from Aspose.

Best regards,
Aurelian Iordache
Software Developer
IBM Romania

Hi there,

Sure, we will inform you via this forum thread once WORDSNET-13232 issue is resolved. Please let us know if you have any more queries.

Hi there,
ibmromania:
it's not removing the unused styles from document as it should (and it did when we first used it in our application).
Could you please share in which Aspose.Words version the Document.RemoveUnusedResources produce correct result?