Applying styles to a document from a sample document

Hey Tahir,
When I copy styles from an existing source to a newly created blank document, I’ve noticed that the hide-until-used styles (such as heading 3 - heading 9) don’t show up when I need to use them. For instance, in Word, when we use Heading 2, the Heading 3 style is added to the quick styles bar - however, in my newly created document, it doesn’t show up and I am forced to go to the recommend tab of the manage style options and then unhide this. Why don’t these styles behave as expected?

Hi Praneeth,

Thanks for your inquiry. I have copied the ‘Heading 1’ style from MS Word document into empty document and have not found the shared issue.

It would be great if you please share your input document along with code here for testing. I will investigate the issue on my side and provide you more information.

Hi Tahir,
I am utilizing the following code to apply styles from sampleDoc to wordDoc :

Document wordDoc = new Document();
StyleCollection sampleStyles = sampleDoc.getStyles();
for (int i = 0; i < sampleStyles.getCount(); i++)
{
    Style sampleStyle = sampleStyles.get(i);
    if (sampleStyle.getName().endsWith("Char")) continue;
    Style newStyle = wordDoc.getStyles().addCopy(sampleStyle);
    newStyle.setName(sampleStyle.getName());
}
wordDoc.save("…/…/generatedDoc.docx");

The issue is that hide-until used styles are not getting displayed even when I want to use them and I’m forced to manually unhide them. The styles themselves are getting copied, and there’s no problem with that. I’m attaching the documents for your perusal.

Hi Praneeth,

Thanks for sharing the detail.

*PraneethS:

… I am forced to go to the recommend tab of the manage style options and then unhide this. Why don’t these styles behave as expected?*

*PraneethS:

The issue is that hide-until used styles are not getting displayed even when I want to use them and I’m forced to manually unhide them. The styles themselves are getting copied, and there’s no problem with that. I’m attaching the documents for your perusal.*

Unfortunately, I have not fully understood your query. It would be great if you please share some more detail about your query what exact you want by using Aspose.Words.

If you want to add a style in the Quick Style gallery inside MS Word UI, please use Style.isQuickStyle(true) method.

Moreover, you can get a style by using Document.getStyles().getByStyleIdentifier method. Please note that when you try to get built-in style
by using getByStyleIdentifier method and the style does not exist in
the document, Aspose.Word copies this style into StyleCollection. Please see the highlighted section of following code example. This code example shows how to get the Heading 3 style from the document’s styles and copy it into new document. Hope this helps you.

Document sampleDoc = new Document(MyDir + "sampleDoc.docx");
Document wordDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(wordDoc);
StyleCollection sampleStyles = sampleDoc.getStyles();
for (int i = 0; i < sampleStyles.getCount(); i++)
{
    Style sampleStyle = sampleStyles.get(i);
    if (sampleStyle.getName().endsWith("Char")) continue;
    Style newStyle = wordDoc.getStyles().addCopy(sampleStyle);
    newStyle.setName(sampleStyle.getName());
}
Style sampleStyle = sampleDoc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_3);
Style newStyle = wordDoc.getStyles().addCopy(sampleStyle);
newStyle.setName(sampleStyle.getName());
System.out.println(sampleStyle.getName());
builder.getParagraphFormat().setStyleName("Heading 3");
builder.writeln("Some Text");
wordDoc.save(MyDir + "Out.docx");

Hi Tahir,
My issue is this: when we open a standard word document, heading 3 is not present in the quick styles gallery. But, when we use heading 2 formatting to some text in the document, heading 3 automatically appears in the quick styles. My concern is that when I use an aspose generated document whose styles have been copied from an existing document, I can’t get heading 3 (through 9) to automatically appear in the quick styles when I use heading 2. I have to go to manage styles and manually unhide it so that it appears for usage. There’s no issue with copying of the styles, but my problem is that the generated Word document doesn’t exhibit similar behaviour like a normal Word document. Do let me know if you need further clarity on this. Thanks.

Hi Praneeth,

Thanks for sharing the detail. I have managed to reproduce the same issue at my side. I have logged this issue as WORDSNET-9146 in our issue tracking system. I have linked this forum thread to the same issue and have also noticed two more issues related to your issue. I have logged these issues as follow. You will be notified via this forum thread once these issues are resolved.

WORDSNET-9146 : Style does not add in quick styles gallery automatically after using Styles.AddCopy
WORDSNET-9143 : Style does not add in quick styles gallery automatically
WORDSNET-9144 : Style.IsQuickStyle does not work if style is copied by Styles.AddCopy

We apologize for your inconvenience.

Hi Tahir,
You said that - StyleCollection.addCopy method could not copy the base styles. By base style do you mean built in styles?

If we want to change the base style formatting. could we use

Object sampleStyle.getDirectParaAttr(int)
Object sampleStyle.getDirectRunAttr(int)
sampleStyle.setParaAttr(int, Object);
sampleStyle.setRunAttr(int, Object);

