We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create new doc with same styles as another doc


Basically I have a doc with 3 sections that I want to split in 3 documents.
When I insert the sections in the splitted docs the style is copied (Normal Style for source doc) but the style is named Normal_0.

When I do a writeLn in the splitted docs the style applied is the Normal (default for my word doc) and not the Normal_0 as I wished.

1) So, is there anyway to copy sections and copy the style to the “Normal” style and not that Normal_0 style?

2) Is there any way to create a new doc and copy all the styles from the source doc to the destination doc? (create a new doc based on another doc or something like that)

Relevant code:
  1. Document doc = new Document(“D:\docx_test\sectiontest.docx”);
  2. // to keep only sections that aren’t annexes in the templatedoc
  3. ArrayList<Section> templateDocSections = new ArrayList<Section>();
  4. for (Section section : doc.getSections()) {
  5. String sectionTitle;
  6. sectionTitle = findSectionTitle(section);
  7. if (sectionTitle != null) {
  8. Document newDoc = new Document();
  9. DocumentBuilder dbTemp = new DocumentBuilder(newDoc);
  10. newDoc.getSections().get(0).appendContent((Section)
  11. newDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING));
  12. /*
  13. Paragraph currentParagraph = dbTemp.getCurrentParagraph();
  14. if (currentParagraph.getText().trim().isEmpty()){
  15. currentParagraph.remove();
  16. }
  17. /
  18. if (sectionTitle == null || sectionTitle.isEmpty()) {
  19. sectionTitle = Math.random() 100 + “random”;
  20. }
  21. dbTemp.moveToDocumentStart();
  22. dbTemp.writeln(“heya”);
  23. newDoc.save(“D:\docx_test\break\ + sectionTitle + “.docx”, SaveFormat.DOCX);
  24. } else {
  25. templateDocSections.add(section);
  26. }
  27. }

Hi Paulo,

Thanks for your inquiry. I believe the easiest way to do this is to simply clone the document three times and then remove the appropriate sections from each cloned document so you end up with the three sections in separate documents. Please see the code example below of how to achieve this.

Document doc = new Document("Document In.doc");

Document doc1 = (Document)doc.Clone(true);

Document doc2 = (Document)doc.Clone(true);

Document doc3 = (Document)doc.Clone(true);

//Remove the second and third sections leaving the first.



//Remove the first and third sections leaving the second



//Remove the first and second sections leaving the third




thanks, that’s a good workaround :slight_smile: