Applying styles to a document from a sample document

Hi,

I have to apply styles defined in a sample document to another target document.

I am using addCopy() method of StyleCollection class to achieve the same by copying all the styles from the sample document into the target document.

I use the below code for the above stated purpose:

for(int i=0;i<tempStyles.getCount();i++) {
    Style stl1=tempStyles.get(i);//tempStyles list is the list of styles from sample document
    destStyles.addCopy(stl1);//destStyles is the list of Styles from target document.

Now, I have a Heading1 style in both the sample document as well target document.
After running the above code i have 3 Heading1 styles in the target document i.e. One Heading1 style from target document and two Heading1_suffixed styles from sample document.
I am not able to understand why there are two Heading1_suffixed styles are added from sample document into the target document.

Can you help in this regard if we can retain only one style with a name without renaming the styles with same name by suffixing them?
Looking forward to your response!

Hi Praneeth,

Thanks for your inquiry. The StyleCollection.addCopy method copies a style into this collection. Style to be copied can belong to the same document as well as to different document. This method does doesn’t copy base styles.

If collection already contains a style with the same name, then new name is automatically generated by adding “_number” suffix starting from 0 e.g. “Normal_0”, “Heading 1_1” etc. Use Style.setName method for changing the name of the imported style as shown in following code snippet.

Hope this answers your query. Please let us know if you have any more queries.

Document source = new Document(MyDir + "source.docx");
Document target = new Document(MyDir + "target.docx");
Style sourceStyle = source.getStyles().get("Heading 1");
Style targetStyle = target.getStyles().addCopy(sourceStyle);
targetStyle.setName("Heading 1");
target.save(MyDir + "Out.docx");

Hi,

Not all styles are overriden. I am using the following code for the stated purpose:"

StyleCollection destStyles = doc.getStyles();
StyleCollection tempStyles = templ.getStyles();
System.out.println("style count in template:" + tempStyles.getCount());
for (int i = 0; i < tempStyles.getCount(); i++)
{
    Style stl1 = tempStyles.get(i);
    Style cpy = destStyles.addCopy(stl1);
    cpy.setName(stl1.getName());
    // stl1.
}

After running this piece of code, i can still see some of the styles having prefixes and orininal as well as new style coexist with the latter being renamed by prefixing.

Looking forward to your response!

Hi Praneeth,

Thanks for your inquiry. Could you please attach your source and target Word documents here for testing? I will investigate the issue on my side and provide you more information.

Hi Tahir,
I’ve been using the Aspose Java API’s (the latest release - 13.5.0) on my Windows 7 system with Java 7 (java version “1.7.0_21” - Java™ SE Runtime Environment (build 1.7.0_21-b11) ). Note that there are diffrences in how Aspose behaves with Word 2007 and Word 2010 documents, as per my investigations. Also attaching the sample documents which were utilized for copying styles - documents whose style attributes are copied have been labelled src with the Word version appended, etcetra. Looking forward to your response - 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-8425 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 Praneeth,

Regarding WORDSNET-8425, it is to update you that our development team has completed the analysis of this issue and has come to a conclusion that they won’t be able to implement the fix to your issue. Most likely, your issue will be closed with ‘‘Won’t Fix’’ resolution.

The simple way to fix this is to check if a style already exists in the document before doing AddCopy. Please see the following sample code:

Document source = new Document(MyDir + "src(2007).docx");
Document target = new Document(MyDir + "target(2007).docx");
foreach (Style srcStyle in source.Styles)
{
    MessageBox.Show(srcStyle.Name);
    if (!srcStyle.BuiltIn)
    {
        Style style = target.Styles[srcStyle.Name];
        if (style == null)
            target.Styles.AddCopy(srcStyle);
    }
}
target.Save(MyDir + "Out.docx");

Hi Praneeth,

Further to my last post, the problem occurs because the styles in your document have ‘linked styles’. By default if AddCopy is requested on a style that has a linked style, both of those styles are copied.

So, in your case when “Heading 1” is copied, linked character style “Heading 1 Char” is also copied. When you add “Heading 1 Char” again by using AddCopy method, Aspose.Words duplicates this existing style with _0 suffix. Another workaround could be skipping all styles that end with " Char" suffix. Hope this helps you.

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.