OpenXML-为演示文稿中的幻灯片设置幻灯片布局 [英] OpenXML- Set a slide layout for a slide in presentation

查看:201
本文介绍了OpenXML-为演示文稿中的幻灯片设置幻灯片布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来创建演示文稿的代码.

Here is the code i used to create the presentation.

我在这里尝试的是创建幻灯片并将形状插入其中并将幻灯片附加到已创建的演示文稿中.很好

What i'm trying here is to create a slide and insert shapes into it and attach the slide into already created presentation. That works fine.

我的问题是我如何设置插入幻灯片的布局.我的意思是这里的幻灯片布局是

My question is how i set the layout the of the inserted slide. what i mean slide layout here is

slideLayoutpart.SlideLayout = new SlideLayout() {
    Type = SlideLayoutValues.VerticalTitleAndText
};

我想将此布局设置为我的幻灯片.

I want to set this layout to my Slide.

我看过使用slidelayout HERE

I had looked working with slidelayout HERE

Slide slide = new Slide(new CommonSlideData(new ShapeTree()));

uint drawingObjectId = 1;

// Construct the slide content.            
// Specify the non-visual properties of the new slide.
NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties());
nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" };
nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties();
nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();

// Specify the group shape properties of the new slide.
slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties());

// Declare and instantiate the title shape of the new slide. TITLE SHAPE
Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape());
drawingObjectId++;

// Specify the required shape properties for the title shape. 
NonVisualShapeProperties nonVisualShapeProperties2;
ShapeProperties shapeProperties2;

CreateVisualProperties(out nonVisualShapeProperties2, out shapeProperties2,
    PlaceholderValues.Title, drawingObjectId);

// Specify the text of the title shape.
TextBody titletextBody = CreateContent(slideTitle, PlaceholderValues.Title);

titleShape.Append(nonVisualShapeProperties2);
titleShape.Append(shapeProperties2);
titleShape.Append(titletextBody);


// Save the new slide part.
slide.Save(slidePart);

#region Slide Poistioning

// The slide ID list should not be null.
SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;

// Find the highest slide ID in the current list.
uint maxSlideId = 1;
SlideId prevSlideId = null;


foreach (SlideId slideId in slideIdList.ChildElements)
{

    if (slideId.Id > maxSlideId)
    {
        maxSlideId = slideId.Id;
    }

    position--;
    if (position == 0)
    {
        prevSlideId = slideId;
    }

}

maxSlideId++;

// Get the ID of the previous slide.
SlidePart lastSlidePart;

if (prevSlideId != null)
{
    //Changed to set first thing as layout
    // lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId);
    lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId);
}
else
{
    lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId);
}

// Use the same slide LAYOUT HERE as that of the previous slide.
if (null != lastSlidePart.SlideLayoutPart)
{
    SlideLayoutPart slideLayoutpartNew = lastSlidePart.SlideLayoutPart;
    slideLayoutpartNew.AddNewPart<SlideMasterPart>();
    slideLayoutpartNew.SlideLayout = new SlideLayout() { Type = SlideLayoutValues.VerticalTitleAndText };
    slidePart.AddPart(slideLayoutpartNew);

    slidePart.AddPart(slideLayoutPart);

    //When i try to set lastslidelayout it works fine.
    //slidePart.AddPart(lastSlidePart.SlideLayoutPart);
}

// Insert the new slide into the slide list after the previous slide.
SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
newSlideId.Id = maxSlideId;
newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart);
#endregion

// Save the modified presentation.
presentationPart.Presentation.Save();

推荐答案

我想通了,如何设置布局

I figured out ,How to set layout

 string layoutName = "Title and Content";

        // Get SlideMasterPart and SlideLayoutPart from the existing Presentation Part
        SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First();
        SlideLayoutPart slideLayoutPart = slideMasterPart.SlideLayoutParts.SingleOrDefault
            (sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName, StringComparison.OrdinalIgnoreCase));
        if (slideLayoutPart == null)
        {
            throw new Exception("The slide layout " + layoutName + " is not found");
        }

        slidePart.AddPart<SlideLayoutPart>(slideLayoutPart);

我在这里将布局添加到slidepart并将保存演示文稿

这篇关于OpenXML-为演示文稿中的幻灯片设置幻灯片布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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