且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用OOXML和c#将Power Point幻灯片复制到演示文稿中

更新时间:2023-02-13 22:59:04

我将其发布到此类别进行讨论。


感谢您的理解和支持。


Within a loop  I create Power Point slides using OOXML and C#. After I save them (one at a time) , I copy that slide into a Final_presentation.pptx file using the following code:

public void CopySlideIntoPresentation(string slide_to_copy, string presentation_to_save)
    {
        //generate a random number
        Random random = new Random();
        int i = random.Next(10, 100000);

        //Copy slide into final_presentation
        using (PresentationDocument newDocument = PresentationDocument.Open(presentation_to_save, true))
        {
            PresentationDocument templateDocument = PresentationDocument.Open(slide_to_copy, false);

            uint uniqueId = GetMaxIdFromChild(newDocument.PresentationPart.Presentation.SlideMasterIdList);
            uint maxId = GetMaxIdFromChild(newDocument.PresentationPart.Presentation.SlideIdList);

            //SlidePart oldPart = GetSlidePartByTagName(templateDocument, "rId1");
            SlidePart oldPart = templateDocument.PresentationPart.SlideParts.First();

            SlidePart newPart = newDocument.PresentationPart.AddPart<SlidePart>(oldPart, "sourceId" + i);

            SlideMasterPart newMasterPart = newDocument.PresentationPart.AddPart(newPart.SlideLayoutPart.SlideMasterPart);

            SlideIdList idList = newDocument.PresentationPart.Presentation.SlideIdList;



            // create new slide ID

            maxId++;

            SlideId newId = new SlideId();

            newId.Id = maxId;

            newId.RelationshipId = "sourceId" + i;

            idList.Append(newId);


            // Create new master slide ID

            uniqueId++;

            SlideMasterId newMasterId = new SlideMasterId();

            newMasterId.Id = uniqueId;

            newMasterId.RelationshipId = newDocument.PresentationPart.GetIdOfPart(newMasterPart);

            newDocument.PresentationPart.Presentation.SlideMasterIdList.Append(newMasterId);

            // change slide layout ID

            FixSlideLayoutIds(newDocument.PresentationPart);

            //newPart.Slide.Save();
            templateDocument.Close();

            newDocument.PresentationPart.Presentation.Save();
            newDocument.Close();
            //return true;
        }

    }

 

Even though all the slides get copied into final_presentation.pptx file, I get the following error when open it:

"Powerpoint found unreadable content in final_presntation.pptx. Do you want to recover the contents of this presentation? If you trust the source of this presentation click Yes"

I click "yes" and then I get:

"The file cannot be open by using Microsoft Office Powerpoint. Do you want to search the Microsoft Office Online website for a converter that can open the file? "

I click "No" and I can see the new slide inserted into this final_presentation file.

Can someone help me to solve this error?

THANKS

I post it to this category for good discussion.

Thanks for your understanding and support.