使用openXML创建模板演示文稿“.potx”的副本到新的“.pptx”

时间:2015-05-12 12:19:32

标签: c# powerpoint openxml-sdk

我有一个扩展名为.potx的powerpoint模板,我需要创建模板文件的副本,但是创建的文件中没有任何内容,它是一个空白的演示文稿,下面是我正在使用的函数使用模板文件创建目标文件。

    /// <summary>
    /// Creates a copy of the source template file
    /// </summary>
    /// <param name="sourceFile">sourcce file which has to be copied</param>
    /// <param name="destinationFile">destination file where the new file has to be placed.</param>
       public static void CreateFileFromTemplate(this string sourceFile, string destinationFile)
    {
        try
        {
            //File.Copy(sourceFile, destinationFile, true);
            if (System.IO.Path.GetExtension(sourceFile) == ".potx")
            {
                if (System.IO.Path.GetExtension(destinationFile) == ".potx")
                {
                    PresentationDocument destinationDoc = PresentationDocument.Create(destinationFile, PresentationDocumentType.Presentation);
                    PresentationPart presentationPart = destinationDoc.AddPresentationPart();
                    presentationPart.Presentation = new Presentation();
                    presentationPart.Presentation.Save();
                    destinationDoc.Close();
                    //Package destinationPackage = Package.Open(destinationFile, FileMode.Open, FileAccess.ReadWrite);
                    using (destinationDoc = PresentationDocument.Open(destinationFile, true))
                    {
                        Package templatePackage = Package.Open(sourceFile, FileMode.Open);
                        using (PresentationDocument templateDocument = PresentationDocument.Open(templatePackage))
                        {
                            PresentationPart sourcePresPart = templateDocument.PresentationPart;
                            PresentationPart destinationPresrPart = destinationDoc.PresentationPart;
                            //var templateSlideCount = templateDocument.CountSlides();
                            //for (int slides = 1; slides <= templateSlideCount; slides++)
                            //{

                            //}
                            SlidePart sourceSlidePart;
                            SlidePart destinationSlidePart;
                            foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList)
                            {
                                //int i=0;

                                sourceSlidePart = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId);
                                destinationSlidePart = sourceSlidePart.Clone();
                                SlideIdList slideIdList = destinationPresrPart.Presentation.SlideIdList;

                                destinationPresrPart.AddPart(destinationSlidePart);
                                // Save the modified presentation.
                                destinationPresrPart.Presentation.Save();
                            }
                        }
                    }
                }
                else
                {
                    throw new FileFormatException("Invalid destination file format, Valid file should have .pptx as extension");
                }
            }
            else
            {
                throw new FileFormatException("Invalid file format, Valid file should have .potx as extension");
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

用于克隆幻灯片的扩展方法

    /// <summary>
    /// Clones the specified slide.
    /// </summary>
    /// <param name="sourceSlide">The slide to clone.</param>
    /// <returns>Cloned slide.</returns>
    public static SlidePart Clone(this SlidePart sourceSlide)
    {
        // find the presentationPart: makes the API more fluent
        var presentationPart = sourceSlide.GetParentParts()
            .OfType<PresentationPart>()
            .Single();

        // clone slide contents
        Slide currentSlide = (Slide)sourceSlide.Slide.CloneNode(true);
        var slidePartClone = presentationPart.AddNewPart<SlidePart>();

        currentSlide.Save(slidePartClone);

        // copy layout part
        slidePartClone.AddPart(sourceSlide.SlideLayoutPart);

        //copy all embedded elements
        foreach (ChartPart part in sourceSlide.ChartParts)
        {
            ChartPart newpart = slidePartClone.AddNewPart<ChartPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
            newpart.FeedData(part.GetStream());
            newpart.AddNewPart<EmbeddedPackagePart>(part.EmbeddedPackagePart.ContentType, part.GetIdOfPart(part.EmbeddedPackagePart));
            newpart.EmbeddedPackagePart.FeedData(part.EmbeddedPackagePart.GetStream());
        }
        foreach (EmbeddedObjectPart part in sourceSlide.EmbeddedObjectParts)
        {
            EmbeddedObjectPart newpart = slidePartClone.AddNewPart<EmbeddedObjectPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
            newpart.FeedData(part.GetStream());
        }
        foreach (EmbeddedPackagePart part in sourceSlide.EmbeddedPackageParts)
        {
            EmbeddedPackagePart newpart = slidePartClone.AddNewPart<EmbeddedPackagePart>(part.ContentType, sourceSlide.GetIdOfPart(part));
            newpart.FeedData(part.GetStream());
        }
        foreach (ImagePart part in sourceSlide.ImageParts)
        {
            ImagePart newpart = slidePartClone.AddNewPart<ImagePart>(part.ContentType, sourceSlide.GetIdOfPart(part));
            newpart.FeedData(part.GetStream());
        }
        foreach (VmlDrawingPart part in sourceSlide.VmlDrawingParts)
        {
            VmlDrawingPart newpart = slidePartClone.AddNewPart<VmlDrawingPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
            newpart.FeedData(part.GetStream());
        }
        foreach (UserDefinedTagsPart part in sourceSlide.UserDefinedTagsParts)
        {
            UserDefinedTagsPart newpart = slidePartClone.AddNewPart<UserDefinedTagsPart>(part.ContentType, sourceSlide.GetIdOfPart(part));
            newpart.FeedData(part.GetStream());
        }
        return slidePartClone;
    }

在我的main方法中,我有下面的代码调用这些函数来创建ppt

 string sourceFilePath =@"C:\TempFolder\template.potx";
 string destinationFilePath = @"C:\TempFolder\ClonedFile.potx";
 sourceFilePath.CreateFileFromTemplate(destinationFilePath);

但最终创建的ppt中不会有任何幻灯片,

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

我在将excel模板保存到实际的Excel文档here之前发布了类似的答案,您应该可以使用功率点模板应用相同的逻辑。您需要使用PresentationDocument而不是#include <stdlib.h> #include <stdio.h> int main() { int m ,n, i; printf("Number of nodes is: "); scanf("%d", &n); int ** array = malloc(n * sizeof(int*)); for(i = 0; i < n; i++) array[i] = malloc(n * sizeof(int)); printf("Number of edges is: "); scanf("%d", &m); int x, y; for(i=0;i<m;i++) { //int num_read = scanf("%d %d", &x, &y); if(scanf("%d %d", &x, &y) < 2) { printf("please enter valid two integers..\n"); while (getchar() != '\n'); // read all characters in the input stream. } else if (x >=0 && y >= 0 && x < n && y < n) // here { array[x][y]=1; printf("array[%d][%d]=1;\n" , x, y); } else printf("error in your values x=%d y=%d\n" , x , y); } return 0; } ,而是调用ChangeDocumentType。这些方面的东西:

SpreadsheetDocument

然后,您可以重新打开该已保存的文件,并在不触及模板文件的情况下执行您想要的任何额外操作。