如何使用OOXML和c#将Power Point幻灯片复制到演示文稿中 [英] how to copy Power Point slides into a presentation using OOXML and c#

查看:71
本文介绍了如何使用OOXML和c#将Power Point幻灯片复制到演示文稿中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个循环中 我使用OOXML和C#创建Power Point幻灯片。后我保存它们(一次一个),我复制滑入使用以下代码Final_presentation.pptx文件:


公共无效CopySlideIntoPresentation(串slide_to_copy,串presentation_to_save)

    {

        //生成随机数

        Random random = new Random();

        int i = random.Next(10,100000);
$


        //将幻灯片复制到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();



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; SlidePart NEWPART = newDocument.PresentationPart.AddPart< SlidePart>(oldPart,"的SourceID" + I);



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



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; SlideIdList idList = newDocument.PresentationPart.Presentation.SlideIdList;
$





      ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //创建新的幻灯片ID



            maxId ++;
$


            SlideId newId = new SlideId();
$


            newId.Id = maxId;



            newId.RelationshipId =" sourceId" + i;



            idList.Append(newId);
$




           ;&NBSP; //创建新的主幻灯片ID



            uniqueId ++;
$


            SlideMasterId newMasterId = new SlideMasterId();
$


            newMasterId.Id = uniqueId;



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



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



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; //更改幻灯片布局ID



            FixSlideLayoutIds(newDocument.PresentationPart);
$


            //newPart.Slide.Save();

            templateDocument.Close();
$


            newDocument.PresentationPart.Presentation.Save();

            newDocument.Close();

            //返回true;

        }


    }


 


即使将所有幻灯片复制到final_presentation.pptx文件中,打开时也会出现以下错误:


" Powerpoint在final_presntation.pptx中找到了不可读的内容。您想恢复此演示文稿的内容吗?如果您信任此演示文稿的来源,请单击是"


我单击"是"和"是"。然后我得到:


"使用Microsoft Office Powerpoint无法打开该文件。是否要在Microsoft Office Online网站上搜索可以打开该文件的转换器? "


我点击"否"我可以看到新的幻灯片插入到这个final_presentation文件中。


有人可以帮我解决这个错误吗?


谢谢

解决方案

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


感谢您的理解和支持。


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.


这篇关于如何使用OOXML和c#将Power Point幻灯片复制到演示文稿中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