コード例 #1
0
        public static void AddSvg(Stream stream, string svgPath, double percentageOfCy)
        {
            using PresentationDocument presentationDocument = PresentationDocument.Open(stream, true);
            PresentationPart presentationPart = presentationDocument.PresentationPart ??
                                                throw new InvalidOperationException(@"PresentationDocument is invalid.");

            // Get relationship ID of first slide.
            string sldRelId = presentationPart.GetXDocument()
                              .Elements(P.presentation)
                              .Elements(P.sldIdLst)
                              .Elements(P.sldId)
                              .Select(sldId => (string)sldId.Attribute(R.id) !)
                              .FirstOrDefault() ?? throw new InvalidOperationException(@"Presentation has no slides.");

            // Get first slide's part.
            var slidePart = (SlidePart)presentationPart.GetPartById(sldRelId);

            // Determine unique relationship IDs for new image parts.
            int    partsCount = slidePart.Parts.Count();
            string pngRelId   = $"rId{++partsCount}";
            string svgRelId   = $"rId{++partsCount}";

            // Add new image part with PNG image to slide part.
            using Stream pngStream = GeneralTools.ReadSvgAsPng(svgPath);
            GeneralTools.AddImagePart(slidePart, ImagePartType.Png, pngRelId, pngStream);

            // Add new image part with SVG image to slide part.
            using Stream svgStream = File.Open(svgPath, FileMode.Open, FileAccess.Read);
            GeneralTools.AddImagePart(slidePart, ImagePartType.Svg, svgRelId, svgStream);

            var picture =
                new XElement(P.pic,
                             new XElement(P.nvPicPr,
                                          new XElement(P.cNvPr,
                                                       new XAttribute(NoNamespace.id, ++partsCount),
                                                       new XAttribute(NoNamespace.name, "Picture 1"),
                                                       new XElement(A.extLst,
                                                                    new XElement(A.ext,
                                                                                 new XAttribute(NoNamespace.uri, "{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}"),
                                                                                 new XElement(A16.creationId,
                                                                                              new XAttribute(XNamespace.Xmlns + "a16", A16.a16),
                                                                                              new XAttribute(NoNamespace.id, GeneralTools.GetNewGuidString()))))),
                                          new XElement(P.cNvPicPr,
                                                       new XElement(A.picLocks,
                                                                    new XAttribute(NoNamespace.noChangeAspect, "1"))),
                                          new XElement(P.nvPr)),
                             new XElement(P.blipFill,
                                          new XElement(A.blip,
                                                       new XAttribute(R.embed, pngRelId),
                                                       new XElement(A.extLst,
                                                                    new XElement(A.ext,
                                                                                 new XAttribute(NoNamespace.uri, "{96DAC541-7B7A-43D3-8B79-37D633B846F1}"),
                                                                                 new XElement(ASVG.svgBlip,
                                                                                              new XAttribute(XNamespace.Xmlns + "asvg", ASVG.asvg),
                                                                                              new XAttribute(R.embed, svgRelId))))),
                                          new XElement(A.stretch,
                                                       new XElement(A.fillRect))),
                             GetSpPr(presentationPart, percentageOfCy));

            XElement spTree = slidePart.GetXDocument()
                              .Elements(P.sld)
                              .Elements(P.cSld)
                              .Elements(P.spTree)
                              .Single();

            spTree.Add(picture);

            slidePart.SaveXDocument();
        }
コード例 #2
0
        public static void AddSvg(Stream stream, string svgPath, double percentageOfCy)
        {
            using PresentationDocument presentationDocument = PresentationDocument.Open(stream, true);
            PresentationPart presentationPart = presentationDocument.PresentationPart ??
                                                throw new InvalidOperationException(@"PresentationDocument is invalid.");

            // Get relationship ID of first slide.
            string sldRelId = presentationPart
                              .Presentation
                              .SlideIdList?
                              .Elements <Presentation.SlideId>()
                              .Select(slideId => (string)slideId.RelationshipId !)
                              .FirstOrDefault() ?? throw new InvalidOperationException(@"Presentation has no slides.");

            // Get first slide's part.
            var slidePart = (SlidePart)presentationPart.GetPartById(sldRelId);

            // Determine unique relationship IDs for new image parts.
            int    partsCount = slidePart.Parts.Count();
            string pngRelId   = $"rId{++partsCount}";
            string svgRelId   = $"rId{++partsCount}";

            // Add new image part with PNG image to slide part.
            using Stream pngStream = GeneralTools.ReadSvgAsPng(svgPath);
            GeneralTools.AddImagePart(slidePart, ImagePartType.Png, pngRelId, pngStream);

            // Add new image part with SVG image to slide part.
            using Stream svgStream = File.Open(svgPath, FileMode.Open, FileAccess.Read);
            GeneralTools.AddImagePart(slidePart, ImagePartType.Svg, svgRelId, svgStream);

            // Create markup.
            var picture =
                new Presentation.Picture(
                    new Presentation.NonVisualPictureProperties(
                        new Presentation.NonVisualDrawingProperties(
                            new Drawing.NonVisualDrawingPropertiesExtensionList(
                                new Drawing.Extension(
                                    new CreationId
            {
                Id = GeneralTools.GetNewGuidString(),
            }.WithNamespaceDeclaration("a16", "http://schemas.microsoft.com/office/drawing/2014/main"))
            {
                Uri = "{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}",
            }))
            {
                Id   = (uint)++partsCount,
                Name = "Picture 1",
            },
                        new Presentation.NonVisualPictureDrawingProperties(
                            new Drawing.PictureLocks
            {
                NoChangeAspect = true,
            }),
                        new Presentation.ApplicationNonVisualDrawingProperties()),
                    new Presentation.BlipFill(
                        new Drawing.Blip(
                            new Drawing.BlipExtensionList(
                                new Drawing.Extension(
                                    new SVGBlip
            {
                Embed = svgRelId,
            })
            {
                Uri = "{96DAC541-7B7A-43D3-8B79-37D633B846F1}",
            }))
            {
                Embed = pngRelId,
            },
                        new Drawing.Stretch(
                            new Drawing.FillRectangle())),
                    GetShapeProperties(presentationPart, percentageOfCy));

            Presentation.ShapeTree shapeTree = slidePart
                                               .Slide
                                               .CommonSlideData?
                                               .ShapeTree ?? throw new InvalidOperationException();

            shapeTree.AppendChild(picture);
        }