Free Support Forum - aspose.com

Style.setBaseStyleName not working properly

Hi,

We are currently using Aspose Words 15.11 version for our product. Now we are evaluating Aspose Words 18.3 so that we can upgrade the Aspose version we are using in our product.

We found an issue in Style.setBaseStyleName API.

Usecase: We have two word documents one is sample and other is doclet. The requirement is to add all styles from sample to doclet along with the base style names.
We found that for some styles, the formatting attributes gets changed when base style name is set for them. Actually, they start inheriting those properties from their base style although in sample document they override those properties in their own style definition.

Please find the code sample below:

public void testSetBaseStykeNameIssue() throws Exception {

Document doclet = new Document("doclet.docx");
    Document sample = new Document("sample.docx");
  
     StyleCollection sampleStyles = sample.getStyles();

//Add all styles from sample to doclet.
    for (Style sampleStyle : sampleStyles) {
       Style styleAdded= docletStyles.addCopy(sampleStyle);
        styleAdded.setName(sampleStyle.getName());
    }
    
  StyleCollection docletStyles = doclet.getStyles();
   //Set base style names
    for (Style docletStyle : docletStyles) {
        String docletStyleName = docletStyle.getName();
        Style sampleStyle = sampleStyles.get(docletStyleName);
        if (sampleStyle != null && sampleStyle.getType() != StyleType.LIST &&
            !StringUtils.equals(sampleStyle.getName(), "Normal") &&
            !StringUtils.equals(sampleStyle.getName(), "Default Paragraph Font") &&
            !StringUtils.equals(sampleStyle.getBaseStyleName(), "Normal") &&
            !StringUtils.equals(sampleStyle.getBaseStyleName(), "Default Paragraph Font")) {
            String sampleBaseStyleName = sampleStyle.getBaseStyleName();
            if (sampleBaseStyleName != null) {
                if (("H3").equals(docletStyleName)) {
                 System.out.println("H3 line spacing before setting basestyle name: " +
                                       docletStyle.getParagraphFormat().getLineSpacing());
                }
                docletStyle.setBaseStyleName(sampleBaseStyleName);
                if (("H3").equals(docletStyleName)) {
                    System.out.println("H3 line spacing after setting basestyle name: " +
                                       docletStyle.getParagraphFormat().getLineSpacing());
                }
            }
        }
    }

}

Output: H3 line spacing before setting basestyle name: 12.0
H3 line spacing after setting basestyle name: 13.8

Please help us in resolving this issue.
Please find the sample documents in attachments.

Looking forward to your response.

Thanks
Manisha<a class="attachment" href="/uploads/discourse_instance3/13950">docs.zip</a> (330.5 KB)

@oraspose,

Thanks for your inquiry. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-16783. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for your inconvenience.

Hi,

Could you please move this post to Enterprise Support?
We need fix for this issue as soon as possible.

Thanks
Manisha

@oraspose,

Please post your request in Paid Support Helpdesk to be able to raise the priority of this issue to the Enterprise Support level. Thanks for your cooperation.

@oraspose,

Regarding WORDSNET-16783, we have completed the work on your issue and has come to a conclusion that this issue is actually not a bug in Aspose.Words. So, we will close this issue as ‘Not a Bug’. Please see the following details:

Style.BaseStyleName setter is used to organize inheritance, when you need to create a new style, which is based on a existed one. But if you take an existed style and change a BaseStyleName, it will change the hierarchy of fetching attributes and will lead to changes in style’s attributes. Direct attributes will remain the same, but inherited attributes will probably change.

Your code uses Styles.AddCopy, it adds a copy of a style with properly calculated attributes. Such copying loses information about base style, and uses style Normal (for a paragraph style) as a base style. Once attributes were calculated, there is no public functionality to change Style.BaseStyleName without changing the style’s attributes.

As far as we understand, you need to open a document, which uses a styles set from the another document. And perform this with preserving of a base style names (in order to preserve style hierarchy).

We think this code will work as expected:

Document doclet = new Document(@"D:\\Temp\\Docs\\doclet.docx");
Document sample = new Document(@"D:\\Temp\\Docs\\sample.docx");

sample.Sections.Clear();
sample.AppendDocument(doclet, ImportFormatMode.UseDestinationStyles);

sample.Save("D:\\Temp\\Docs\\18.4.docx");

The main idea of workaround is deal with whole style collection. Additionally to workaround we would like to ask customer what is purpose of your code? Do you just want to copy ALL styles from one document to another? Maybe it is better to implement method for it? We have all information about style hierarchy to do style mass copy fast and without loosing information.

However, in this case Aspose.Words just tries to follow MS Word behavior:

AddCopy() method implements MS Word “Style organizer” feature which allow to copy styles between documents. Unfortunately MS Word does not copy base styles and we have to follow the same in Aspose.Words API. So, BaseStyleName setter also follows MS Word and does not preserve inherited formatting.

