Get the copy of style from one document to another

Hello there,
We want our style to be copied from one document to another. We’re using StyleCollection.addCopy(Style) and seeing some thing which is not solving our problem.

Attached document (Source.docx) has a style ‘Normal’, now if we try to add it to a blank document (also attached as Dest.docx),

dest.getStyles().addCopy(src.getStyles().get(“Normal”));

the added style looks like -

If we see the Source.docx defined it as -

and Source.docx also has document default settings defined at styles.xml:

So, we finally want the Style to inherit these formatting as well, while adding the style to the destination.

Our Need is to have as follows in destination document after addCopy(NormalStyle) is used:

instead of (cloned version),

Could you please help us, how can we get the desired style like below (which inherits the formatting from document default formatting etc)?

Thanks for looking into this.

We tried to use something like -
private Style copyStyle(Style styleToCopy) throws Exception {
Style dstStyle = null;
NodeImporter nodeImporter = new NodeImporter(sourceDocx, destinationDocx, ImportFormatMode.KEEP_SOURCE_FORMATTING);

if (styleToCopy.getType() == StyleType.PARAGRAPH) {
// Create an empty paragraph and assign the style.
Paragraph par = new Paragraph(styleToCopy.getDocument());
par.getParagraphFormat().setStyle(styleToCopy);

// Import paragraph to the destination document.
Paragraph newPara = (Paragraph)nodeImporter.importNode(par, true);
dstStyle = newPara.getParagraphFormat().getStyle();

} else if (styleToCopy.getType() == StyleType.CHARACTER) {
// Create an empty run and assign the style.
Run run = new Run(styleToCopy.getDocument());
run.getFont().setStyle(styleToCopy);

// Import Run to the destination document.
Run newRun = (Run)nodeImporter.importNode(run, true);
dstStyle = newRun.getFont().getStyle();

// } else if (styleToCopy.getType() == StyleType.TABLE) {
// Table table = new Table(styleToCopy.getDocument());
// table.a
// table.setStyle(styleToCopy);
//
// Table newTable = (Table)nodeImporter.importNode(table, true);
// dstStyle = newTable.getStyle();
// } else {
// TODO:
}
return dstStyle;
}

but this is not solving our problem with all styles.

To complete our details we are dealing with styles associated with nodes like:
if (node.getNodeType() == NodeType.PARAGRAPH) {
nodeStyleName = ((Paragraph)node).getParagraphFormat().getStyle().getName();
} else if (node.getNodeType() == NodeType.RUN) {
// inline-level nodes can have character formatting associated with them, but cannot have child nodes of their own.
nodeStyleName = ((Run)node).getFont().getStyle().getName();
} else if (node.getNodeType() == NodeType.TABLE) {
nodeStyleName = ((Table)node).getStyle().getName();
} else if (node.getNodeType() == NodeType.FIELD_START) {
nodeStyleName = ((FieldStart)node).getFont().getStyle().getName();
} else if (node.getNodeType() == NodeType.FIELD_END) {
nodeStyleName = ((FieldEnd)node).getFont().getStyle().getName();
} else if (node.getNodeType() == NodeType.STRUCTURED_DOCUMENT_TAG) {
nodeStyleName = ((StructuredDocumentTag)node).getContentsFont().getStyle().getName();
} else if (node.getNodeType() == NodeType.SHAPE) {
nodeStyleName = ((Shape)node).getFont().getStyle().getName();
}

So, please consider these nodes also - PARAGRAPH, RUN, TABLE, FIELD_START, FIELD_END, SDT, SHAPE, while formulating the copy as we need and discussed in previous thread.

Thanks for looking into this.

Hi there,
With continuation of our requirement, you might see that required copy has some extra spacing nodes and size nodes. That might be not needed for us.

Our requirement could be sufficed by something like:


Thanks for looking into this.

Hi Praneeth,


Thanks for your inquiry. You can use the following workaround to get the defaults of source.docx in final document:
Document srcDoc = new Document(getMyDir() + “Source.docx”);
Document dstDoc = new Document(getMyDir() + “Dest.docx”);

Document finalDoc = (Document)srcDoc.deepClone(false);
finalDoc.removeAllChildren();
finalDoc.removeUnusedResources();

finalDoc.appendDocument(dstDoc, ImportFormatMode.USE_DESTINATION_STYLES);
finalDoc.getStyles().addCopy(srcDoc.getStyles().get(“Normal”));

finalDoc.save(getMyDir() + “out.docx”);

I hope, this helps.

Best regards,