1

我正在尝试使用 OpenXML 生成包含图像的 PowerPoint 文件。不幸的是,它不起作用。图像未显示。我检查了使用 OpenXML 生产力工具生成的文件,并分别解压缩了文件内容。该文件本身包含 /ppt/media/image.png 中的图像,它应该显示在第二张幻灯片中。

这是我的代码:

private void InsertSlide(string chartString, int position, string title, string text = "")
{
    if (m_presentation == null || title == null || m_presentation.PresentationPart == null)
        return;

    var slide = new Slide(new CommonSlideData(new ShapeTree()));
    var nonVisualProperties =
       slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties());
       nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties { Id = 1, Name = "" };
       nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties();
       nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
       slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties());


    var slidePart = m_presentation.PresentationPart.AddNewPart<SlidePart>();
    var imagePart = slidePart.AddImagePart(ImagePartType.Png, "irgendeinscheiss");   

    //var imageStream = new MemoryStream(Convert.FromBase64String(chartString));
    using (var imageStream = new FileStream(@"C:\Users\DA\Desktop\Charts\1_Chart2_01.png", FileMode.Open))
    {
         imageStream.Position = 0;
         imagePart.FeedData(imageStream);
    }

    slide.Save(slidePart);

    var slideIdList = m_presentation.PresentationPart.Presentation.SlideIdList;
    uint maxSlideId = 1;
    SlideId prevSlideId = null;
    foreach (SlideId slideId in slideIdList.ChildElements)
    {
        if (slideId.Id > maxSlideId)
            maxSlideId = slideId.Id;
        position--;

        if (position == 0)
            prevSlideId = slideId;
    }

    maxSlideId++;
    SlidePart lastSlidePart;

    if (prevSlideId != null)
          lastSlidePart = (SlidePart)m_presentation.PresentationPart.GetPartById(prevSlideId.RelationshipId);
    else
          lastSlidePart = (SlidePart)m_presentation.PresentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId);

    if (lastSlidePart.SlideLayoutPart != null)
         slidePart.AddPart(lastSlidePart.SlideLayoutPart);

    var newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
    newSlideId.Id = maxSlideId;
    newSlideId.RelationshipId = m_presentation.PresentationPart.GetIdOfPart(slidePart);
    m_presentation.PresentationPart.Presentation.Save();
}

我错过了什么吗?也许是关系?在查找了 232243 千个不同的示例之后,我仍然停留在这一点上。谢谢!

4

1 回答 1

0

我认为您需要将图像添加到 slide.CommonSlideData

  public Slide InsertSlide(PresentationPart presentationPart, string layoutName) 
   { 
       UInt32 slideId = 256U; 


       // Get the Slide Id collection of the presentation document 
       var slideIdList = presentationPart.Presentation.SlideIdList; 


       if (slideIdList == null) 
       { 
           throw new NullReferenceException("The number of slide is empty, please select a ppt with a slide at least again"); 
       } 


       slideId += Convert.ToUInt32(slideIdList.Count()); 


       // Creates an Slide instance and adds its children. 
       Slide slide = new Slide(new CommonSlideData(new ShapeTree())); 


       SlidePart slidePart = presentationPart.AddNewPart<SlidePart>(); 
       slide.Save(slidePart); 

       // Get SlideMasterPart and SlideLayoutPart from the existing Presentation Part 
       SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First(); 
       SlideLayoutPart slideLayoutPart = slideMasterPart.SlideLayoutParts.SingleOrDefault 
           (sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName, StringComparison.OrdinalIgnoreCase)); 
       if (slideLayoutPart == null) 
       { 
           throw new Exception("The slide layout " + layoutName + " is not found"); 
       } 


       slidePart.AddPart<SlideLayoutPart>(slideLayoutPart); 


       slidePart.Slide.CommonSlideData = (CommonSlideData)slideMasterPart.SlideLayoutParts.SingleOrDefault( 
           sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone(); 


       // Create SlideId instance and Set property 
       SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild<SlideId>(new SlideId()); 
       newSlideId.Id = slideId; 
       newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); 


       return GetSlideByRelationShipId(presentationPart, newSlideId.RelationshipId); 
   } 

   /// <summary> 
   /// Get Slide By RelationShip ID 
   /// </summary> 
   /// <param name="presentationPart">Presentation Part</param> 
   /// <param name="relationshipId">Relationship ID</param> 
   /// <returns>Slide Object</returns> 
   private static Slide GetSlideByRelationShipId(PresentationPart presentationPart, StringValue relationshipId) 
   { 
       // Get Slide object by Relationship ID 
       SlidePart slidePart = presentationPart.GetPartById(relationshipId) as SlidePart; 
       if (slidePart != null) 
       { 
           return slidePart.Slide; 
       } 
       else 
       { 
           return null; 
       } 
   } 

Public void InsertImageInLastSlide(Slide slide, string imagePath, string imageExt) 
 { 
     // Creates an Picture instance and adds its children. 
     P.Picture picture = new P.Picture(); 
     string embedId = string.Empty; 
     embedId = "rId" + (slide.Elements().Count() + 915).ToString(); 
     P.NonVisualPictureProperties nonVisualPictureProperties = new P.NonVisualPictureProperties( 
         new P.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture 5" }, 
         new P.NonVisualPictureDrawingProperties(new A.PictureLocks() { NoChangeAspect = true }), 
         new ApplicationNonVisualDrawingProperties()); 


     P.BlipFill blipFill = new P.BlipFill(); 
     Blip blip = new Blip() { Embed = embedId }; 


     // Creates an BlipExtensionList instance and adds its children 
     BlipExtensionList blipExtensionList = new BlipExtensionList(); 
     BlipExtension blipExtension = new BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; 


     UseLocalDpi useLocalDpi = new UseLocalDpi() { Val = false }; 
     useLocalDpi.AddNamespaceDeclaration("a14", 
         "http://schemas.microsoft.com/office/drawing/2010/main"); 


     blipExtension.Append(useLocalDpi); 
     blipExtensionList.Append(blipExtension); 
     blip.Append(blipExtensionList); 


     Stretch stretch = new Stretch(); 
     FillRectangle fillRectangle = new FillRectangle(); 
     stretch.Append(fillRectangle); 


     blipFill.Append(blip); 
     blipFill.Append(stretch); 


     // Creates an ShapeProperties instance and adds its children. 
     P.ShapeProperties shapeProperties = new P.ShapeProperties(); 


     A.Transform2D transform2D = new A.Transform2D(); 
     A.Offset offset = new A.Offset() { X = 457200L, Y = 1524000L }; 
     A.Extents extents = new A.Extents() { Cx = 8229600L, Cy = 5029200L }; 


     transform2D.Append(offset); 
     transform2D.Append(extents); 


     A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle }; 
     A.AdjustValueList adjustValueList = new A.AdjustValueList(); 


     presetGeometry.Append(adjustValueList); 


     shapeProperties.Append(transform2D); 
     shapeProperties.Append(presetGeometry); 


     picture.Append(nonVisualPictureProperties); 
     picture.Append(blipFill); 
     picture.Append(shapeProperties); 


     slide.CommonSlideData.ShapeTree.AppendChild(picture); 


     // Generates content of imagePart. 
     ImagePart imagePart = slide.SlidePart.AddNewPart<ImagePart>(imageExt, embedId); 
     FileStream fileStream = new FileStream(imagePath, FileMode.Open); 
     imagePart.FeedData(fileStream); 
     fileStream.Close(); 
 } 

源代码

于 2014-09-12T12:55:48.843 回答