I'm creating a method cloneHTMLDocument to get an equivalent Document of another HTMLDocument.

My problem is that the element in plain text are not exactly the same. They use a different font after cloneHTMLDocument

Maybe I forget the paragraphAttributes but I can't find them so I didn't add any.

Here is my program

Java Code:
import javax.swing.JTextPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.AttributeSet;
import java.util.Enumeration;
import javax.swing.text.StyleContext;

        
public class HTMLJTextPane {
    
    public static void main(String [] args){
        //Create myTextPane
        JFrame myFrame = new JFrame("MyTextEditor");
        myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        JTextPane myTextPane = new JTextPane();
        JScrollPane myScrollPane = new JScrollPane(myTextPane);
        myFrame.add(myScrollPane, BorderLayout.CENTER);
        myFrame.setSize(400,300);  
        myFrame.setVisible(true);
        
        //Create aTextPane
        JFrame aFrame = new JFrame("NewTextEditor");
        myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        JTextPane aTextPane = new JTextPane();
        JScrollPane aScrollPane = new JScrollPane(aTextPane);
        aFrame.add(aScrollPane, BorderLayout.CENTER);
        aFrame.setSize(400,300);
        aFrame.setLocation(500,0);
        aFrame.setVisible(true);

        aTextPane.setContentType("text/html");
        myTextPane.setContentType("text/html");
        StyledDocument myStyledDocument = myTextPane.getStyledDocument();
        
        //2  Create styles
        Style myStyle = myTextPane.addStyle("Bold", null);
        StyleConstants.setBold(myStyle, true);
        Style myStyle2 = myTextPane.addStyle("Italic", null);
        StyleConstants.setItalic(myStyle2, true);
        Style myStyle3 = myTextPane.addStyle("BoldItalic", myStyle);
        StyleConstants.setItalic(myStyle3, true);

        //3  Insert the text
        try{
            myStyledDocument.insertString(myStyledDocument.getLength(), "Hello World", null);
            myStyledDocument.insertString(0, "This text is bold\n", myStyle);
            myStyledDocument.insertString(5, "Coucou ", null);
            myStyledDocument.insertString(5, "Black ", myStyle2);
            myStyledDocument.insertString(18, "Blue ", myStyle2);
            myStyledDocument.insertString(18, "Yellow ", myStyle3);
        }catch (BadLocationException e){
            //Do nothing
        }
        
        StyledDocument aStyledDocument = cloneHTMLDocument(myStyledDocument, myTextPane);
        aTextPane.setStyledDocument(aStyledDocument);
    }
    public static HTMLDocument cloneHTMLDocument(StyledDocument src, JTextPane srcTextPane) { 
        Element rootElement, paragraphElement, textElement, smallElement;
        SimpleAttributeSet copyAttrs;
        int startOffset, endOffset;
        String copy_string;
        
        rootElement = src.getDefaultRootElement();
        HTMLDocument copyDoc = new HTMLDocument();
        
        for (int lpParagraph=0; lpParagraph<rootElement.getElementCount(); lpParagraph++){
            paragraphElement = rootElement.getElement(lpParagraph);
            for (int lpText=0; lpText<paragraphElement.getElementCount(); lpText++) {
                textElement = paragraphElement.getElement(lpText);
                for (int lpSmall=0; lpSmall<textElement.getElementCount(); lpSmall++) {
                    
                    //Insert a Element in the new Document    
                    smallElement = textElement.getElement(lpSmall);
                    copyAttrs = new SimpleAttributeSet(smallElement.getAttributes()); 
                    startOffset = smallElement.getStartOffset();
                    endOffset = smallElement.getEndOffset();
                    
                    try{
                        copy_string = src.getText(startOffset, (endOffset-startOffset));
                        copyDoc.insertString(copyDoc.getLength(), copy_string, copyAttrs);
                    }catch (BadLocationException e){
                        //Do nothing
                    }
                }           
            }
        }
        return copyDoc;
    }
    
}