2017-09-24 6 views
0

의 슬라이드 추가 C# 추가 기능을 사용하면 PowerPoint에 단추를 추가하려고하는데 단추를 클릭하면 현재 프레젠테이션에 새 슬라이드가 추가됩니다.PowerPoint 프레젠테이션에 C#

나는 실제로 내가하고 싶은 일과 비슷하지만 ... 다른 PowerPoint 파일을 열고 슬라이드를 추가하기위한 것입니다. this solution입니다. 하지만 현재 프레젠테이션에 슬라이드를 추가하고 싶습니다.

주어진 코드를 수정하려고했지만 성공하지 못했습니다.

나는 문제가 있다고 생각 코드로이 질문에 삽입 시작과 코드

private void buttonForSlide_Click(object sender, EventArgs e) 
{ 
    InsertNewSlide(2, "New Slide added"); 
} 

// Insert the specified slide into the presentation at the specified position. 
public static void InsertNewSlide(int position, string slideTitle) 
{ 

    //*******question here********* Initially the code was like the following line where presentationDocument was presentationDocument = PresentationDocument.Open(pathOfTheFile, true) - I don't know how to specify to modify the current presentation ? 

    PresentationPart presentationPart = presentationDocument.PresentationPart; 

    // Declare and instantiate a new slide. 
    Slide slide = new Slide(new CommonSlideData(new ShapeTree())); 
    uint drawingObjectId = 1; 

    // Construct the slide content.    
    // Specify the non-visual properties of the new slide. 
    NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties()); 
    nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" }; 
    nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties(); 
    nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); 

    // Specify the group shape properties of the new slide. 
    slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties()); 

    // Declare and instantiate the title shape of the new slide. 
    Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape()); 

    drawingObjectId++; 

    // Specify the required shape properties for the title shape. 
    titleShape.NonVisualShapeProperties = new NonVisualShapeProperties 
     (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Title" }, 
     new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), 
     new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title })); 
    titleShape.ShapeProperties = new ShapeProperties(); 

    // Specify the text of the title shape. 
    titleShape.TextBody = new TextBody(new Drawing.BodyProperties(), 
      new Drawing.ListStyle(), 
      new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = slideTitle }))); 

    // Declare and instantiate the body shape of the new slide. 
    Shape bodyShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape()); 
    drawingObjectId++; 

    // Specify the required shape properties for the body shape. 
    bodyShape.NonVisualShapeProperties = new NonVisualShapeProperties(new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Content Placeholder" }, 
      new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), 
      new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Index = 1 })); 
    bodyShape.ShapeProperties = new ShapeProperties(); 

    // Specify the text of the body shape. 
    bodyShape.TextBody = new TextBody(new Drawing.BodyProperties(), 
      new Drawing.ListStyle(), 
      new Drawing.Paragraph()); 

    // Create the slide part for the new slide. 
    SlidePart slidePart = presentationPart.AddNewPart<SlidePart>(); 

    // Save the new slide part. 
    slide.Save(slidePart); 

    // Modify the slide ID list in the presentation part. 
    // The slide ID list should not be null. 
    SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; 

    // Find the highest slide ID in the current list. 
    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++; 

    // Get the ID of the previous slide. 
    SlidePart lastSlidePart; 

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

    // Use the same slide layout as that of the previous slide. 
    if (null != lastSlidePart.SlideLayoutPart) 
    { 
     slidePart.AddPart(lastSlidePart.SlideLayoutPart); 
    } 

    // Insert the new slide into the slide list after the previous slide. 
    SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); 
    newSlideId.Id = maxSlideId; 
    newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); 

    //*******question here********** Do you believe I have to save if I want to modify the current presentation ? 
    //presentationPart.Presentation.Save(); 
} 
의 끝에서 여기에 ***** 그들 // ***** 질문을 태그 한

현재 프레젠테이션을 수정하는 방법을 어떻게 알 수 있습니까?

답변

0

사용이 :

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     string folderName = @"E:\PPTFolder\"; 
     AddSlides(folderName);    
    } 

    private void AddSlides(string folderName) 
    {    
     string[] filePaths = Directory.GetFiles(folderName, "*.pptx", SearchOption.TopDirectoryOnly); 

     Microsoft.Office.Core.MsoTriState oFalse = Microsoft.Office.Core.MsoTriState.msoFalse; 
     Microsoft.Office.Core.MsoTriState oTrue = Microsoft.Office.Core.MsoTriState.msoTrue; 

     PowerPoint.Application oApp = new PowerPoint.Application(); 
     oApp.Visible = oTrue; 
     oApp.WindowState = PowerPoint.PpWindowState.ppWindowNormal; 

     PowerPoint.Presentation oPres; 
     PowerPoint.Slide oSlide=new PowerPoint.Slide(); 

     for (int i = 0; i < filePaths.Length; i++) 
     { 
      oPres = oApp.Presentations.Open(filePaths[i], ReadOnly: oFalse);     
      oSlide = oPres.Slides.Add(oPres.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly); 
      oSlide.Shapes[1].TextFrame.TextRange.Text = "Final Test"; 
      oSlide.Shapes[1].TextFrame.TextRange.Font.Name = "Comic Sans MS"; 
      oSlide.Shapes[1].TextFrame.TextRange.Font.Size = 48; 

      oPres.Save(); 
      oPres.Close(); 
     } 
     oSlide = null; 
     oPres = null; 
     oApp.Quit(); 
     oApp = null; 
     GC.WaitForPendingFinalizers(); 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
     GC.Collect(); 
    } 

이 너무 How to use Automation to create and to show a PowerPoint 2002 presentation by using Visual C# .NET 2002

+0

음을 참조하시기 바랍니다, 나는 문제가 같은 생각 : 여기 당신이 열려있는 폴더를주고 다른 프레젠테이션을 수정합니다. 내가하고 싶은 일은 현재 프리젠 테이션을 수정하는 것입니다. (ppt에서 애드 인을하고 있습니다. 그리고 프리젠 테이션을 열고 추가 기능에서 제공하는 버튼을 누르면 새로운 슬라이드가 추가됩니다.) 현재 프리젠 테이션 수정). 어쩌면 그것은 당신의 코드에서 매우 간단하지만 어떻게 해야할지 모르겠다. – droledenom