使用OpenXML在PowerPoint中的幻灯片之间创建链接

时间:2019-05-24 09:28:34

标签: c# powerpoint openxml

我正在尝试使用OpenXML构建PowerPoint演示文稿时构建目录。这将需要具有到幻灯片的超链接。 我使用了Open XML生产力工具来尝试找出要使用的代码,并查看此question以及其中引用的链接。

我用于测试的powerpoint文件包含一些幻灯片,但是为了测试代码,应该在文本框的开头添加一个幻灯片,并在文本框中添加两行文本,分别引用幻灯片2和3。

最后打开文件时,出现以下错误 PowerPoint发现中的内容有问题。 PowerPoint可以尝试修复演示文稿。

如果我尝试修复,则有两行的文本框在那里,但整个演示文稿中的所有格式均已更改,并且文本未链接到其他幻灯片。

这是我正在使用的代码

    private void InsertLinkOnNewSlide()
    {
        var position = 0;
        string slideTitle = "Slide with a hyperlink";
        using (PresentationDocument presentationDocument = PresentationDocument.Open(openFileDialog1.FileName, true))
        {
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            if (slideTitle == null)
            {
                throw new ArgumentNullException("slideTitle");
            }

            PresentationPart presentationPart = presentationDocument.PresentationPart;

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

            // Declare and instantiate a new slide.
            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.
            Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape());

            drawingObjectId++;

            // Specify the required shape properties for the title shape. 
            titleShape.NonVisualShapeProperties = new NonVisualShapeProperties
                (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Title" },
                new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }),
                new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title }));
            titleShape.ShapeProperties = new ShapeProperties();

            // Specify the text of the title shape.
            titleShape.TextBody = new TextBody(new Drawing.BodyProperties(),
                    new Drawing.ListStyle(),
                    new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = slideTitle })));

            // Declare and instantiate the body shape of the new slide.
            slide.CommonSlideData.ShapeTree.AppendChild(ToC());

            // Create the slide part for the new slide.
            SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();
            // Save the new slide part.
            slide.Save(slidePart);

            // Modify the slide ID list in the presentation part.
            // 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)
            {
                lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId);
            }
            else
            {
                lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId);
            }


            SlideLayoutIdList slideLayoutIdList1 = new SlideLayoutIdList();
            SlideLayoutId slideLayoutId1 = new SlideLayoutId() { Id = (UInt32Value)2147483649U, RelationshipId = "rId2" };
            SlideLayoutId slideLayoutId2 = new SlideLayoutId() { Id = (UInt32Value)2147483650U, RelationshipId = "rId3" };
            slideLayoutIdList1.Append(slideLayoutId1);
            slideLayoutIdList1.Append(slideLayoutId2);
            lastSlidePart.SlideLayoutPart.SlideMasterPart.SlideMaster.Append(slideLayoutIdList1);

            // Use the same slide layout as that of the previous slide.
            if (null != lastSlidePart.SlideLayoutPart)
            {
                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);

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

    private Shape ToC()
    {
        Shape shape65 = new Shape();

        NonVisualShapeProperties nonVisualShapeProperties65 = new NonVisualShapeProperties();

        NonVisualDrawingProperties nonVisualDrawingProperties79 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "TextBox 3" };

        Drawing.NonVisualDrawingPropertiesExtensionList nonVisualDrawingPropertiesExtensionList65 = new Drawing.NonVisualDrawingPropertiesExtensionList();

        Drawing.NonVisualDrawingPropertiesExtension nonVisualDrawingPropertiesExtension65 = new Drawing.NonVisualDrawingPropertiesExtension() { Uri = "{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}" };

        OpenXmlUnknownElement openXmlUnknownElement65 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<a16:creationId xmlns:a16=\"http://schemas.microsoft.com/office/drawing/2014/main\" id=\"{82A8F1A8-1020-4A5E-9C65-D3C2432A2F35}\" />");

        nonVisualDrawingPropertiesExtension65.Append(openXmlUnknownElement65);

        nonVisualDrawingPropertiesExtensionList65.Append(nonVisualDrawingPropertiesExtension65);

        nonVisualDrawingProperties79.Append(nonVisualDrawingPropertiesExtensionList65);
        NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties65 = new NonVisualShapeDrawingProperties() { TextBox = true };
        ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties79 = new ApplicationNonVisualDrawingProperties();

        nonVisualShapeProperties65.Append(nonVisualDrawingProperties79);
        nonVisualShapeProperties65.Append(nonVisualShapeDrawingProperties65);
        nonVisualShapeProperties65.Append(applicationNonVisualDrawingProperties79);

        ShapeProperties shapeProperties65 = new ShapeProperties();

        Drawing.Transform2D transform2D25 = new Drawing.Transform2D();
        Drawing.Offset offset39 = new Drawing.Offset() { X = 278296L, Y = 318052L };
        Drawing.Extents extents39 = new Drawing.Extents() { Cx = 3331596L, Cy = 646331L };

        transform2D25.Append(offset39);
        transform2D25.Append(extents39);

        Drawing.PresetGeometry presetGeometry6 = new Drawing.PresetGeometry() { Preset = Drawing.ShapeTypeValues.Rectangle };
        Drawing.AdjustValueList adjustValueList6 = new Drawing.AdjustValueList();

        presetGeometry6.Append(adjustValueList6);
        Drawing.NoFill noFill1 = new Drawing.NoFill();

        shapeProperties65.Append(transform2D25);
        shapeProperties65.Append(presetGeometry6);
        shapeProperties65.Append(noFill1);

        TextBody textBody65 = new TextBody();

        Drawing.BodyProperties bodyProperties65 = new Drawing.BodyProperties() { Wrap = Drawing.TextWrappingValues.Square, RightToLeftColumns = false };
        Drawing.ShapeAutoFit shapeAutoFit1 = new Drawing.ShapeAutoFit();

        bodyProperties65.Append(shapeAutoFit1);
        Drawing.ListStyle listStyle65 = new Drawing.ListStyle();

        Drawing.Paragraph paragraph101 = new Drawing.Paragraph();

        Drawing.Run run64 = new Drawing.Run();

        Drawing.RunProperties runProperties88 = new Drawing.RunProperties() { Language = "en-ZA", Dirty = false };
        Drawing.HyperlinkOnClick hyperlinkOnClick1 = new Drawing.HyperlinkOnClick() { Id = "rId2", Action = "ppaction://hlinksldjump" };

        runProperties88.Append(hyperlinkOnClick1);
        Drawing.Text text88 = new Drawing.Text();
        text88.Text = "Link to slide 2";

        run64.Append(runProperties88);
        run64.Append(text88);

        Drawing.Break break1 = new Drawing.Break();
        Drawing.RunProperties runProperties89 = new Drawing.RunProperties() { Language = "en-ZA", Dirty = false };

        break1.Append(runProperties89);

        Drawing.Run run65 = new Drawing.Run();

        Drawing.RunProperties runProperties90 = new Drawing.RunProperties() { Language = "en-ZA", Dirty = false };
        Drawing.HyperlinkOnClick hyperlinkOnClick2 = new Drawing.HyperlinkOnClick() { Id = "rId3", Action = "ppaction://hlinksldjump" };

        runProperties90.Append(hyperlinkOnClick2);
        Drawing.Text text89 = new Drawing.Text();
        text89.Text = "Link to slide 3";

        run65.Append(runProperties90);
        run65.Append(text89);
        Drawing.EndParagraphRunProperties endParagraphRunProperties59 = new Drawing.EndParagraphRunProperties() { Language = "en-ZA", Dirty = false };

        paragraph101.Append(run64);
        paragraph101.Append(break1);
        paragraph101.Append(run65);
        paragraph101.Append(endParagraphRunProperties59);

        textBody65.Append(bodyProperties65);
        textBody65.Append(listStyle65);
        textBody65.Append(paragraph101);

        shape65.Append(nonVisualShapeProperties65);
        shape65.Append(shapeProperties65);
        shape65.Append(textBody65);

        return shape65;
    }

我认为这与本节有关,但我不确定这可能是什么

            SlideLayoutIdList slideLayoutIdList1 = new SlideLayoutIdList();
            SlideLayoutId slideLayoutId1 = new SlideLayoutId() { Id = (UInt32Value)2147483649U, RelationshipId = "rId2" };
            SlideLayoutId slideLayoutId2 = new SlideLayoutId() { Id = (UInt32Value)2147483650U, RelationshipId = "rId3" };
            slideLayoutIdList1.Append(slideLayoutId1);
            slideLayoutIdList1.Append(slideLayoutId2);
            lastSlidePart.SlideLayoutPart.SlideMasterPart.SlideMaster.Append(slideLayoutIdList1);

0 个答案:

没有答案