/// <summary>
        /// Replaces text tag with hyperlink id. Hyperlink identified by Id hes to be a part of the slide.
        /// </summary>
        /// <param name="p">The paragraph (a:p).</param>
        /// <param name="tag">The tag to replace by newText, if null or empty do nothing; tag is a regex string.</param>
        /// <param name="newText">The new text to replace the tag with, if null replaced by empty string and not visible.</param>
        /// <param name="relationshipId">Hyperlink relationship Id. Relationship has to be existing on slide level.</param>
        /// <param name="fontName">Font name</param>
        /// <param name="fontSize">Font size. E.g. 800 is 8pt (small) font. If value is less than 100 it will be multiplied by 100 to keep up with PPT notation.</param>
        /// <returns></returns>
        internal static bool ReplaceTagWithHyperlink(A.Paragraph p, string tag, string newText, string relationshipId, string fontName = "Calibri", int fontSize = 800)
        {
            bool replaced = false;

            if (string.IsNullOrEmpty(tag))
            {
                return(replaced);
            }

            if (newText == null)
            {
                newText = string.Empty;
            }
            newText = RemoveInvalidXMLChars(newText);

            while (true)
            {
                // Search for the tag
                Match match = Regex.Match(GetTexts(p), tag);
                if (!match.Success)
                {
                    break;
                }

                p.RemoveAllChildren(); // remove exisitng children then add new

                A.HyperlinkOnClick link = new A.HyperlinkOnClick()
                {
                    Id = relationshipId
                };
                A.Run r = new A.Run();
                r.RunProperties = new A.RunProperties();
                A.Text at = new A.Text(newText);
                r.RunProperties.AppendChild(link);
                r.AppendChild(at);
                p.Append(r);

                var run = p.Descendants <A.Run>();
                foreach (var item in run)
                {
                    item.RunProperties.RemoveAllChildren <A.LatinFont>();
                    var latinFont = new A.LatinFont();
                    latinFont.Typeface = fontName;
                    item.RunProperties.AppendChild(latinFont);
                    item.RunProperties.FontSize = (fontSize > 99) ? fontSize : fontSize * 100; // e.g. translate value 8 (Power Point UI font size) to 800 for API
                }

                replaced = true;
            }

            return(replaced);
        }
示例#2
0
        public static A.RunProperties CreateRunProperties(PPTXTextRun Text, Dictionary <string, string> HyperLinkIDMap)
        {
            A.RunProperties runProperties3 = new A.RunProperties()
            {
                Kumimoji = true, Language = "ja-JP", AlternativeLanguage = "en-US", FontSize = (int)(Text.Font.FontSize * 100), Dirty = false
            };

            runProperties3.Bold   = Text.Font.Bold;
            runProperties3.Italic = Text.Font.Italic;

            if (Text.Font.UnderLine)
            {
                runProperties3.Underline = A.TextUnderlineValues.Single;
            }

            if (Text.Font.Strike)
            {
                runProperties3.Strike = A.TextStrikeValues.SingleStrike;
            }

            A.LatinFont latinFont1 = new A.LatinFont()
            {
                Typeface = Text.Font.FontFamily, Panose = "020B0604030504040204", PitchFamily = 50, CharacterSet = -128
            };
            A.EastAsianFont eastAsianFont1 = new A.EastAsianFont()
            {
                Typeface = Text.Font.FontFamily, Panose = "020B0604030504040204", PitchFamily = 50, CharacterSet = -128
            };

            if (Text.Font.ForegroundColor.IsTransparent == false)
            {
                A.SolidFill solidFill1 = new A.SolidFill();
                solidFill1.Append(CreateRGBColorModeHex(Text.Font.ForegroundColor));
                runProperties3.Append(solidFill1);
            }

            runProperties3.Append(latinFont1);
            runProperties3.Append(eastAsianFont1);

            if (HyperLinkIDMap.ContainsKey(Text.Link.LinkKey))
            {
                A.HyperlinkOnClick hyperlinkOnClick1 = new A.HyperlinkOnClick()
                {
                    Id = HyperLinkIDMap[Text.Link.LinkKey]
                };

                runProperties3.Append(hyperlinkOnClick1);
            }


            return(runProperties3);
        }
