我有一个应用程序,它根据某些标准识别一些幻灯片,并希望将这些幻灯片复制到单个 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");

}
你可以试试这个。

关于c# - 如何从一组PPT中复制特定幻灯片?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36997193/

10-14 15:35