我目前正在尝试使用open- xml sdk 2.5 将图像添加到文字处理文档中。 对于内联图像,这对我来说效果很好,但是我从未设法使锚定图像正常工作。
为了澄清:对于嵌入式图像,我的意思是将嵌入式对象添加到图形对象,并带有锚定图像,将锚对象添加到图形对象。
我已经阅读了有关将图像添加到文字处理文档中的docs.microsoft文档,但这仅处理嵌入式图像(链接:Insert a picture into a word processing document (Open XML SDK)。我还找到了堆栈溢出答案,该示例提供了锚点的示例实现对象,问题在于此示例不起作用(链接至示例代码:anchor inline text with image。 我进一步制作了带有锚图像的Word文档,并在Microsoft生产力工具中打开了此Wordfile。然后,我尝试将生成的图像代码复制粘贴到我的项目中,但也没有成功。我也尝试调整此代码,但是它也不起作用。
我在这里提供我的内联代码和锚代码以及生成的xml文件。 如微软文档中所述,我得到了RelationshipId:
MainDocumentPart mainPart = (MainDocumentPart)targetDocPart;
imgp = mainPart.AddImagePart(ImagePartType.Png);
MemoryStream memStream = new MemoryStream(Convert.FromBase64String(data));
imgp.FeedData(memStream);
string relationshipId = targetDocPart.GetIdOfPart(imgp);
WordDrawing = GenerateInlineDrawing(relationshipId);
//Later this drawing is added to a run, paragraph and finally to the doc...
内联代码:
private Drawing GenerateInlineDrawing(string relationshipId)
{
DW.Extent extent = new DW.Extent() {Cx = 990000L, Cy = 792000L};
DW.EffectExtent effectEx = new DW.EffectExtent()
{
LeftEdge = 500L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
};
DW.DocProperties docProp = new DW.DocProperties()
{
Id = (UInt32Value) 1U,
Name = "Picture 1"
};
DW.NonVisualGraphicFrameDrawingProperties nonVis = new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() {NoChangeAspect = true});
A.Graphic graphic = new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value) 0U,
Name = "New Bitmap Image.png"
},
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 = 990000L, Cy = 792000L}),
new A.PresetGeometry(
new A.AdjustValueList()
)
{Preset = A.ShapeTypeValues.Rectangle}))
)
{Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"});
//nonVis, effectEx
return new Drawing(new DW.Inline(extent, docProp, graphic));
}
内联XML:
<w:document xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
<w:body>
<w:p w14:paraId="0E1A9AD6">
<w:pPr>
<w:rPr>
<w:lang w:val="de-DE" />
</w:rPr>
</w:pPr>
<w:r>
<w:t />
</w:r>
<w:r>
<w:rPr>
<w:lang w:val="de-DE" />
</w:rPr>
<w:drawing>
<wp:inline>
<wp:extent cx="990000" cy="792000" />
<wp:docPr id="1" name="Picture 1" />
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="0" name="New Bitmap Image.png" />
<pic:cNvPicPr />
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="R9428c323687947d3" cstate="print">
<a:extLst>
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}" />
</a:extLst>
</a:blip>
<a:stretch>
<a:fillRect />
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="990000" cy="792000" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
</w:p>
<w:p w14:paraId="01834FB7">
<w:pPr>
<w:rPr>
<w:lang w:val="de-DE" />
</w:rPr>
</w:pPr>
<w:r>
<w:t />
</w:r>
</w:p>
<w:sectPr w:rsidRPr="00A36CD5" w:rsidR="00A178D9" w:rsidSect="00DC0279">
<w:headerReference w:type="default" r:id="rId7" />
<w:footerReference w:type="default" r:id="rId8" />
<w:pgSz w:w="11907" w:h="16840" w:code="9" />
<w:pgMar w:top="1440" w:right="1077" w:bottom="1440" w:left="1440" w:header="851" w:footer="851" w:gutter="0" />
<w:cols w:space="708" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:body>
</w:document>
锚代码:
private Drawing GenerateAnchorDrawing(string relationshipId)
{
Random random = new Random();
int randomNumber = random.Next(0, 100);
DW.Extent extent = new DW.Extent() { Cx = 990000L, Cy = 792000L };
DW.EffectExtent effectEx = new DW.EffectExtent()
{
LeftEdge = 500L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
};
DW.DocProperties docProp = new DW.DocProperties()
{
Id = UInt32Value.FromUInt32((uint)randomNumber),
Name = $"Picture {randomNumber+10}"
};
DW.NonVisualGraphicFrameDrawingProperties nonVis = new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true });
A.Graphic graphic = new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = UInt32Value.FromUInt32((uint)randomNumber),
Name = $"NewBitmap{randomNumber+10}.png"
},
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 = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
)
{ Preset = A.ShapeTypeValues.Rectangle }))
)
{ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" });
DW.SimplePosition simplePos = new SimplePosition(){X = 0,Y=0};
DW.HorizontalPosition positionH = new DW.HorizontalPosition(new PositionOffset("2084375"));
positionH.RelativeFrom = HorizontalRelativePositionValues.Column;
DW.VerticalPosition positionY = new DW.VerticalPosition(new PositionOffset("628878"));
positionY.RelativeFrom = VerticalRelativePositionValues.Paragraph;
DW.Anchor anchor = new DW.Anchor(simplePos,positionH,positionY,extent,docProp,graphic);
anchor.DistanceFromTop = 0;
anchor.DistanceFromBottom = 0;
anchor.DistanceFromLeft = 114300;
anchor.DistanceFromRight = 114300;
anchor.SimplePosition = simplePos;
anchor.RelativeHeight = 251658240;
anchor.BehindDoc = true;
anchor.Locked = false;
anchor.LayoutInCell = true;
anchor.AllowOverlap = true;
anchor.EditId = "7CEAA868";
anchor.AnchorId = GetRandomHexNumber(8);
//nonVis, effectEx
return new Drawing(anchor);
}
static Random random = new Random();
public static string GetRandomHexNumber(int digits)
{
byte[] buffer = new byte[digits / 2];
random.NextBytes(buffer);
string result = String.Concat(buffer.Select(x => x.ToString("X2")).ToArray());
if (digits % 2 == 0)
return result;
return result + random.Next(16).ToString("X");
}
锚定XML:
<w:document xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
<w:body>
<w:p w14:paraId="0E1A9AD6">
<w:pPr>
<w:rPr>
<w:lang w:val="de-DE" />
</w:rPr>
</w:pPr>
<w:r>
<w:t />
</w:r>
<w:r>
<w:rPr>
<w:lang w:val="de-DE" />
</w:rPr>
<w:drawing>
<wp:anchor distT="0" distB="0" distL="114300" distR="114300" relativeHeight="251658240" behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" wp14:editId="7CEAA868" wp14:anchorId="2AC6C3E0">
<wp:simplePos x="0" y="0" />
<wp:positionH relativeFrom="column">
<wp:posOffset>2084375</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="paragraph">
<wp:posOffset>628878</wp:posOffset>
</wp:positionV>
<wp:extent cx="990000" cy="792000" />
<wp:docPr id="71" name="Picture 81" />
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="71" name="NewBitmap81.png" />
<pic:cNvPicPr />
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="Rd17f79509d4844d0" cstate="print">
<a:extLst>
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}" />
</a:extLst>
</a:blip>
<a:stretch>
<a:fillRect />
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="990000" cy="792000" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:anchor>
</w:drawing>
</w:r>
</w:p>
<w:p w14:paraId="01834FB7">
<w:pPr>
<w:rPr>
<w:lang w:val="de-DE" />
</w:rPr>
</w:pPr>
<w:r>
<w:t />
</w:r>
</w:p>
<w:sectPr w:rsidRPr="00A36CD5" w:rsidR="00A178D9" w:rsidSect="00DC0279">
<w:headerReference w:type="default" r:id="rId7" />
<w:footerReference w:type="default" r:id="rId8" />
<w:pgSz w:w="11907" w:h="16840" w:code="9" />
<w:pgMar w:top="1440" w:right="1077" w:bottom="1440" w:left="1440" w:header="851" w:footer="851" w:gutter="0" />
<w:cols w:space="708" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:body>
</w:document>
当执行锚代码并尝试打开生成的文档时,出现以下错误: ''我们很抱歉。我们无法打开Document_ReportWithImages.docx,因为发现其竞争问题。
详细信息。没有错误详细信息 位置:零件:/word/document.xml,行:0,列:0
屏幕截图:
所以问题是,如何将这些锚定图像转换成word文件:-)
答案 0 :(得分:0)
尝试以下功能。我已经使用以下功能设法在左侧的段落中插入了锚定图像。
using Wp = DocumentFormat.OpenXml.Wordprocessing;
using DWp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using OXML = DocumentFormat.OpenXml;
public static Wp.Drawing GetAnchorPicture(String imagePartId)
{
Wp.Drawing _drawing = new Wp.Drawing();
DWp.Anchor _anchor = new DWp.Anchor()
{
DistanceFromTop = (OXML.UInt32Value)0U,
DistanceFromBottom = (OXML.UInt32Value)0U,
DistanceFromLeft = (OXML.UInt32Value)0U,
DistanceFromRight = (OXML.UInt32Value)0U,
SimplePos = false,
RelativeHeight = (OXML.UInt32Value)0U,
BehindDoc = true,
Locked = false,
LayoutInCell = true,
AllowOverlap = true,
EditId = "44CEF5E4",
AnchorId = "44803ED1"
};
DWp.SimplePosition _spos = new DWp.SimplePosition()
{
X = 0L,
Y = 0L
};
DWp.HorizontalPosition _hp = new DWp.HorizontalPosition()
{
RelativeFrom = DWp.HorizontalRelativePositionValues.Column
};
DWp.PositionOffset _hPO = new DWp.PositionOffset();
_hPO.Text = "76200";
_hp.Append(_hPO);
DWp.VerticalPosition _vp = new DWp.VerticalPosition()
{
RelativeFrom = DWp.VerticalRelativePositionValues.Paragraph
};
DWp.PositionOffset _vPO = new DWp.PositionOffset();
_vPO.Text = "83820";
_vp.Append(_vPO);
DWp.Extent _e = new DWp.Extent()
{
Cx = 989965L,
Cy = 791845L
};
DWp.EffectExtent _ee = new DWp.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
};
DWp.WrapTight _wp = new DWp.WrapTight()
{
WrapText = DWp.WrapTextValues.BothSides
};
DWp.WrapPolygon _wpp = new DWp.WrapPolygon()
{
Edited = false
};
DWp.StartPoint _sp = new DWp.StartPoint()
{
X = 0L,
Y = 0L
};
DWp.LineTo _l1 = new DWp.LineTo() { X = 0L, Y = 0L };
DWp.LineTo _l2 = new DWp.LineTo() { X = 0L, Y = 0L };
DWp.LineTo _l3 = new DWp.LineTo() { X = 0L, Y = 0L };
DWp.LineTo _l4 = new DWp.LineTo() { X = 0L, Y = 0L };
_wpp.Append(_sp);
_wpp.Append(_l1);
_wpp.Append(_l2);
_wpp.Append(_l3);
_wpp.Append(_l4);
_wp.Append(_wpp);
DWp.DocProperties _dp = new DWp.DocProperties()
{
Id = 1U,
Name = "Test Picture"
};
OXML.Drawing.Graphic _g = new OXML.Drawing.Graphic();
OXML.Drawing.GraphicData _gd = new OXML.Drawing.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };
OXML.Drawing.Pictures.Picture _pic = new OXML.Drawing.Pictures.Picture();
OXML.Drawing.Pictures.NonVisualPictureProperties _nvpp = new OXML.Drawing.Pictures.NonVisualPictureProperties();
OXML.Drawing.Pictures.NonVisualDrawingProperties _nvdp = new OXML.Drawing.Pictures.NonVisualDrawingProperties()
{
Id = 0,
Name = "Picture"
};
OXML.Drawing.Pictures.NonVisualPictureDrawingProperties _nvpdp = new OXML.Drawing.Pictures.NonVisualPictureDrawingProperties();
_nvpp.Append(_nvdp);
_nvpp.Append(_nvpdp);
OXML.Drawing.Pictures.BlipFill _bf = new OXML.Drawing.Pictures.BlipFill();
OXML.Drawing.Blip _b = new OXML.Drawing.Blip()
{
Embed = imagePartId,
CompressionState = OXML.Drawing.BlipCompressionValues.Print
};
_bf.Append(_b);
OXML.Drawing.Stretch _str = new OXML.Drawing.Stretch();
OXML.Drawing.FillRectangle _fr = new OXML.Drawing.FillRectangle();
_str.Append(_fr);
_bf.Append(_str);
OXML.Drawing.Pictures.ShapeProperties _shp = new OXML.Drawing.Pictures.ShapeProperties();
OXML.Drawing.Transform2D _t2d = new OXML.Drawing.Transform2D();
OXML.Drawing.Offset _os = new OXML.Drawing.Offset()
{
X = 0L,
Y = 0L
};
OXML.Drawing.Extents _ex = new OXML.Drawing.Extents()
{
Cx = 989965L,
Cy = 791845L
};
_t2d.Append(_os);
_t2d.Append(_ex);
OXML.Drawing.PresetGeometry _preGeo = new OXML.Drawing.PresetGeometry()
{
Preset = OXML.Drawing.ShapeTypeValues.Rectangle
};
OXML.Drawing.AdjustValueList _adl = new OXML.Drawing.AdjustValueList();
_preGeo.Append(_adl);
_shp.Append(_t2d);
_shp.Append(_preGeo);
_pic.Append(_nvpp);
_pic.Append(_bf);
_pic.Append(_shp);
_gd.Append(_pic);
_g.Append(_gd);
_anchor.Append(_spos);
_anchor.Append(_hp);
_anchor.Append(_vp);
_anchor.Append(_e);
_anchor.Append(_ee);
_anchor.Append(_wp);
_anchor.Append(_dp);
_anchor.Append(_g);
_drawing.Append(_anchor);
return _drawing;
}
我将返回的Drawing
对象附加在Paragraph
中,并且可以正常工作。