Ejemplo n.º 1
0
        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();
        }