所有形狀的集合參考迴圈通過集合

現在你已經擁有了文件中所有形狀引用的集合。使用 foreach 遍歷集合,並通過每次迭代建立一個 Blip 物件。將 Blip 物件嵌入值設定為先前從影象部件捕獲的圖片 ID 參考。還要建立一個 Stretch 物件和 FillRectangle 物件(這些並不是必需的,它們只是用於正確對齊影象)。並將每個附加到其父物件,如 XML 樹等效。

foreach (DocumentFormat.OpenXml.Office2010.Word.DrawingShape.WordprocessingShape sp in shapes2)
                              {
                                  //Wps.ShapeProperties shapeProperties1 = 
                                  A.BlipFill blipFill1 = new A.BlipFill() { Dpi = (UInt32Value)0U, RotateWithShape = true };
                                  A.Blip blip1 = new A.Blip() { Embed = temp };

                                  A.Stretch stretch1 = new A.Stretch();
                                  A.FillRectangle fillRectangle1 = new A.FillRectangle() { Left = 10000, Top = 10000, Right = 10000, Bottom = 10000 };
                                  Wps.WordprocessingShape wordprocessingShape1 = new Wps.WordprocessingShape();

                                      stretch1.Append(fillRectangle1);
                                      blipFill1.Append(blip1);
                                      blipFill1.Append(stretch1);
                                      Wps.ShapeProperties shapeProperties1 = sp.Descendants<Wps.ShapeProperties>().First();
                                      shapeProperties1.Append(blipFill1);
                                      
                              }