Ejemplo n.º 1
0
        public void CreateCBDocumentFromCBTemplate()
        {
            // Get the template
            using (WordprocessingDocument SourceTemplate = WordprocessingDocument.Open(pathtemplatedoc + TEMPLATE_NAME, false))
                // Create a new document
                using (WordprocessingDocument NewDoc = WordprocessingDocument.Create(pathnewdoc + DOCNAME_GENERATED, WordprocessingDocumentType.Document))
                {
                    /* Get the main boilerplate text from the template for the new document */
                    MainDocumentPart mdpNewDoc = NewDoc.AddMainDocumentPart();
                    mdpNewDoc.FeedData(SourceTemplate.MainDocumentPart.GetStream());
                    NewDoc.ChangeIdOfPart(mdpNewDoc, "rId" + (short)relshpids.wdrelMainDocPart);

                    foreach (IdPartPair sourceTempMainDocPart in SourceTemplate.MainDocumentPart.Parts)
                    {
                        // Transfer a select set of parts to the new document from the template
                        string PartName = "partname";
                        PartName = sourceTempMainDocPart.OpenXmlPart.GetType().Name;
                        if (IsCBDocumentPart(PartName))
                        {
                            OpenXmlPart NewPart = mdpNewDoc.AddPart(sourceTempMainDocPart.OpenXmlPart, AssignRelID(PartName));
                            NewPart.FeedData(sourceTempMainDocPart.OpenXmlPart.GetStream());
                        }
                    }

                    // Update the relationship ids
                    // Get section and update Header and Footer relationship IDs
                    Document          doc     = mdpNewDoc.Document;
                    SectionProperties secprps = doc.Body.Elements <SectionProperties>().First <SectionProperties>();
                    foreach (HeaderFooterReferenceType hfref in secprps.Elements <HeaderFooterReferenceType>())
                    {
                        if (hfref.LocalName.Contains("header"))
                        {
                            hfref.Id = "rId" + (short)relshpids.wdrelHeaderPart;
                        }

                        if (hfref.LocalName.Contains("footer"))
                        {
                            hfref.Id = "rId" + (short)relshpids.wdrelFooterPart;
                        }
                    }
                }
        }