以编程方式生成的PowerPoint演示文稿打破PowerPoint 2013

时间:2014-10-15 14:48:56

标签: c# powerpoint openxml-sdk

我正在尝试从不同来源生成演示文稿。基本上我从大量的PowerPoint演示文稿中有大约200张幻灯片。 为了实现这一点,我使用的是OpenXml Sdk。 该计划的流程如下:

  • 打开模板演示文稿
  • 打开新演示文稿
  • 将第二个演示文稿中的幻灯片合并到模板演示文稿中

最后我将新的演示文稿保存到磁盘。 尝试使用PowerPoint 2013打开它会破坏它,并提供以下信息: 问题签名:

Problem Event Name: APPCRASH
Application Name:   POWERPNT.EXE
Application Version:    15.0.4454.1000
Application Timestamp:  509a3abf
Fault Module Name:  oart.dll
Fault Module Version:   15.0.4605.1000
Fault Module Timestamp: 531f9b2a
Exception Code: c00000fd
Exception Offset:   00003051
OS Version: 6.2.9200.2.0.0.272.7
Locale ID:  1033

有关此问题的其他信息:

LCID:   1033
skulcid:    1033

PowerPoint 2010打开它然而效果很好。它没有问题。

这是代码:

private static void MergePresentation(string generatedPresentation, string presentationToBeMerged)
{
    try
    {
        int id = 0;

        // Open the destination presentation.
        using (PresentationDocument generatedPresentationDeck = PresentationDocument.Open(generatedPresentation, true))
        {
            PresentationPart generatedPresentationPart = generatedPresentationDeck.PresentationPart;

            // If the merged presentation does not have a SlideIdList 
            // element yet, add it.
            if (generatedPresentationPart.Presentation.SlideIdList == null)
            {
                generatedPresentationPart.Presentation.SlideIdList = new SlideIdList();
            }

            // Open the source presentation. This will throw an exception if
            // the source presentation does not exist.
            using (PresentationDocument mySourceDeck = PresentationDocument.Open(presentationToBeMerged, false))
            {
                PresentationPart sourcePresPart = mySourceDeck.PresentationPart;

                // Get unique ids for the slide master and slide lists
                // for use later.
                _uniqueId = GetMaxSlideMasterId(generatedPresentationPart.Presentation.SlideMasterIdList);

                uint maxSlideId = GetMaxSlideId(generatedPresentationPart.Presentation.SlideIdList);

                // Copy each slide in the source presentation, in order, to 
                // the destination presentation.
                foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList)
                {
                    SlidePart sp;
                    SlidePart destSp;
                    SlideMasterPart destMasterPart;
                    string relId;
                    SlideMasterId newSlideMasterId;
                    SlideId newSlideId;

                    // Create a unique relationship id.
                    id++;
                    sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId);

                    //sp.Slide.Transition.Remove();

                    relId = Path.GetFileNameWithoutExtension(presentationToBeMerged).Replace(" ", "_") + id;

                    // Add the slide part to the destination presentation.
                    destSp = generatedPresentationPart.AddPart<SlidePart>(sp, relId);

                    // The slide master part was added. Make sure the
                    // relationship between the main presentation part and
                    // the slide master part is in place.
                    destMasterPart = destSp.SlideLayoutPart.SlideMasterPart;
                    generatedPresentationPart.AddPart(destMasterPart);

                    // Add the slide master id to the slide master id list.
                    _uniqueId++;
                    newSlideMasterId = new SlideMasterId();

                    newSlideMasterId.RelationshipId = generatedPresentationPart.GetIdOfPart(destMasterPart);

                    newSlideMasterId.Id = _uniqueId;

                    generatedPresentationPart.Presentation.SlideMasterIdList.Append(newSlideMasterId);

                    // Add the slide id to the slide id list.
                    maxSlideId++;
                    newSlideId = new SlideId();
                    newSlideId.RelationshipId = relId;
                    newSlideId.Id = maxSlideId;

                    generatedPresentationPart.Presentation.SlideIdList.Append(newSlideId);
                }

                // Make sure that all slide layout ids are unique.
                FixSlideLayoutIds(generatedPresentationPart);
            }

            // Save the changes to the destination deck.
            generatedPresentationPart.Presentation.Save();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

/// <summary>
/// Find the maximum value of ID of all slide masters
/// </summary>
/// <param name="slideMasterIdList" />
/// <returns>
private static uint GetMaxSlideMasterId(SlideMasterIdList slideMasterIdList)
{
    // Slide master identifiers have a minimum value of greater than
    // or equal to 2147483648. 
    uint max = 2147483648;

    if (slideMasterIdList != null)
        // Get the maximum id value from the current set of children.
        foreach (SlideMasterId child in
            slideMasterIdList.Elements<SlideMasterId>())
        {
            uint id = child.Id;

            if (id > max)
                max = id;
        }

    return max;
}

/// <summary>
/// Find the maximum ID of all slides
/// </summary>
/// <param name="slideIdList" />
/// <returns>
private static uint GetMaxSlideId(SlideIdList slideIdList)
{
    // Slide identifiers have a minimum value of greater than or
    // equal to 256 and a maximum value of less than 2147483648. 
    uint max = 256;

    if (slideIdList != null)
        // Get the maximum id value from the current set of children.
        foreach (SlideId child in slideIdList.Elements<SlideId>())
        {
            uint id = child.Id;

            if (id > max)
                max = id;
        }

    return max;
}

/// <summary>
/// Fix the IDs of all slide layouts by making sure that all the slide layout IDs in the
/// destination slide are unique.
/// </summary>
/// <param name="presPart" />
private static void FixSlideLayoutIds(PresentationPart presPart)
{
    // Make sure that all slide layouts have unique ids.
    foreach (SlideMasterPart slideMasterPart in presPart.SlideMasterParts)
    {
        foreach (SlideLayoutId slideLayoutId in slideMasterPart.SlideMaster.SlideLayoutIdList)
        {
            _uniqueId++;
            slideLayoutId.Id = (uint)_uniqueId;
        }

        slideMasterPart.SlideMaster.Save();
    }
}

我调用Merge函数的方式是:

string templateFilePath = @"C:\Users\mm\Desktop\testing pp\Presentation1.pptx";
string newFilePath = @"C:\Users\mm\Desktop\testing pp\Generated Presentation-105.pptx";
MergePresentation(templateFilePath, newFilePath);

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:1)

我设法找到解决方案。 将其发布在此处以防其他人遇到此问题。

我已经习惯了生成的演示文稿上的OpenXml Sdk Validator,并发现了以下错误: 该元素具有意外的子元素&#39; http://schemas.openxmlformats.org/presentationml/2006/main:notesMasterIdLst&#39;

这引导我注意幻灯片中的注释。删除它们解决了问题,一切正常。

删除笔记的代码(即使您在PP中打开演示文稿时没有看到任何笔记):

 public static void RemoveNotesFromDoc(string docPath)
    {
        try
        {
            using (PresentationDocument pDoc =
                PresentationDocument.Open(docPath, true))
            {
                foreach (var slide in pDoc.PresentationPart.SlideParts)
                {
                    NotesSlidePart notes = slide.NotesSlidePart;


                    if (notes != null)
                    {
                        slide.DeletePart(slide.GetIdOfPart(slide.NotesSlidePart));
                    }
                }
            }
        }
    }