Skip to content

Commit 70f5aa3

Browse files
committed
Fix issue with WordPad OLE objects and images
1 parent 17f0b9d commit 70f5aa3

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/DocSharp.Docx/DocxToRtf/DocxToRtfConverter.Vml.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,29 @@ internal void ProcessVml(OpenXmlElement element, RtfStringWriter sb, bool ignore
2525
if (element.Descendants<V.ImageData>().FirstOrDefault() is V.ImageData imageData &&
2626
imageData.RelationshipId?.Value is string relId)
2727
{
28-
// Width and height should be in a v:shape element in the style attribute:
29-
// style="width:165.6pt;height:110.4pt;visibility:visible..."
28+
OpenXmlElement? shape;
29+
// This method supports can be called for either a picture container (w:pict)
30+
// or the underlying shape/rectangle.
31+
// Usually ImageData is contained in a shape of type 75 (picture),
32+
// but a rectangle may also be used (e.g. by WordPad for OLE objects).
33+
if (element is V.Shape || element is V.Rectangle)
34+
{
35+
shape = element;
36+
}
37+
else
38+
{
39+
shape = element.Elements<V.Shape>().FirstOrDefault() ?? element.Elements<V.Rectangle>().FirstOrDefault() as OpenXmlElement;
40+
}
3041

31-
var shape = element as V.Shape ?? element.Elements<V.Shape>().FirstOrDefault();
32-
var style = shape?.Style;
33-
if (style?.Value != null)
42+
if (shape == null)
43+
{
44+
return;
45+
}
46+
47+
// Width and height are specified in the style attribute, like this:
48+
// style="width:165.6pt;height:110.4pt;visibility:visible..."
49+
var style = shape.GetAttribute("style", "");
50+
if (style.Value != null)
3451
{
3552
var properties = new PictureProperties();
3653

@@ -135,7 +152,7 @@ private void ProcessVmlShapeProperties(Dictionary<string, string> properties, Rt
135152
}
136153
}
137154

138-
private void ProcessVmlShapeStyleProperties(Dictionary<string, string> properties, RtfStringWriter sb, V.Shape shape, long width, long height)
155+
private void ProcessVmlShapeStyleProperties(Dictionary<string, string> properties, RtfStringWriter sb, OpenXmlElement shape, long width, long height)
139156
{
140157
long left = 0;
141158
long top = 0;
@@ -231,7 +248,7 @@ private void ProcessVmlShapeStyleProperties(Dictionary<string, string> propertie
231248
sb.WriteWordWithValue("pctVertPos", ParseTwips(pctTop));
232249
}
233250

234-
var wrap = shape!.Elements<W10.TextWrap>().FirstOrDefault();
251+
var wrap = shape.Elements<W10.TextWrap>().FirstOrDefault();
235252
if (wrap != null)
236253
{
237254
if (wrap.Type != null && wrap.Type.Value == W10.WrapValues.TopAndBottom)
@@ -313,8 +330,17 @@ private void ProcessVmlShapeStyleProperties(Dictionary<string, string> propertie
313330
}
314331

315332
sb.WriteShapeProperty("shapeType", "75");
316-
sb.WriteShapeProperty("fAllowOverlap ", shape.AllowOverlap == null ? true : shape.AllowOverlap.Value);
317-
sb.WriteShapeProperty("fLayoutInCell", shape.AllowInCell == null ? true : shape.AllowInCell.Value);
333+
334+
if (shape is V.Shape shp)
335+
{
336+
sb.WriteShapeProperty("fAllowOverlap ", shp.AllowOverlap == null ? true : shp.AllowOverlap.Value);
337+
sb.WriteShapeProperty("fLayoutInCell", shp.AllowInCell == null ? true : shp.AllowInCell.Value);
338+
}
339+
else if (shape is V.Rectangle rect)
340+
{
341+
sb.WriteShapeProperty("fAllowOverlap ", rect.AllowOverlap == null ? true : rect.AllowOverlap.Value);
342+
sb.WriteShapeProperty("fLayoutInCell", rect.AllowInCell == null ? true : rect.AllowInCell.Value);
343+
}
318344

319345
if (properties.TryGetValue("mso-position-horizontal", out string? hPos))
320346
{

0 commit comments

Comments
 (0)