Ejemplo n.º 1
0
        public void CheckForRelationship()
        {
            // Retrieve RELATIONSHIP IDs from the document/document.xml in GLOSSARY PART/AUTOTEXT GALLERY
            // Fix Here? Content consist of the Content Control Property element AND the Content Control Content element
            XElement AutoTextContent = XElement.Parse(this.cccontent);
            IEnumerable <XAttribute> autotextPartAttribs = AutoTextContent.Descendants().Attributes();

            // LINQ over an XElement is easier than LINQ over an OpenXmlElement
            var AutoTextRelIDs = from attrb in autotextPartAttribs
                                 where attrb.Value.Contains("rId")
                                 select attrb;

            if (AutoTextRelIDs.Count() == 0)
            {
                Console.WriteLine("There are no relationship IDs in this Autotext/DocPart");
                hasrelationship = false;
            }
            else
            {
                hasrelationship = true;
                foreach (var relID in AutoTextRelIDs)
                {
                    /* Maybe, instead of adding relationship Parts I should raise an event
                     * to ADD a NEW RELATIONSHIP passing the part to the parent document
                     * AND returning the Relationship ID to the AutoText
                     * AND the newly received Rel ID is INSERTED into the AutoText content.
                     */
                    relationshipparts.Add(gdp.GetPartById(relID.Value));
                }
            }
        }
Ejemplo n.º 2
0
        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.");
                    }
                }
        }