Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Jan 16, 2024 3:58 pm

Hello Team,

I'm trying to use style from a Document for another Document.
I use Spire Doc for Java (12.1.0) on Windows environnement.

On one of my Document (htmlStyle.docx), I have style like "Heading 1" color in blue and with font Calibri 18 no bold (for exemple).
I use this file as Source Style.

I have another Document with content, it's a html file with default Style.

Im trying to replace styles from html file by these ones in Source Style.

My code look pretty simple but the main function to clone Style not work. (cloneDefaultStyleTo / cloneThemesTo)
You can check result.docx file.

Code: Select all
    //Create object of Document class
    Document html = new Document(is, FileFormat.Html);
    Document styleDocx = new Document(style, FileFormat.Docx);

    // Copy default style to another document
    styleDocx.cloneDefaultStyleTo(html);
    //styleDocx.cloneThemesTo(html);

    // Saving to tmp file outputstream
    html.saveToStream(os, FileFormat.Docx);


I also tried to merge the document while keeping the styles but they are not copied at hundred percent, the color is taken but not the font size and other properties.

My last try was browse Section and Paragraph to get a list of Style and add to the main html Document but without success.

Do you already encountered this problem?

Relin579
 
Posts: 8
Joined: Mon Jan 15, 2024 8:57 am

Wed Jan 17, 2024 2:55 am

Hello,

Thank you for your inquiry.
I have tested the Word document you provided and were able to reproduce the issue you mentioned. I have logged the issue into our bug tracking system with the ticket number SPIREDOC-10232. Our development team will investigate and fix it. Once it is resolved, I will inform you in time. Sorry for the inconvenience caused.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Feb 09, 2024 10:38 am

Hello,

Do you have any idea when a fix would be available?

Relin579
 
Posts: 8
Joined: Mon Jan 15, 2024 8:57 am

Mon Feb 12, 2024 4:11 am

Hi,

Thanks for your follow-up.
The issue with the number SPIREDOC-10232 hasn’t been solved, due to February 8 to February 17 is our Spring Festival, we have a holiday. After the holiday, I’ll urge our Dev team to speed up fixing your issue. Sorry for the inconvenience caused.

Sincerely
Abel
E-iceblue Support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Mar 04, 2024 2:31 pm

Hello, any news ?

Relin579
 
Posts: 8
Joined: Mon Jan 15, 2024 8:57 am

Tue Mar 05, 2024 3:04 am

Hello,

Thank you for following up.
Regarding the issue SPIREDOC-10232, due to the complexity of the issue, it has not been fully resolved at this time. However, please rest assured that I have urged our development team to address this issue as soon as possible. I will promptly inform you of any significant updates regarding this matter. Your understanding and support are greatly appreciated.
Thank you for your patience and cooperation.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Feb 06, 2025 3:54 pm

Hello,

Did dev team take some time for this ticket ?

Relin579
 
Posts: 8
Joined: Mon Jan 15, 2024 8:57 am

Fri Feb 07, 2025 8:05 am

Hi,

Sorry to keep you waiting.
I have further investigated your question and found that there are many factors that affect the style of a document, such as default style, paragraph style, character style, etc. Merging documents does not automatically apply certain styles to paragraphs, you need to reapply the styles. Please refer to the following code to implement your requirement.
Code: Select all
public static void main(String[] args){
    Document html = new Document();
    html.loadFromFile("data/html1.html",FileFormat.Html, XHTMLValidationType.None);
    Document doc = new Document();
    doc.loadFromFile("data/htmlStyle.docx",FileFormat.Auto);
    doc.setKeepSameFormat(false);
    for (int i = 0; i < html.getSections().get(0).getBody().getChildObjects().getCount(); i++) {
        DocumentObject documentObject = html.getSections().get(0).getBody().getChildObjects().get(i);
        if (documentObject instanceof Paragraph) {
            Paragraph paragraph = (Paragraph) documentObject;
            paragraph.getFormat().getOutlineLevel()==OutlineLevel.Level_1
            // Handle different paragraph styles
            processParagraph(paragraph, "Test titre 1", "Heading 1");
            processParagraph(paragraph, "Test titre 2", "Heading 2");
        }
        doc.getSections().get(0).getBody().getChildObjects().add(documentObject.deepClone());
    }
    doc.saveToFile("merge_Result.docx",FileFormat.Docx_2013);
}
private static void processParagraph(Paragraph paragraph, String searchText, String styleName) {
    if (paragraph.getText().equals(searchText)) {
        paragraph.getFormat().clearFormatting();
        paragraph.applyStyle(styleName);
        for (DocumentObject cObj : (Iterable<? extends DocumentObject>) paragraph.getChildObjects()) {
            if (cObj instanceof TextRange) {
                ((TextRange) cObj).getCharacterFormat().clearFormatting();
            }
        }
    }
}

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.Doc