If yes, provide some more details as we could not find the javadocs for the above.

Hi Praneeth,

Thanks for your inquiry.

The style that is based on another style is called base style. Please check the attached images for detail.

Hi Tahir,
For some of the styles copying this way the paragraph spacing is differing -
We would like to apply para and run attributes using following methods which appear when seeing the methods via IDE:

Object Style.getDirectParaAttr(int)
Object Style.getDirectRunAttr(int)
Style.setParaAttr(int, Object);
Style.setRunAttr(int, Object);

Could you please provide some detail and Javadocs for these? We couold not find the Javadocs for the same.

Thanks.

Hi Praneeth,

Thanks for your inquiry. The getDirectParaAttr/setParaAttr methods are internal methods and should not public. We are in communication with our development team about your query and will get back to you as soon as possible.

Thanks Tahir. It would be very helpful if we could apply para and run attributes as is from the document style.
Thanks.

Hi Praneeth,

Thanks for your inquiry.

*PraneethS:

For some of the styles copying this way the paragraph spacing is differing -
We would like to apply para and run attributes using following methods which appear when seeing the methods via IDE:
Object Style.getDirectParaAttr(int)
Object Style.getDirectRunAttr(int)
Style.setParaAttr(int, Object);
Style.setRunAttr(int, Object);

Could you please provide some detail and Javadocs for these? We couold not find the Javadocs for the same.*

I have logged your request as WORDSJAVA-877 in our issue tracking system. We will update you via this forum thread once there is any update available on this.

*PraneethS:

It would be very helpful if we could apply para and run attributes as is from the document style.*

Could you please share some more detail about your query along with input and expected output document? There might be some other way to achieve your requirement in a proper way. We will then provide you more information about your query along with code.

Hi Praneeth,

Thanks for sharing the detail. 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-10024 (Styles.AddCopy does not copy ParagraphFormat.SpaceAfter value). 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.

Regarding your query about getDirectParaAttr/setParaAttr methods, I have logged this issue as WORDSJAVA-877. We will update you via this forum thread once we have any information on this. Thanks for your patience.

Thanks Tahir. Just to add to WORDSNET-10024, the problem seems to be more than ParagraphFormat.SpaceAfter value or ParagraphFormat.LineSpacing value. The problem is with fonts as well as we can see the in the images attached to previous threads the font name and font size is also differing.

So, the problem is not only with ParagraphFormat.SpaceAfter but all the paragraph and font attributes unless Aspose could tell us the problem is not with all attributes.

We have already discussed some of these attributes applicability in the thread - Issue (with Fonts) when copying Styles or applying at style level via -

Object Style.getDirectParaAttr(int)
Object Style.getDirectRunAttr(int)
Style.setParaAttr(int, Object);
Style.setRunAttr(int, Object);

Thanks.

Hi Praneeth,

Thanks for sharing the detail.

*PraneethS:

The problem is with fonts as well as we can see the in the images attached to previous threads the font name and font size is also differing.

So, the problem is not only with ParagraphFormat.SpaceAfter but all the paragraph and font attributes unless Aspose could tell us the problem is not with all attributes.*

For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10027 (Styles.AddCopy does not copy Font Name/Size). I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved. I have logged the shared detail with WORDSNET-10027. We apologize for your inconvenience.

*PraneethS:

We have already discussed some of these attributes applicability in the thread - <a href="Issue (with Fonts) when copying Styles*

In this case, I suggest you please update the style inside Normal.dotm and attach the dotm file to your final output document. Please check the following code example for your kind reference.

Document doc = new Document("C:\\Users\\tahir\\AppData\\Roaming\\Microsoft\\Templates\\Normal.dotm");
for (Style style : doc.getStyles())
{
    if (style.getFont() != null)
    {
        style.getFont().clearFormatting();
        style.getFont().setSize(41);
        style.getFont().setName("Arial");
    }
}
doc.save(MyDir + "Out.dotm");
Document doc2 = new Document(MyDir + "in.docx");
doc2.setAttachedTemplate(MyDir + "Out.dotm");
doc2.setAutomaticallyUpdateSyles(true);
doc2.save(MyDir + "Out.docx");

Tahir: In this case, I suggest you please update
the style inside Normal.dotm and attach the dotm file to your final
output document.

Hi Tahir,
Sorry we could not understand the way you have suggested. We are not dealing with template directly. User would give us a sample document and we don’t know the template it is based on and we have to apply all style (+ template or document level attributes) to another document uploaded by the client or to the Aspose blank document. Office would not be installed on the server, so assume no Normal.dotm template is present.
We need to extract info from sample document itself and apply it to another document as is.

Thanks.

Hi Praneeth,

Thanks for sharing the detail.

*PraneethS:

This is why we brought up the issue of applying document level font and paragraph attributes or template attributes before or after adding the styles in this thread - <a href="#540987*

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

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

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

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

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

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