在pptx

时间:2015-10-27 10:29:32

标签: c# powerpoint openxml-sdk

我使用文件夹中的图像创建了Powerpoint,一切正常,我想要做的是每张幻灯片插入4张图片,我引用下面的链接进行演示,它就像魅力一样  MSDN

然而,有一个代码,它说每张幻灯片插入多个图像,我们必须将硬编码关系id更改为更动态的ID,

// Create a slide part for the new slide.
var slidePart = presentationPart.AddNewPart<SlidePart>(relId);
GenerateSlidePart(imageFileNameNoPath, imageFileNameNoPath,
  imageWidthEMU, imageHeightEMU).Save(slidePart);

// Add the relationship between the slide and the
// slide layout.
slidePart.AddPart<SlideLayoutPart>(slideLayoutPart);

// Create an image part for the image used by the new slide.
// A hardcoded relationship id is used for the image part since
// there is only one image per slide. If more than one image
// was being added to the slide, an approach similar to that 
// used before for the slide part relationship id could be
// followed, where the image part relationship id could be
// incremented for each image part.
var imagePart = slidePart.AddImagePart(ImagePartType.Jpeg,"relId1");
GenerateImagePart(imagePart, imageBytes);

这样做会破坏Presentation,下面是修改过的函数

 using (PresentationDocument newDeck =
          PresentationDocument.Open(newPresentation, true))
        {
            PresentationPart presentationPart = newDeck.PresentationPart;

            // Reuse the slide master part. This code assumes that the
            // template presentation being used has at least one
            // master slide.
            var slideMasterPart = presentationPart.SlideMasterParts.First();

            // Reuse the slide layout part. This code assumes that the
            // template presentation being used has at least one
            // slide layout.
            var slideLayoutPart = slideMasterPart.SlideLayoutParts.First();

            // If the new presentation doesn't have a SlideIdList element
            // yet then add it.
            if (presentationPart.Presentation.SlideIdList == null)
                presentationPart.Presentation.SlideIdList = new SlideIdList();

            // Loop through each image file creating slides
            // in the new presentation.
            int imageNo = 0;
            SlidePart slidePart = null;
            foreach (string imageFileNameWithPath in imageFileNames)
            {

                imageFileNameNoPath =
                  Path.GetFileNameWithoutExtension(imageFileNameWithPath);

                // Create a unique relationship id based on the current
                // slide id.
                relId = "rel" + currentSlideId;
                imageRelId = "relId" + RandomString(5); //function to generate random string of length 5
                // Get the bytes, type and size of the image.
                ImagePartType imagePartType = ImagePartType.Png;
                byte[] imageBytes = GetImageData(imageFileNameWithPath,
                  ref imagePartType, ref imageWidthEMU, ref imageHeightEMU);

                if (imageNo % 4 == 0)
                {
                    // Create a slide part for the new slide.
                    slidePart = presentationPart.AddNewPart<SlidePart>(relId);

                    GenerateSlidePart(imageFileNameNoPath, imageFileNameNoPath,
                      imageWidthEMU, imageHeightEMU,imageRelId).Save(slidePart);

                    // Add the relationship between the slide and the
                    // slide layout.
                    slidePart.AddPart<SlideLayoutPart>(slideLayoutPart);
                }
                // Create an image part for the image used by the new slide.
                // A hardcoded relationship id is used for the image part since
                // there is only one image per slide. If more than one image
                // was being added to the slide an approach similar to that
                // used above for the slide part relationship id could be
                // followed, where the image part relationship id could be
                // incremented for each image part.
                var imagePart = slidePart.AddImagePart(ImagePartType.Jpeg,
                  imageRelId);
                GenerateImagePart(imagePart, imageBytes);


                if (imageNo % 4 == 0)
                {
                    // Add the new slide to the slide list.
                    slideId = new SlideId();
                    slideId.RelationshipId = relId;
                    slideId.Id = currentSlideId;
                    presentationPart.Presentation.SlideIdList.Append(slideId);

                    // Increment the slide id;
                    currentSlideId++;
                }
                imageNo++;
            }

            // Save the changes to the slide master part.
            slideMasterPart.SlideMaster.Save();
            //OpenXmlUtils.DeleteSlide(presentationPart, presentationPart.GetSlidePartsInOrder().Last());
            // Save the changes to the new deck.
            presentationPart.Presentation.Save();
        }

这是获取随机字符串的函数

  private static string RandomString(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var random = new Random();
        return new string(Enumerable.Repeat(chars, length)
          .Select(s => s[random.Next(s.Length)]).ToArray());
    }

0 个答案:

没有答案