public TFluent SetPresetGeometry(OpenXmlPresetGeometry geometry)
        {
            OpenXmlElement shapeProperties = this.element.GetOrCreateShapeProperties();
            PresetGeometry presetGeometry  = shapeProperties.GetFirstChild <PresetGeometry>() ?? shapeProperties.AppendChild(new PresetGeometry());

            presetGeometry.RemoveAllChildren();
            presetGeometry.Preset = geometry.ShapeType;
            if (!geometry.AdjustValues.IsDefault)
            {
                presetGeometry.AppendChild(new AdjustValueList().AppendFluent(geometry.AdjustValues.Select(av => new ShapeGuide()
                {
                    Name = av.Name, Formula = av.Formula
                })));
            }

            return(this.result);
        }
Esempio n. 2
0
        public Run GetRun(RunProperties runProperties)
        {
            var run = new Run {
                RunProperties = runProperties.CloneNode()
            };

            run.RunProperties.AppendChild(new NoProof());

            var drawing = new Drawing();
            var inline  = new Inline
            {
                DistanceFromTop    = (UInt32Value)0U,
                DistanceFromBottom = (UInt32Value)0U,
                DistanceFromLeft   = (UInt32Value)0U,
                DistanceFromRight  = (UInt32Value)0U
            };

            var nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();

            var graphicFrameLocks1 = new A.GraphicFrameLocks()
            {
                NoChangeAspect = true
            };

            graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

            nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);


            var graphic1 = new Graphic();

            graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

            var graphicData1 = new GraphicData()
            {
                Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
            };

            var picture1 = new DocumentFormat.OpenXml.Drawing.Pictures.Picture();

            picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");

            var nonVisualPictureProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureProperties();
            var nonVisualDrawingProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualDrawingProperties()
            {
                Id = (UInt32Value)1U, Name = _config.Name
            };
            var nonVisualPictureDrawingProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureDrawingProperties();

            nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
            nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);

            var blipFill1 = new Pic.BlipFill();
            var blip1     = new Blip()
            {
                Embed = _config.Name
            };
            var blipExtensionList1 = new BlipExtensionList();

            var blipExtension1 = new BlipExtension()
            {
                Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
            };

            var useLocalDpi1 = new A14.UseLocalDpi()
            {
                Val = false
            };

            useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");

            blipExtension1.Append(useLocalDpi1);

            blipExtensionList1.Append(blipExtension1);

            blip1.Append(blipExtensionList1);

            var stretch1       = new Stretch();
            var fillRectangle1 = new FillRectangle();

            stretch1.Append(fillRectangle1);

            blipFill1.Append(blip1);
            blipFill1.Append(stretch1);

            var shapeProperties1 = new DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties();

            var transform2D1 = new Transform2D();
            var offset1      = new Offset()
            {
                X = 0L, Y = 0L
            };

            transform2D1.Append(offset1);

            var presetGeometry1 = new PresetGeometry()
            {
                Preset = ShapeTypeValues.Rectangle
            };
            var adjustValueList1 = new AdjustValueList();

            presetGeometry1.Append(adjustValueList1);

            shapeProperties1.Append(transform2D1);
            shapeProperties1.Append(presetGeometry1);

            picture1.Append(nonVisualPictureProperties1);
            picture1.Append(blipFill1);
            picture1.Append(shapeProperties1);

            graphicData1.Append(picture1);

            graphic1.Append(graphicData1);

            inline.Append(nonVisualGraphicFrameDrawingProperties1);
            inline.Append(graphic1);
            drawing.Append(inline);
            run.AppendChild(drawing);
            return(run);
        }
        /// <summary>
        /// Creates an image paragraph.
        /// </summary>
        /// <param name="relationshipId">The relationship id.</param>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <returns>The <see cref="Paragraph" /> containing the image.</returns>
        private DocumentFormat.OpenXml.Wordprocessing.Paragraph CreateImageParagraph(
            string relationshipId, string name, string description, double width, double height)
        {
            // http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.extents.aspx
            // http://polymathprogrammer.com/2009/10/22/english-metric-units-and-open-xml/

            // cx (Extent Length)
            // Specifies the length of the extents rectangle in EMUs. This rectangle shall dictate the size of the object as displayed (the result of any scaling to the original object).
            // Example: Consider a DrawingML object specified as follows:
            // <… cx="1828800" cy="200000"/>
            // The cx attributes specifies that this object has a height of 1828800 EMUs (English Metric Units). end example]
            // The possible values for this attribute are defined by the ST_PositiveCoordinate simple type (§20.1.10.42).

            // cy (Extent Width)
            // Specifies the width of the extents rectangle in EMUs. This rectangle shall dictate the size of the object as displayed (the result of any scaling to the original object).
            // Example: Consider a DrawingML object specified as follows:
            // < … cx="1828800" cy="200000"/>
            // The cy attribute specifies that this object has a width of 200000 EMUs (English Metric Units). end example]
            // The possible values for this attribute are defined by the ST_PositiveCoordinate simple type (§20.1.10.42).
            var paragraph1 = new DocumentFormat.OpenXml.Wordprocessing.Paragraph
            {
                RsidParagraphAddition  = "00D91137",
                RsidRunAdditionDefault = "00AC08EB"
            };

            var run1 = new Run();

            var runProperties1 = new RunProperties();
            var noProof1       = new NoProof();

            runProperties1.AppendChild(noProof1);

            var drawing1 = new Drawing();

            var inline1 = new Inline
            {
                DistanceFromTop    = 0U,
                DistanceFromBottom = 0U,
                DistanceFromLeft   = 0U,
                DistanceFromRight  = 0U
            };
            var extent1 = new Extent {
                Cx = 5753100L, Cy = 3600450L
            };

            extent1.Cx = (long)(width * 914400);
            extent1.Cy = (long)(height * 914400);

            var effectExtent1 = new EffectExtent {
                LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L
            };
            var docProperties1 = new DocProperties {
                Id = 1U, Name = name, Description = description
            };

            var nonVisualGraphicFrameDrawingProperties1 = new NonVisualGraphicFrameDrawingProperties();

            var graphicFrameLocks1 = new GraphicFrameLocks {
                NoChangeAspect = true
            };

            graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

            nonVisualGraphicFrameDrawingProperties1.AppendChild(graphicFrameLocks1);

            var graphic1 = new Graphic();

            graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

            var graphicData1 = new GraphicData {
                Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
            };

            var picture1 = new Picture();

            picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");

            var nonVisualPictureProperties1 = new NonVisualPictureProperties();
            var nonVisualDrawingProperties1 = new NonVisualDrawingProperties
            {
                Id          = 0U,
                Name        = name,
                Description = description
            };

            var nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties();
            var pictureLocks1 = new PictureLocks {
                NoChangeAspect = true, NoChangeArrowheads = true
            };

            nonVisualPictureDrawingProperties1.AppendChild(pictureLocks1);

            nonVisualPictureProperties1.AppendChild(nonVisualDrawingProperties1);
            nonVisualPictureProperties1.AppendChild(nonVisualPictureDrawingProperties1);

            var blipFill1 = new BlipFill();

            var blip1 = new Blip {
                Embed = relationshipId
            };

            var blipExtensionList1 = new BlipExtensionList();

            var blipExtension1 = new BlipExtension {
                Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
            };

            var useLocalDpi1 = new UseLocalDpi {
                Val = false
            };

            useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");

            blipExtension1.AppendChild(useLocalDpi1);

            blipExtensionList1.AppendChild(blipExtension1);

            blip1.AppendChild(blipExtensionList1);
            var sourceRectangle1 = new SourceRectangle();

            var stretch1       = new Stretch();
            var fillRectangle1 = new FillRectangle();

            stretch1.AppendChild(fillRectangle1);

            blipFill1.AppendChild(blip1);
            blipFill1.AppendChild(sourceRectangle1);
            blipFill1.AppendChild(stretch1);

            var shapeProperties1 = new ShapeProperties {
                BlackWhiteMode = BlackWhiteModeValues.Auto
            };

            var transform2D1 = new Transform2D();
            var offset1      = new Offset {
                X = 0L, Y = 0L
            };
            var extents1 = new Extents {
                Cx = extent1.Cx, Cy = extent1.Cy
            };

            transform2D1.AppendChild(offset1);
            transform2D1.AppendChild(extents1);

            var presetGeometry1 = new PresetGeometry {
                Preset = ShapeTypeValues.Rectangle
            };
            var adjustValueList1 = new AdjustValueList();

            presetGeometry1.AppendChild(adjustValueList1);
            var noFill1 = new NoFill();

            var outline1 = new Outline();
            var noFill2  = new NoFill();

            outline1.AppendChild(noFill2);

            shapeProperties1.AppendChild(transform2D1);
            shapeProperties1.AppendChild(presetGeometry1);
            shapeProperties1.AppendChild(noFill1);
            shapeProperties1.AppendChild(outline1);

            picture1.AppendChild(nonVisualPictureProperties1);
            picture1.AppendChild(blipFill1);
            picture1.AppendChild(shapeProperties1);

            graphicData1.AppendChild(picture1);

            graphic1.AppendChild(graphicData1);

            inline1.AppendChild(extent1);
            inline1.AppendChild(effectExtent1);
            inline1.AppendChild(docProperties1);
            inline1.AppendChild(nonVisualGraphicFrameDrawingProperties1);
            inline1.AppendChild(graphic1);

            drawing1.AppendChild(inline1);

            run1.AppendChild(runProperties1);
            run1.AppendChild(drawing1);

            paragraph1.AppendChild(run1);

            return(paragraph1);
        }
        private static void AddImage(Func <FileStream> fileStreamFunc, WorksheetPart worksheetPart)
        {
            if (fileStreamFunc == null)
            {
                return;
            }

            DrawingsPart drawingsPart;
            ImagePart    imagePart;
            Extents      extents;

            using (var fileStream = fileStreamFunc())
            {
                if (fileStream == null)
                {
                    return;
                }

                drawingsPart = worksheetPart.AddNewPart <DrawingsPart>();
                imagePart    = drawingsPart.AddImagePart(ImagePartType.Png, worksheetPart.GetIdOfPart(drawingsPart));
                imagePart.FeedData(fileStream);

                Image image = Image.FromStream(fileStream);
                //http://en.wikipedia.org/wiki/English_Metric_Unit#DrawingML
                //http://stackoverflow.com/questions/1341930/pixel-to-centimeter
                //http://stackoverflow.com/questions/139655/how-to-convert-pixels-to-points-px-to-pt-in-net-c
                extents = new Extents
                {
                    Cx = (long)image.Width * (long)((float)914400 / image.HorizontalResolution),
                    Cy = (long)image.Height * (long)((float)914400 / image.VerticalResolution)
                };
                image.Dispose();
            }

            NonVisualPictureProperties pictureNonVisualPictureProperties =
                new NonVisualPictureProperties
            {
                NonVisualDrawingProperties = new NonVisualDrawingProperties
                {
                    Id          = 1025,
                    Name        = "Picture 1",
                    Description = "eventtree"
                },
                NonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties
                {
                    PictureLocks = new PictureLocks
                    {
                        NoChangeAspect     = true,
                        NoChangeArrowheads = true
                    }
                }
            };

            Stretch stretch =
                new Stretch
            {
                FillRectangle = new FillRectangle()
            };

            BlipFill blipFill = new BlipFill
            {
                Blip = new Blip
                {
                    Embed            = drawingsPart.GetIdOfPart(imagePart),
                    CompressionState = BlipCompressionValues.Print
                },
                SourceRectangle = new SourceRectangle()
            };

            blipFill.Append(stretch);

            ShapeProperties shapeProperties =
                new ShapeProperties
            {
                BlackWhiteMode = BlackWhiteModeValues.Auto,
                Transform2D    = new Transform2D
                {
                    Offset = new Offset
                    {
                        X = 0,
                        Y = 0
                    },
                    Extents = extents
                }
            };
            PresetGeometry prstGeom =
                new PresetGeometry
            {
                Preset          = ShapeTypeValues.Rectangle,
                AdjustValueList = new AdjustValueList()
            };

            shapeProperties.Append(prstGeom);
            shapeProperties.Append(new SolidFill
            {
                RgbColorModelHex = new RgbColorModelHex
                {
                    Val = Color.White.ToSimpleHexValue()
                }
            });
            DocumentFormat.OpenXml.Drawing.Outline outline = new DocumentFormat.OpenXml.Drawing.Outline
            {
                Width = 25400,
            };

            var solidFill1 = new SolidFill
            {
                RgbColorModelHex = new RgbColorModelHex
                {
                    Val = StyleSheetLibrary.BorderColor.ToSimpleHexValue()
                }
            };

            outline.Append(solidFill1);
            shapeProperties.Append(outline);

            DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture =
                new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture
            {
                NonVisualPictureProperties = pictureNonVisualPictureProperties,
                BlipFill        = blipFill,
                ShapeProperties = shapeProperties
            };

            var           iColumnId     = 11;
            var           iRowId        = 1;
            var           lColumnOffset = 0;
            var           lRowOffset    = 0;
            OneCellAnchor ocanchor      = new OneCellAnchor
            {
                FromMarker = new DocumentFormat.OpenXml.Drawing.Spreadsheet.FromMarker
                {
                    ColumnId = new ColumnId {
                        Text = iColumnId.ToString(CultureInfo.InvariantCulture)
                    },
                    ColumnOffset = new ColumnOffset {
                        Text = lColumnOffset.ToString(CultureInfo.InvariantCulture)
                    },
                    RowId = new RowId {
                        Text = iRowId.ToString(CultureInfo.InvariantCulture)
                    },
                    RowOffset = new RowOffset {
                        Text = lRowOffset.ToString(CultureInfo.InvariantCulture)
                    }
                },
                Extent = new Extent {
                    Cx = extents.Cx, Cy = extents.Cy
                }
            };

            ocanchor.Append(picture);
            ocanchor.Append(new ClientData());


            WorksheetDrawing worksheetDrawing = new WorksheetDrawing();

            worksheetDrawing.Append(ocanchor);
            Drawing drawing = new Drawing {
                Id = drawingsPart.GetIdOfPart(imagePart)
            };

            worksheetDrawing.Save(drawingsPart);

            worksheetPart.Worksheet.Append(drawing);
        }