1

OpenXML を使用して画像を含む PowerPoint ファイルを生成しようとしています。残念ながら、うまくいきません。画像が表示されていません。OpenXML 生産性ツールで生成されたファイルを確認し、それぞれファイルの内容を解凍しました。ファイル自体には /ppt/media/image.png の画像が含まれており、2 番目のスライドに表示されます。

これが私のコードです:

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

画像をスライドに追加する必要があると思います.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 に答える