Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建模板演示文稿的副本;。potx“;到一个新的“世界”;。pptx“;使用openXML_C#_Powerpoint_Openxml Sdk - Fatal编程技术网

C# 创建模板演示文稿的副本;。potx“;到一个新的“世界”;。pptx“;使用openXML

C# 创建模板演示文稿的副本;。potx“;到一个新的“世界”;。pptx“;使用openXML,c#,powerpoint,openxml-sdk,C#,Powerpoint,Openxml Sdk,我有一个扩展名为.potx的powerpoint模板,我需要创建模板文件的副本,但创建的文件中没有任何内容,它是一个空白演示文稿,下面是我使用模板文件创建目标文件的功能 /// <summary> /// Creates a copy of the source template file /// </summary> /// <param name="sourceFile">sourcce file which has to

我有一个扩展名为.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;
        }
    }
但最终制作的ppt中不会有任何幻灯片


任何帮助都将不胜感激

在将excel模板保存到实际excel文档之前,我发布了一个类似的答案,您应该能够对power point模板应用相同的逻辑。您不需要使用
电子表格文档
,而需要在其上使用和调用ChangeDocumentType。大致如下:

byte[] byteArray = File.ReadAllBytes("C:\\temp\\sa123.potx");
using (MemoryStream stream = new MemoryStream())
{
    stream.Write(byteArray, 0, (int)byteArray.Length);
    using (PresentationDocument presentationDoc = PresentationDocument.Open(stream, true))
    {
       // Change from template type to presentation type
       presentationDoc.ChangeDocumentType(PresentationDocumentType.Presentation);
    }
    File.WriteAllBytes("C:\\temp\\sa123.pptx", stream.ToArray()); 
}
然后,您可以重新打开保存的文件,并在不接触模板文件的情况下对其执行任何其他操作

    /// <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;
        }
    }
byte[] byteArray = File.ReadAllBytes("C:\\temp\\sa123.potx");
using (MemoryStream stream = new MemoryStream())
{
    stream.Write(byteArray, 0, (int)byteArray.Length);
    using (PresentationDocument presentationDoc = PresentationDocument.Open(stream, true))
    {
       // Change from template type to presentation type
       presentationDoc.ChangeDocumentType(PresentationDocumentType.Presentation);
    }
    File.WriteAllBytes("C:\\temp\\sa123.pptx", stream.ToArray()); 
}