问题
i am trying to add a image with the option inline with text, i already am able to place floating images around text, but i want to give the option to put them inline with text, as i read the docs it says that i can't use the wrap mode for the inline option.
My daubt is how can i set the anchor element to inline without changing all my function?
at the moment i have this:
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;
}
i already used the openxml productivity sdk but it is not specific about how i can do this, any help?
来源:https://stackoverflow.com/questions/45814121/anchor-inline-text-with-image