Hi,

Thanks for looking into the issue.

Our requirement is specified below:

Assume we have two word documents one is sample document and other one is target document.

Sample document has following styles:
1. Style1
2. Style2
3. Style3

Target document has following style collection:
1. Style1
2. Style3
3. Style4
4. Style5
We want to apply styles from sample to target document in such a way that target document should have following style collection:
1. Style1 from sample document.
2. Style2 from sample document.
3. Style3 from sample document
4. Style4 from target document
5. Style5 from target document

Could you please suggest some way to achieve the above requirement?

Thanks
Manisha

@oraspose,

Thanks for sharing your use-case details. We will further look into the details of your requirement and will keep you posted on further updates.

Hi,

Do we have any approach/solution for the use-case described above?
Looking forward to your response.

Thanks
Manisha

@oraspose,

We request you to please elaborate your inquiry further by providing answers to a few more questions i.e. is it essential to copy style with its base style (or styles)? Also, should the destination document preserve original formatting? Or it should get it from styles being copied? What is the exact purpose of your scenario/usecase? This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly. Thanks for your cooperation.

Hi,

Please find my answers below:

  1. is it essential to copy style with its base style (or styles)?: Yes
  2. should the destination document preserve original formatting? Or it should get it from styles being copied? : It should get it from styles being copied.
  3. What is the exact purpose of your scenario/use-case? : Our product allows users to upload a sample document which can have formatting details like styles, page set up (page top ,bottom , left, right margins etc), header footer etc which user want to use across all documents.

Please let us know if you need more information.

Looking forward to your response.

Thanks
Oraspose

@oraspose,

Thanks for sharing these details. We will further look into the details of your requirement and will keep you posted on further updates.

@oraspose,

We understand your requirement and will consider to add more options to control AddCopy() method behavior.

However, please tell why do you not use Document.AutomaticallyUpdateStyles feature? It seems that it is exactly what you are looking for. You just need to pass sample file to SaveOptions.DefaultTemplate and turn Document.AutomaticallyUpdateStyles On. Please let us know about it.

Hi,

Thanks for sharing the above approach.

I actually used this approach to copy styles from sample document to target document.

      public void updateStyles()
       {                                                     
          SaveOptions saveOptions = 
                          SaveOptions.createSaveOptions(SaveFormat.DOCX);
          saveOptions.setDefaultTemplate(("Sample.docx");

          Document targetDoc = new Document("target.docx");
    
         targetDoc.setAutomaticallyUpdateSyles(true);

         targetDoc.save("DocAfterStylesApplied.docx", saveOptions);
      }

The above code just updates those styles in target document which are also present in sample document.

For example:
> Sample doc has following styles:
> 1. Normal
> 2. Default Paragpraph Font
> 3. Table Normal
> 4. NoList
> 5. Heading 1
> 6. Heading 2

Target doc has following styles:
     1. Normal 
     2. Default Paragpraph Font  
     3. Table Normal  
     4. NoList  
     5. List Paragraph  

After running above code, styles named “Normal”, “Default Paragraph Font”, “Table Normal” and “NoList” are updated in target document as these styles are common in both documents. This approach covers our partial requirement. We want to copy not only those styles which are common in both documents but also those styles which are present only in sample document.

The actual output coming from above approach is as follows:
Target document is updated to have following styles:
1. Normal (From sample)
2. Default Paragpraph Font (From sample)
3. Table Normal (From sample)
4. NoList (From sample)
5. List Paragraph (From target)

The expected output is:
Target doc should have following styles:
1. Normal (From sample)
2. Default Paragpraph Font (From sample)
3. Table Normal (From sample)
4. NoList (From sample)
5. List Paragraph
6. Heading 1 (From sample)
7. Heading 2 (From sample)

The sample documents are attached with this post for your reference.docs.zip (26.7 KB)

Note: The style names used in above example may differ from styles present in attached documents.

Please let me know if there is a way to achieve our requirement as a whole.

Thanks
Manisha

@oraspose,

Thanks for sharing these details. We will further look into the details of your requirement and will keep you posted on further updates.

Hi,

Do we have any updates on it?

Thanks
Manisha

@oraspose,

I am afraid, there is no further update available at the moment. We will inform when an update is available. We apologize for any inconvenience.

Hi Awais,

Do we have any update on this?
Looking forward to your reply.

Thanks
Manisha

@oraspose,

I am afraid, there is no further update available at the moment. We will inform you via this this thread when an update is available. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-16783) have been fixed in this Aspose.Words for .NET 18.10 update and this Aspose.Words for Java 18.10 update.

Hi,

I am in process of evaluation of Aspose.Words 18.10(java) and for this I need the documents which were attached in the original query(post). Now I am not seeing any option for downloading them.

Could you please help me in accessing the documents attached to original query.

Looking forward to your response.

Thanks
Manisha