如何从一组PPTS中复制特定的幻灯片?

How to copy specific slides from a group of PPTS?

我有一个应用程序可以根据某些条件识别一些幻灯片,并希望将这些幻灯片复制到一个 PPTX 文件中。我有用于复制幻灯片的 openxml 代码,它工作得很好,但是当输出文件大小增加时会花费太多时间。所以我决定转向互操作来应对。以下代码用于应对幻灯片。

using Microsoft.Office.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;


namespace CloneSlide
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                PowerPoint.Application app = new PowerPoint.Application();
                PowerPoint.Presentation currPresentation = null;
                PowerPoint.Presentation currPresentationop = null;

                string inputFileName = @"C:\Users\user\Desktop\Blannkdocs\ppt\Input.pptx";
                //PowerPoint.Presentations presentations = app.Presentations;
                //var readOnly = Microsoft.Office.Core.MsoTriState.msoTrue;
                //var untitled = Microsoft.Office.Core.MsoTriState.msoTrue;
                //var withwindow = Microsoft.Office.Core.MsoTriState.msoFalse;
                //string chkfileforpassword = inputFileName + "::" + "\"\"" + "::" + "\"\"";
                //currPresentation = presentations.Open(chkfileforpassword, readOnly, untitled, withwindow);
                //currPresentation.Slides[1].Copy();

                string outputFileName = @"C:\Users\user\Desktop\Blannkdocs\ppt\Presentation1.pptx";
                PowerPoint.Presentations presentationsop = app.Presentations;
                currPresentationop = presentationsop.Open(outputFileName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
                //currPresentationop.Slides.Paste(1);
                currPresentationop.Slides.InsertFromFile(inputFileName, 1, 1, 1);
                System.Threading.Thread.Sleep(4000);
                currPresentationop.Save();
                app.Quit();
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);
            }
            Console.WriteLine("Execution Complete");
            Console.ReadLine();
        }
    }
}

我已经尝试过

        //currPresentationop.Slides.Paste(1);
        currPresentationop.Slides.InsertFromFile(inputFileName, 1, 1, 1);

在这两种情况下都复制了内容,但幻灯片的背景和格式在输出中消失了。有什么我在应对过程中遗漏的吗

using PPT = Microsoft.Office.Interop.PowerPoint;

public void Main()
{
    PPT.Application app = new PPT.Application();
    app.Visible = MsoTriState.msoCTrue;
    PPT.Presentation ppt1 = app.Presentations.Open(@"C:\Presentation1.pptx");
    ppt1.Slides[1].Copy();

    PPT.Presentation ppt2 = app.Presentations.Open(@"C:\Presentation2.pptx");
    ppt2.Windows[1].View.GotoSlide(1);

    app.CommandBars.ExecuteMso("PasteSourceFormatting");

}

你可以试试这个。