private static void AddShape(OpenXmlElement[] shapeProperties, OpenXmlElement[] shapeStyle, OpenXmlElement[] textBodyProperties)
        {
            WordprocessingDocument docp = WordprocessingDocument.Open(@"C:\Users\Федотов РР\Desktop\test.docx", true);
            Body   bodyp             = docp.MainDocumentPart.Document.Body;
            Anchor ap                = bodyp.Descendants <Paragraph>().First().Descendants <Run>().First().Descendants <AlternateContent>().First().Descendants <AlternateContentChoice>().First().Descendants <Drawing>().First().Descendants <Anchor>().First();
            WordprocessingShape wpsp = ap.Descendants <DocumentFormat.OpenXml.Drawing.Graphic>().First().Descendants <DocumentFormat.OpenXml.Drawing.GraphicData>().First().Descendants <WordprocessingShape>().First();

            WordprocessingDocument doc = WordprocessingDocument.Create(@"C:\Users\Федотов РР\Desktop\test2.docx", WordprocessingDocumentType.Document);

            doc.AddMainDocumentPart();

            Anchor a = new Anchor();

            doc.MainDocumentPart.Document = new Document(new Body(new Paragraph(new Run(bodyp.Descendants <Paragraph>().First().Descendants <Run>().First().Descendants <AlternateContent>().First().Clone() as AlternateContent))));
            a.SimplePosition = new SimplePosition()
            {
                X = 0, Y = 0
            };
            a.HorizontalPosition = new HorizontalPosition(new PositionOffset("0"));
            a.VerticalPosition   = new VerticalPosition(new PositionOffset("0"));
            WordprocessingShape wps = new WordprocessingShape();

            a.AppendChild(new DocumentFormat.OpenXml.Drawing.Graphic(new DocumentFormat.OpenXml.Drawing.GraphicData(wps)));
            wps.AppendChild(new NonVisualDrawingShapeProperties());
            ShapeProperties    sp  = new ShapeProperties();
            ShapeStyle         ss  = new ShapeStyle();
            TextBodyProperties tbp = new TextBodyProperties();

            for (int i = 0; i < shapeProperties.Length; i++)
            {
                sp.AppendChild(shapeProperties[i].Clone() as OpenXmlElement);
            }
            for (int i = 0; i < shapeStyle.Length; i++)
            {
                ss.AppendChild(shapeStyle[i].Clone() as OpenXmlElement);
            }
            //for (int i = 0; i < textBodyProperties.Length; i++)
            //    tbp.AppendChild(textBodyProperties[i].Clone() as OpenXmlElement);
            wps.AppendChild(sp);
            wps.AppendChild(ss);
            wps.AppendChild(tbp);

            doc.Save();
            doc.Close();
        }
Beispiel #2
0
        /// <summary>
        /// Fill a textbox with a text
        /// </summary>
        /// <param name="textbox">textbox to fill</param>
        /// <param name="newText">text to fill</param>
        public static void FillTextBox(this WordprocessingShape textbox, string newText)
        {
            // Get Textbox content
            TextBoxContent content = textbox.Descendants <TextBoxContent>().FirstOrDefault();

            if (content != null)
            {
                // Retrieve old paragraph to get the current style of text
                Paragraph oldParagraph = content.Elements <Paragraph>().FirstOrDefault();

                // Create a new text
                var paragraph = CreateParagraph(oldParagraph, newText);

                // Replace old text with new one
                if (oldParagraph != null)
                {
                    content.ReplaceChild(paragraph, oldParagraph);
                }
                else
                {
                    content.Append(paragraph);
                }
            }
        }
        private static string GetShapeName(WordprocessingShape shape)
        {
            DocProperties dp = ((shape.Parent as DocumentFormat.OpenXml.Drawing.GraphicData)?.Parent as DocumentFormat.OpenXml.Drawing.Graphic)?.Parent is Anchor a && a.Elements <DocProperties>().Count() > 0 ? a.Elements <DocProperties>().First() : null;

            return(dp?.Name);
        }