C#OpenXML PPTX幻灯片副本

时间:2018-08-21 12:27:29

标签: c# asp.net powerpoint openxml

我有一个带一张幻灯片的pptx文件。 我想用另一个名称保存此文件,并且我想在第一张幻灯片上复制几张幻灯片。 我想更改里面的内容。

我的问题是创建了10张幻灯片,但是复制的幻灯片是空幻灯片。

预先感谢您提供帮助。

public class PowerPointHelper
{
    public static void Test()
    {
        var presentationFile = @"D:/certificate.pptx";
        string saveFilepath =  @"D:/" + DateTime.Now.ToString("yyyy년MM월dd일HH시mm분_") + "certificate.pptx";
        SaveAs(presentationFile, saveFilepath);
        AddForm(saveFilepath, 100);
    }

    public static void SaveAs(string filePath, string new_filePath)
    {
        using (var destDoc = PresentationDocument.Open(filePath, false))
        {
            destDoc.SaveAs(new_filePath).Close();
        }
    }

    public static void AddForm(string new_filePath, int count)
    {

        using (var destDoc = PresentationDocument.Open(new_filePath, true))
        {
            PresentationPart presentationPart = destDoc.PresentationPart;
            Presentation presentation = presentationPart.Presentation;

            // Verify that the presentation is not empty.
            if (presentationPart == null)
            {
                throw new InvalidOperationException("The presentation document is empty.");
            }

            var slideIds = presentationPart.Presentation.SlideIdList.ChildElements;
            string slidePartRelationshipId = (slideIds[0] as SlideId).RelationshipId;
            SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);

            presentationPart.AddPart<SlidePart>(slidePart);
            for (uint i = 1; i < 10; ++i)
            {
                SlideId slideId = new SlideId
                {
                    Id = i,
                    RelationshipId = destDoc.PresentationPart.GetIdOfPart(slidePart)
                };
                presentation.SlideIdList.Append(slideId);
            }
            destDoc.PresentationPart.Presentation.Save();
        }
    }
}

0 个答案:

没有答案