Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a template document that has a few paragraphs and tables with some styles associated with them.

I need to select the elements from template document based on a sequence, and then append them to the body of my new document. Below is my code that does the copying. I somehow need to copy the styles associated with the elements as well. Although some styles get applied, things like font size and table borders are not getting copied onto the new document. Any help would be much appreciated. Thanks

Dictionary<string, string> sequence = GetSequence();
            using (WordprocessingDocument templateDocument = WordprocessingDocument.Open(sourceFileLocation, false))
            {
                Body templateBody = templateDocument.MainDocumentPart.Document.Body;
                using (
                    WordprocessingDocument wordDoc = WordprocessingDocument.Create(destinationFileLocation,
                        WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
                    mainPart.Document = new Document();
                    Body wordDocDocBody = mainPart.Document.AppendChild(new Body());
                   //don't think the below two lines work as I intended.
                    ThemePart themePart1 = templateDocument.MainDocumentPart.ThemePart;
                    mainPart.AddPart(themePart1);

                    foreach (var item in sequence)
                    {
                     var block = templateBody.Elements().TakeWhile(x => x.InnerText == item.Key);
                        foreach (var blockItem in block)
                        {
                            wordDocBody.Append(blockItem.CloneNode(true));
                        }
                    }
                }
            }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900