public void InsertAutoText(string SignatureName) { const string NEW_DOCUMENT_NAME = "Sample AutoText Insert.docx"; string oldrelIDPic = ""; DocumentFormat.OpenXml.Drawing.Blip imgblip; using (WordprocessingDocument sampleDocument = WordprocessingDocument.Create(pathnewdoc + NEW_DOCUMENT_NAME, WordprocessingDocumentType.Document)) using (WordprocessingDocument wrdTemplate = WordprocessingDocument.Open(pathtemplatedoc + TEMPLATE_NAME, false)) { MainDocumentPart mdp = sampleDocument.AddMainDocumentPart(); mdp.Document = new Document(new Body()); GlossaryDocumentPart gDocPart = wrdTemplate.MainDocumentPart.GetPartsOfType <GlossaryDocumentPart>().FirstOrDefault(); if (gDocPart != null) { GlossaryDocument gDoc = gDocPart.GlossaryDocument; if (gDoc != null) { Console.WriteLine("AutoText Entries!"); foreach (DocPart entry in gDoc.DocParts) { if (entry.DocPartProperties.Category.Gallery.Val == DocPartGalleryValues.AutoText && entry.DocPartProperties.DocPartName.Val == SignatureName) { Console.WriteLine("Entry Name ==> {0}", entry.DocPartProperties.DocPartName.Val); Console.WriteLine(entry.DocPartBody.InnerXml); int paracount = entry.DocPartBody.Descendants <Paragraph>().Count(); Console.WriteLine("Count of paragraphs ==> {0}", paracount); foreach (Paragraph entrypara in entry.DocPartBody.Descendants <Paragraph>()) { // Let's get the relationship ID if it's there int PicCount = entrypara.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().Count(); if (PicCount > 0) { imgblip = entrypara.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault(); oldrelIDPic = imgblip.Embed.Value; Console.WriteLine("Old Relationship ID ==> {0}", oldrelIDPic); imgblip.Embed.Value = "rId10"; Console.WriteLine(imgblip.Embed.Value); ImagePart newSigImg = mdp.AddImagePart(ImagePartType.Png, imgblip.Embed.Value); mdp.CreateRelationshipToPart(newSigImg, imgblip.Embed.Value); newSigImg.FeedData(gDocPart.GetPartById(oldrelIDPic).GetStream()); } mdp.Document.Body.AppendChild <Paragraph>(new Paragraph(entrypara.OuterXml)); } } } } } else { Console.WriteLine("No Glossary Document Part (AutoText Entries) found."); } } }
public void BuildDocument(string[] AutoTextName) { /* Here's where I look in the Glossary Document Part to determine it there IS AutoText * If there isn't, then there's no use in creating the List of AutoText objects! */ gdp = mdp.GlossaryDocumentPart; if (gdp != null) { foreach (string atxname in AutoTextName) { atxt = new CBAutoText { GDP = gdp, Name = atxname }; Console.WriteLine("AutoText Name ==> {0}", atxt.Name); // Create a new relationship in the NEW document with the AutoText FOUND in the template // Think about changing how this code is called. atxt.CheckForRelationship(); // Retrieve RELATIONSHIP IDs from the document/document.xml in Main document/Document being created Console.WriteLine("Count of Content Controls in this document is {0}\n", doc.Body.Descendants <SdtElement>().Count()); Console.ReadLine(); var cctrl = (from sdtCtrl in doc.Body.Descendants <SdtElement>() where sdtCtrl.SdtProperties.GetFirstChild <SdtAlias>().Val == atxt.CCName || sdtCtrl.SdtProperties.GetFirstChild <SdtAlias>().Val == atxt.Name select sdtCtrl).SingleOrDefault(); XElement cc = XElement.Parse(atxt.CCContent); IEnumerable <XAttribute> ccAttrs = cc.Descendants().Attributes(); var ccRelIDs = (from attrib in ccAttrs where attrib.Value.Contains("rId") select attrib).ToArray(); Console.WriteLine("List of IdPartPairs {0}", mdp.Parts.Count()); foreach (var item in mdp.Parts) { Console.WriteLine("RelIDs ==> {0}", item.RelationshipId); } Console.WriteLine("Found rIds {0}", ccRelIDs.Count()); foreach (var item in ccRelIDs) { Console.WriteLine("RelIDs ==> {0}", item); } Console.WriteLine(); if (ccRelIDs.Count() > 0) { foreach (var RelIDs in ccRelIDs) { oldid = RelIDs.Value; Console.WriteLine("RelIDs = {0} and oldid = {1}", RelIDs.Value, oldid); } } if (atxt.HasARelationship) { int i = 0; foreach (var RelPart in atxt.RelationshipParts) { //Establish new relationship atxt.NewRelID = mdp.CreateRelationshipToPart(RelPart); newsignature = atxt.Content.Replace(oldid, atxt.NewRelID); i++; } } if (newsignature.Length != 0) { cctrl.InnerXml = newsignature; } else { cctrl.InnerXml = atxt.Content; } } wrddoc.SaveAs(DOC_PATH_NAME); wrddoc.Close(); } }