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.

Fri Jul 12, 2019 7:30 am

I have to copy doc Style from one doc to another and I cannot able to do it by using spire.doc.

Scenario
I have one master doc file and I have to copy master doc file styles and need to add in another ten documents(documents has content in it).

Code
Code: Select all
 Document doc = new Document();
            doc.LoadFromFile(filepathFrom);

            Document newWord = new Document();
            newWord.LoadFromFile(filepats);
  foreach (IStyle style in doc.Styles)
            {
                newWord.Styles.Add(style);
            }
 newWord.Sections.Add(doc.Sections[0].Clone());
            var GetFiles = Path.GetFileName(filepats);
            var Output = string.Format(@"C:\****\Doc\{0}", GetFiles);
            newWord.SaveToFile(Output);


tried online example but does not work
Code: Select all
Document doc = new Document();
doc.LoadFromFile("theme.docx");

Document newWord = new Document();     
doc.CloneDefaultStyleTo(newWord);
doc.CloneThemesTo(newWord);
doc.CloneCompatibilityTo(newWord);
newWord.Sections.Add(doc.Sections[0].Clone());

newWord.SaveToFile("result.docx", FileFormat.Docx);

Note: I just only wants to copy the styles of master doc to another doc.

Cheers,
Jay

jaypatel
 
Posts: 2
Joined: Fri Jul 05, 2019 6:34 am

Fri Jul 12, 2019 7:57 am

Dear Jay,

Thanks for your inquiry.
I did an initial test with the latest Spire.Doc Pack(hot fix) Version:7.6.16 and found that the styles could be copied successfully after cloning section. If you were using an old version, I would suggest that you give the latest version a try. If the issue still happens, please provide your input Word documents as well as your generated document. Then we will look into your case accurately and help you out. You could send them to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Nina
E-iceblue support team
User avatar

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

Fri Jul 12, 2019 1:49 pm

Hi Nina,

Thank you so much for your replay, I have tried from my side but it didn't work for me.

Code: Select all
 //Master doc from where I have to copy Style position
            Document MasterDoc = new Document();
            MasterDoc.LoadFromFile(@"MasterDocPasth");

            //Destination Doc where I have to past all Styles which copied from master Doc
            Document DestinationDoc = new Document();
            DestinationDoc.LoadFromFile(@"DestinationDocPast");

            MasterDoc.CloneDefaultStyleTo(DestinationDoc);
            MasterDoc.CloneThemesTo(DestinationDoc);
            MasterDoc.CloneCompatibilityTo(DestinationDoc);
            DestinationDoc.Sections.Add(MasterDoc.Sections[0].Clone());

            //Doc save on new location           
            DestinationDoc.SaveToFile(@"NewFileDocPath", FileFormat.Docx);


This code some time copy one style or sometimes not but in my case, I have to copy all Style from my master Doc to destination Doc.

Cheers,
Jay

jaypatel
 
Posts: 2
Joined: Fri Jul 05, 2019 6:34 am

Mon Jul 15, 2019 4:02 am

Dear Jay,

Sorry for late reply as weekend.
I simulated your case but didn't encounter the style issue. Please share your input Word documents along with your generated Word document, so that we can look into your issue accurately and quickly. You could send them to us via email (support@e-iceblue.com). Don't worry, we promise to keep your documents confidential and will never publish them or use them for other purpose. Or you can remove the important data from your documents as long as the modified files could reproduce your issue. Please tell us your testing environment as well. Such as OS information (E.g. Windows 7, 64bit) and Region setting (E.g. China, Chinese). Thanks in advance.

Sincerely,
Nina
E-iceblue support team
User avatar

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

Return to Spire.Doc