【问题标题】:Adding a slide in a PowerPoint presentation in C#在 C# 的 PowerPoint 演示文稿中添加幻灯片
【发布时间】:2017-09-25 00:12:24
【问题描述】:

使用 C# 加载项,我正在尝试向 PowerPoint 添加一个按钮,当我单击该按钮时,一张新幻灯片会添加到当前演示文稿中。

我找到了this solution - 它真的很接近我想要做的......但它旨在打开另一个 PowerPoint 文件并添加一张幻灯片。但我想做的是在当前演示文稿中添加一张幻灯片。

我尝试修改给定的代码,但没有成功。

我在代码中插入了 2 个我认为有问题的问题,并在代码的开头和结尾标记了 //*****question here*****

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();
}

你知道我可以怎么说修改当前的演示文稿吗?

【问题讨论】:

    标签: c# powerpoint openxml


    【解决方案1】:

    使用这个:

     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

    【讨论】:

    • 好吧,我想问题是一样的:在这里你给一个文件夹,打开并修改另一个演示文稿。我想要做的是修改当前的演示文稿(我正在做一个ppt插件 - 当你打开一个演示文稿并按下插件给出的按钮时,会添加一张新幻灯片-->所以我需要修改当前演示文稿)。也许你的代码很简单,但我不知道该怎么做?
    猜你喜欢
    • 2023-03-29
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 2023-03-17
    • 1970-01-01
    • 2012-06-28
    • 2020-08-08
    相关资源
    最近更新 更多