如何从一组PPT中复制特定幻灯片



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

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:UsersuserDesktopBlannkdocspptInput.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:UsersuserDesktopBlannkdocspptPresentation1.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");
}

你可以试试这个。

最新更新