用图像锚定内联文本

时间:2017-08-22 09:41:51

标签: c# xml asp.net-web-api openxml

我正在尝试添加带有文本内联选项的图像,我已经可以在文本周围放置浮动图像了,但我想给出选项,让它们与文本内联,因为我读了文档,它说的是我无法使用换行模式进行内联选项。

我的daubt是如何在不改变所有功能的情况下将锚元素设置为内联?

目前我有这个:

 private static Drawing DrawingManager(string relationshipId, string name, int cxVal, int cyVal, string impPosition, String horizontalAlign, double? horizontalPosition, double? verticalPosition, String wrapMode)
        {
            int iWidth = 0;
            int iHeight = 0;

            iWidth = (int)Math.Round((decimal)cxVal * EMUTOPIXELCONVERTVALUE);
            iHeight = (int)Math.Round((decimal)cyVal * EMUTOPIXELCONVERTVALUE);

            // Define the reference of the image.
            DW.Anchor anchor = new DW.Anchor();


            string haPosition = impPosition;

            if (string.IsNullOrEmpty(haPosition))
            {
                haPosition = horizontalAlign;
            }

            DW.VerticalPosition vp = new DW.VerticalPosition(new DW.PositionOffset("0"))
            {
                RelativeFrom =
                    DW.VerticalRelativePositionValues.Paragraph
            };

            DW.PositionOffset positionOffset2 = new DW.PositionOffset();
            positionOffset2.Text = MathOpenXml.centimetersToEMU(verticalPosition).ToString();

            if (verticalPosition != null)
            {
                vp = new DW.VerticalPosition(positionOffset2)
                {
                    RelativeFrom = DW.VerticalRelativePositionValues.Paragraph

                };
            }

            DW.HorizontalPosition hp = new DW.HorizontalPosition(new DW.HorizontalAlignment(haPosition))
            {
                RelativeFrom =
                      DW.HorizontalRelativePositionValues.Column
            };

            if (horizontalPosition != null)
            {
                DW.PositionOffset positionOffsetConv = new DW.PositionOffset(MathOpenXml.centimetersToEMU(horizontalPosition).ToString());
                hp = new DW.HorizontalPosition(new DW.PositionOffset(MathOpenXml.centimetersToEMU(horizontalPosition).ToString()))
                {
                    RelativeFrom =
                      DW.HorizontalRelativePositionValues.Column
                };
            }

            // how the text displays with the image


            anchor.Append(new DW.SimplePosition() { X = 0L, Y = 0L });
            anchor.Append(hp);
            anchor.Append(vp);
            anchor.Append(
                new DW.Extent()
                {
                    Cx = iWidth,
                    Cy = iHeight
                }
            );
            anchor.Append(
                new DW.EffectExtent()
                {
                    LeftEdge = 0L,
                    TopEdge = 0L,
                    RightEdge = 0L,
                    BottomEdge = 0L
                }
            );

            if (wrapMode != "In Line With Text")
            {
                anchor = wrapText(anchor, wrapMode);
            }
            anchor.Append(
                new DW.DocProperties()
                {
                    Id = (UInt32Value)1U,
                    Name = name
                }
            );
            anchor.Append(
                new DW.NonVisualGraphicFrameDrawingProperties(
                      new A.GraphicFrameLocks() { NoChangeAspect = true })
            );
            anchor.Append(
                new A.Graphic(
                      new A.GraphicData(
                        new PIC.Picture(

                          new PIC.NonVisualPictureProperties(
                            new PIC.NonVisualDrawingProperties()
                            {
                                Id = (UInt32Value)0U,
                                Name = name + ".jpg"
                            },
                            new PIC.NonVisualPictureDrawingProperties()),

                            new PIC.BlipFill(
                                new A.Blip(
                                    new A.BlipExtensionList(
                                        new A.BlipExtension()
                                        {
                                            Uri =
                                            "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                        })
                                )
                                {
                                    Embed = relationshipId,
                                    CompressionState =
                                    A.BlipCompressionValues.Print
                                },
                                new A.Stretch(
                                    new A.FillRectangle())),

                          new PIC.ShapeProperties(

                            new A.Transform2D(
                              new A.Offset() { X = 0L, Y = 0L },

                              new A.Extents()
                              {
                                  Cx = iWidth,
                                  Cy = iHeight
                              }),

                            new A.PresetGeometry(
                              new A.AdjustValueList()
                            )
                            { Preset = A.ShapeTypeValues.Rectangle }
                          )
                        )
                  )
                      { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
            );

            anchor.DistanceFromTop = (UInt32Value)0U;
            anchor.DistanceFromBottom = (UInt32Value)0U;
            anchor.DistanceFromLeft = (UInt32Value)114300U;
            anchor.DistanceFromRight = (UInt32Value)114300U;
            anchor.SimplePos = false;
            anchor.RelativeHeight = (UInt32Value)251658240U;
            anchor.BehindDoc = true;
            anchor.Locked = false;
            anchor.LayoutInCell = true;
            anchor.AllowOverlap = true;


            Drawing element = new Drawing();
            element.Append(anchor);
            return element;
        }

        /// <summary>
        /// Checks how image wraps the text besides
        /// </summary>
        /// <param name="dw">drawing element(pic)</param>
        /// <param name="wrapMode">user choice, how he wants to wrap the image</param>
        public static DW.Anchor wrapText(DW.Anchor anchor, String wrapMode)
        {
            switch (wrapMode)
            {
                case "Square":
                    DW.WrapSquare wrapSquare = new DW.WrapSquare() { WrapText = DW.WrapTextValues.BothSides };
                    anchor.Append(wrapSquare);
                    break;
                case "Tight":
                    DW.WrapTight tight = new DW.WrapTight() { WrapText = DW.WrapTextValues.BothSides };
                    anchor.Append(tight);
                    break;
                case "Through":
                    DW.WrapThrough wrapThrough = new DW.WrapThrough() { WrapText = DW.WrapTextValues.BothSides };
                    anchor.Append(wrapThrough);
                    break;
                case "Top and bottom":
                    DW.WrapTopBottom wrapTopAndBottom = new DW.WrapTopBottom();
                    anchor.Append(wrapTopAndBottom);
                    break;
                case "Behind Text":
                    DW.WrapNone behindText = new DW.WrapNone(); //behind doc must be true
                    anchor.Append(behindText);
                    break;
                case "In Front Text":
                    DW.WrapNone frontText = new DW.WrapNone(); //Fron doc must be false
                    anchor.Append(frontText);
                    break;
                default:
                    anchor.Append(new DW.WrapSquare() { WrapText = DW.WrapTextValues.BothSides });
                    break;
            }
            return anchor;
        }

我已经使用过openxml的生产力sdk,但是我没有具体说明如何做到这一点,有什么帮助吗?

0 个答案:

没有答案