示例#3
0
        public void AddVideo(SlidePart slidepart, string videoFilePath, string videoCoverPath, D.Transform2D transform)
        {
            Slide     slide        = slidepart.Slide;
            ShapeTree shapeTree1   = slidepart.Slide.CommonSlideData.ShapeTree;
            var       ptrlid       = Doc.PresentationPart.GetIdOfPart(slidepart);
            var       picID        = AnalysisHelper.GetMaxId(shapeTree1);
            string    imgEmbedId   = string.Format("imgId{0}{1}{2}", ptrlid, picID, 1);
            string    videoEmbedId = string.Format("vidId{0}{1}{2}", ptrlid, picID, 2);
            string    mediaEmbedId = string.Format("medId{0}{1}{2}", ptrlid, picID, 3);

            Picture picture1 = new Picture();
            NonVisualPictureProperties nonVisualPictureProperties1 = new NonVisualPictureProperties();

            NonVisualDrawingProperties nonVisualDrawingProperties2 =
                new NonVisualDrawingProperties()
            {
                Id = (UInt32Value)3U, Name = videoEmbedId + ""
            };

            D.HyperlinkOnClick hyperlinkOnClick1 = new D.HyperlinkOnClick()
            {
                Id = "", Action = "ppaction://media"
            };
            nonVisualDrawingProperties2.Append(hyperlinkOnClick1);

            NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties();

            D.PictureLocks pictureLocks1 = new D.PictureLocks()
            {
                NoChangeAspect = true
            };
            nonVisualPictureDrawingProperties1.Append(pictureLocks1);

            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties2 = new ApplicationNonVisualDrawingProperties();

            D.VideoFromFile videoFromFile1 = new D.VideoFromFile()
            {
                Link = videoEmbedId
            };

            ApplicationNonVisualDrawingPropertiesExtensionList applicationNonVisualDrawingPropertiesExtensionList1 =
                new ApplicationNonVisualDrawingPropertiesExtensionList();
            ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension1 =
                new ApplicationNonVisualDrawingPropertiesExtension()
            {
                Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}"
            };

            P14.Media media1 = new P14.Media()
            {
                Embed = mediaEmbedId
            };
            media1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
            applicationNonVisualDrawingPropertiesExtension1.Append(media1);
            applicationNonVisualDrawingPropertiesExtensionList1.Append(applicationNonVisualDrawingPropertiesExtension1);

            applicationNonVisualDrawingProperties2.Append(videoFromFile1);
            applicationNonVisualDrawingProperties2.Append(applicationNonVisualDrawingPropertiesExtensionList1);

            nonVisualPictureProperties1.Append(nonVisualDrawingProperties2);
            nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
            nonVisualPictureProperties1.Append(applicationNonVisualDrawingProperties2);

            BlipFill blipFill1 = new BlipFill();

            D.Blip blip1 = new D.Blip()
            {
                Embed = imgEmbedId
            };

            D.Stretch       stretch1       = new D.Stretch();
            D.FillRectangle fillRectangle1 = new D.FillRectangle();

            stretch1.Append(fillRectangle1);

            blipFill1.Append(blip1);
            blipFill1.Append(stretch1);

            ShapeProperties shapeProperties1 = new ShapeProperties();

            D.PresetGeometry presetGeometry1 = new D.PresetGeometry()
            {
                Preset = D.ShapeTypeValues.Rectangle
            };
            D.AdjustValueList adjustValueList1 = new D.AdjustValueList();

            presetGeometry1.Append(adjustValueList1);

            shapeProperties1.Append(transform);
            shapeProperties1.Append(presetGeometry1);

            picture1.Append(nonVisualPictureProperties1);
            picture1.Append(blipFill1);
            picture1.Append(shapeProperties1);

            shapeTree1.Append(picture1);

            if (!(slide.Timing?.ChildElements?.Count > 0))
            {
                AnalysisHelper.InitTiming(slide);
            }

            ImagePart imagePart = slidepart.AddImagePart(AnalysisHelper.GetImagePartType(videoCoverPath), imgEmbedId);

            using (var data = File.OpenRead(videoCoverPath))
            {
                imagePart.FeedData(data);
            };

            Doc.PartExtensionProvider.AddPartExtension("video/mp4", ".mp4");
            MediaDataPart mediaDataPart1 = Doc.CreateMediaDataPart("video/mp4", ".mp4");

            using (System.IO.Stream mediaDataPart1Stream = File.OpenRead(videoFilePath))
            {
                mediaDataPart1.FeedData(mediaDataPart1Stream);
            }
            slidepart.AddVideoReferenceRelationship(mediaDataPart1, videoEmbedId);
            slidepart.AddMediaReferenceRelationship(mediaDataPart1, mediaEmbedId);
            slide.Save();
        }
        /// <summary>
        /// Replaces a tag inside a paragraph (a:p) with parsed HTML
        /// </summary>
        /// <param name="p">The paragraph (a:p).</param>
        /// <param name="tag">The tag to replace by newText, if null or empty do nothing; tag is a regex string.</param>
        /// <param name="newText">The new text to replace the tag with, if null replaced by empty string and not visible.</param>
        /// <param name="fontName">Font name</param>
        /// <param name="fontSize">Font size. E.g. 800 is 8pt (small) font. If value is less than 100 it will be multiplied by 100 to keep up with PPT notation.</param>
        /// <param name="hyperlinks">URL Relationships dictionary. Relationship has to be defined on slide level.</param>
        /// <returns><c>true</c> if a tag has been found and replaced, <c>false</c> otherwise.</returns>
        internal static bool ReplaceTagWithHtml(A.Paragraph p, string tag, string newText, string fontName = null, int fontSize = 0, IDictionary <string, string> hyperlinks = null)
        {
            newText = CorrectUnhandledHtmlTags(newText); // e.g. deal with ul/li html tags
            bool isFirstLine = true;                     // avoiding unintentional empty line at the begining of the text
            bool replaced    = false;

            string[] closingTags = new string[] { "div", "p" }; // tags that force line break in PPTX paragraph

            if (string.IsNullOrEmpty(tag))
            {
                return(replaced);
            }

            if (newText == null)
            {
                newText = string.Empty;
            }
            newText = RemoveInvalidXMLChars(newText);

            while (true)
            {
                // Search for the tag
                Match match = Regex.Match(GetTexts(p), tag);
                if (!match.Success)
                {
                    break;
                }

                p.RemoveAllChildren(); // // remove exisitng children then add new

                HtmlParser hp = new HtmlParser(newText);
                MariGold.HtmlParser.IHtmlNode nodes = null;

                try
                {
                    nodes = hp.FindBodyOrFirstElement();
                }
                catch
                {
                    Console.WriteLine(String.Format("WARNING: HTML is empty or HTML schema has errors. Parsed HTML[]: [{0}]", newText));
                }

                while (nodes != null)
                {
                    foreach (var item in nodes.Children)
                    {
                        bool  skipLineBreak = false;
                        A.Run r             = new A.Run();
                        r.RunProperties = new A.RunProperties();

                        if (item.Html.Contains("<b>") || item.Html.Contains("<strong>"))
                        {
                            r.RunProperties.Bold = new DocumentFormat.OpenXml.BooleanValue(true);
                        }
                        if (item.Html.Contains("<i>") || item.Html.Contains("<em>"))
                        {
                            r.RunProperties.Italic = new DocumentFormat.OpenXml.BooleanValue(true);
                        }
                        if (item.Html.Contains("<u>") || item.Html.Contains("text-decoration: underline"))
                        {
                            r.RunProperties.Underline = new DocumentFormat.OpenXml.EnumValue <A.TextUnderlineValues>(A.TextUnderlineValues.Dash);
                        }
                        if (item.Html.Contains("<a"))
                        {
                            string uriId = null;
                            try
                            {
                                string url = PptxSlide.ParseHttpUrls(item.Html).First().Value;
                                uriId = hyperlinks.Where(q => q.Value == url).FirstOrDefault().Key;
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("URL is no available");
                            }
                            if (uriId != null)
                            {
                                A.HyperlinkOnClick link = new A.HyperlinkOnClick()
                                {
                                    Id = uriId
                                };
                                r.RunProperties.AppendChild(link);
                            }
                        }

                        A.Text at = new A.Text(PptxTemplater.TextTransformationHelper.HtmlToPlainTxt(item.InnerHtml) + " "); // clear not interpreted html tags
                        if (at.InnerText.Trim() == "" && isFirstLine)
                        {
                            at          = new A.Text(); // avoid excessive new lines
                            isFirstLine = false;
                        }
                        r.AppendChild(at);
                        p.Append(r);

                        // LINE BREAK -- if outer tag is div add line break
                        if (closingTags.Contains(item.Parent.Tag) && skipLineBreak == false)
                        {
                            p.Append(new A.Break());
                        }
                    }

                    // remove parsed html part
                    newText = newText.Substring(nodes.Html.Length);
                    if (newText.Trim() == "")
                    {
                        break;
                    }
                    hp    = new HtmlParser(newText);
                    nodes = hp.FindBodyOrFirstElement();
                }

                var run = p.Descendants <A.Run>();
                foreach (var item in run)
                {
                    if (fontName != null)
                    {
                        item.RunProperties.RemoveAllChildren <A.LatinFont>();
                        var latinFont = new A.LatinFont();
                        latinFont.Typeface = fontName;
                        item.RunProperties.AppendChild(latinFont);
                    }
                    if (fontSize > 0)
                    {
                        item.RunProperties.FontSize = (fontSize > 99) ? fontSize : fontSize * 100; // e.g. translate value 8 (Power Point UI font size) to 800 for API
                    }
                }

                replaced = true;
            }

            return(replaced);
        }