在幻灯片(PPTX)中的特定位置缩放和插入图像

时间:2015-11-02 06:41:16

标签: c# powerpoint openxml openxml-sdk

我正在尝试将图像插入幻灯片(每张幻灯片3张图片),我可以为每张幻灯片插入一张图片,但我想要实现的更复杂,我需要将每张图片缩放到特定尺寸(两个高度)并将其插入到幻灯片中,我想让这个过程变得简单的是将文本放在幻灯片中(作为模板,因为幻灯片是固定的),用图像替换文本。 我已经google了很多,并没有找到很多帮助缩放和将图像放在幻灯片中,下面是msdn的链接,每个幻灯片放置一个图像     Placing one image per slide - MSDN

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

最后得到了一些R& D使用openxml生产力工具并得出答案, 假设模板已存在,下面的代码将图像放置到指定的幻灯片

        public class SegmentationShapeProperties
    {
        public Int64 OffsetX { get; set; }
        public Int64 OffsetY { get; set; }
        public Int64 ScaleX { get; set; }
        public Int64 ScaleY { get; set; }
    }   
    public class SegmentationSlideInputData
    {
        public int SlideId { get; set; }
        public OpenXmlUtils.SegmentationShapeProperties ShapeProperties { get; set; }
    }

 public void InsertImages(List<string> imageFilesWithPath, string presentation)
    {
        using (PresentationDocument prstDoc = PresentationDocument.Open(presentation, true))
        {
            PresentationPart presentationPart = prstDoc.PresentationPart;
            var slideParts = OpenXmlUtils.GetSlidePartsInOrder(presentationPart); //gets all the slide parts present in the documetn
            Slide slide = null;
            foreach (string imageWithPath in imageFilesWithPath)
            {
                SegmentationSlideInputData data = GetWorkingImageDetails(Path.GetFileName(imageWithPath));//function which decides which slide to work on and image scaling options
                slide = slideParts.ElementAt(data.SlideId).Slide;
                Picture pic = OpenXmlUtils.AddPicture(slide, imageWithPath, data.ShapeProperties);
                slide.Save();
            }
            prstDoc.PresentationPart.Presentation.Save();
        }
    }

  private SegmentationSlideInputData GetWorkingImageDetails(string fileName)
    {
        SegmentationSlideInputData data = new SegmentationSlideInputData();
        data.SlideId = 0;//slide id to work on
        data.ShapeProperties = new OpenXmlUtils.SegmentationShapeProperties() { OffsetX = 4695825L, OffsetY = 504825L, ScaleX = 6721828L, ScaleY = 1988495L };//offset specifies the position, scale specifies the height and widht of image
                break;
        }
        return data;
    }


internal static P.Picture AddPicture(this Slide slide, string imageFile, SegmentationShapeProperties sP)
    {
        P.Picture picture = new P.Picture();

        string embedId = string.Empty;
        UInt32Value picId = 10001U;
        string name = string.Empty;

        if (slide.Elements<P.Picture>().Count() > 0)
        {
            picId = ++slide.Elements<P.Picture>().ToList().Last().NonVisualPictureProperties.NonVisualDrawingProperties.Id;
        }
        name = "image" + picId.ToString();
        embedId = "rId" + (RandomString(5)).ToString(); // some value

        P.NonVisualPictureProperties nonVisualPictureProperties = new P.NonVisualPictureProperties()
        {
            NonVisualDrawingProperties = new P.NonVisualDrawingProperties() { Name = name, Id = picId, Title = name },
            NonVisualPictureDrawingProperties = new P.NonVisualPictureDrawingProperties() { PictureLocks = new Z.Drawing.PictureLocks() { NoChangeAspect = true } },
            ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties() { UserDrawn = true }
        };

        P.BlipFill blipFill = new P.BlipFill() { Blip = new Z.Drawing.Blip() { Embed = embedId } };
        Z.Drawing.Stretch stretch = new Z.Drawing.Stretch() { FillRectangle = new Z.Drawing.FillRectangle() };
        blipFill.Append(stretch);

        P.ShapeProperties shapeProperties = new P.ShapeProperties()
        {
            Transform2D = new Z.Drawing.Transform2D()
            {
                //Offset = new Z.Drawing.Offset() { X = 1835696L, Y = 1036712L },
                //Extents = new Z.Drawing.Extents() { Cx = 5334617, Cy = 1025963 }
                Offset = new Z.Drawing.Offset() { X = sP.OffsetX, Y = sP.OffsetY },
                Extents = new Z.Drawing.Extents() { Cx = sP.ScaleX, Cy = sP.ScaleY }
            }
        };
        Z.Drawing.PresetGeometry presetGeometry = new Z.Drawing.PresetGeometry() { Preset = Z.Drawing.ShapeTypeValues.Rectangle };
        Z.Drawing.AdjustValueList adjustValueList = new Z.Drawing.AdjustValueList();

        presetGeometry.Append(adjustValueList);
        shapeProperties.Append(presetGeometry);
        picture.Append(nonVisualPictureProperties);
        picture.Append(blipFill);
        picture.Append(shapeProperties);

        slide.CommonSlideData.ShapeTree.Append(picture);

        // Add Image part
        slide.AddImagePart(embedId, imageFile);

        slide.Save();
        return picture;
    }


private static void AddImagePart(this Slide slide, string relationshipId, string imageFile)
    {
        ImagePart imgPart = slide.SlidePart.AddImagePart(GetImagePartType(imageFile), relationshipId);
        using (FileStream imgStream = File.Open(imageFile, FileMode.Open))
        {
            imgPart.FeedData(imgStream);
        }
    }