Exemplo n.º 1
0
        public static a.Paragraph SetText(this a.Paragraph paragraph, string text, Font font = null, Color?fontColor = null)
        {
            paragraph.RemoveAllChildren <a.Run>();
            var run = new a.Run().Init(text, font, fontColor);

            paragraph.Append(run);
            return(paragraph);
        }
Exemplo n.º 2
0
 // ReSharper disable once UnusedParameter.Local
 private static void UpdatePowerPointBlock(OpenXmlPartContainer container, OpenXmlElement block, string content)
 {
     OXP.Shape shape = (OXP.Shape)block.CloneNode(true);
     OXD.Run   run   = (OXD.Run)shape.TextBody.Descendants <OXD.Run>().First().CloneNode(true);
     run.Text = new OXD.Text(content);
     OXD.Paragraph paragraph = shape.TextBody.GetFirstChild <OXD.Paragraph>();
     paragraph.RemoveAllChildren <OXD.Run>();
     OXD.EndParagraphRunProperties endP = paragraph.GetFirstChild <OXD.EndParagraphRunProperties>();
     paragraph.InsertBefore(run, endP);
     block.Parent.ReplaceChild(shape, block);
 }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        public static void FormatFLow(SlidePart slidePart)
        {
            if (slidePart != default(SlidePart))
            {
                IEnumerable <Shape> flowChartShapes = slidePart.Slide.Descendants <Shape>().Where(d => d.Descendants <NonVisualDrawingProperties>().Where(e => e.Name.InnerText.Contains(Constants._ARROW_)).Count() > 0);
                int           i       = 0;
                long          firstId = 0;
                D.Transform2D prevBox = new D.Transform2D();
                foreach (Shape shape in flowChartShapes)
                {
                    String title = String.Empty;
                    NonVisualDrawingProperties meta = shape.Descendants <NonVisualDrawingProperties>().FirstOrDefault();


                    //resize shape
                    D.Transform2D currentShapePos = shape.Descendants <D.Transform2D>().FirstOrDefault();

                    currentShapePos.Extents.Cx = 2775204;
                    currentShapePos.Extents.Cy = 1446936;
                    if (i == 0)
                    {
                        firstId = meta.Id;
                        currentShapePos.Offset.X = 458569;
                        currentShapePos.Offset.Y = 1712339;
                    }
                    else
                    {
                        currentShapePos.Offset.X = Convert.ToInt64((prevBox.Extents.Cx.Value - (prevBox.Extents.Cx.Value * 0.20)) + prevBox.Offset.X);
                        currentShapePos.Offset.Y = prevBox.Offset.Y;
                    }



                    prevBox = currentShapePos;


                    if (meta != default(NonVisualDrawingProperties))
                    {
                        long idToFind = (firstId + i) - (flowChartShapes.Count() + 1);//check

                        Shape titleShape = slidePart.Slide.Descendants <Shape>().Where(d => d.Descendants <NonVisualDrawingProperties>().First() != default(NonVisualDrawingProperties) &&
                                                                                       d.Descendants <NonVisualDrawingProperties>().First().Id == idToFind &&
                                                                                       d.Descendants <NonVisualDrawingProperties>().First().Name.InnerText.Contains(Constants._TEXTBOX_))
                                           .FirstOrDefault();
                        if (titleShape != default(Shape))
                        {
                            D.Paragraph data = titleShape.Descendants <D.Paragraph>().FirstOrDefault();

                            title = data != default(D.Paragraph) ? data.InnerText : title;
                        }

                        //slidePart.Slide.RemoveChild(titleShape);
                        titleShape.Remove();
                        slidePart.Slide.Save();

                        D.Paragraph paragraph = shape.Descendants <D.Paragraph>().FirstOrDefault();
                        if (paragraph != default(D.Paragraph))
                        {
                            paragraph.RemoveAllChildren();
                            slidePart.Slide.Save();
                            D.Run run1 = new D.Run();
                            paragraph.AppendChild(new D.ParagraphProperties()
                            {
                                Alignment = D.TextAlignmentTypeValues.Center
                            });
                            run1.AppendChild(new D.RunProperties()
                            {
                                Language = "en-US", Dirty = true
                            });
                            run1.Text = new D.Text(title);
                            paragraph.AppendChild(run1);
                        }
                        slidePart.Slide.Save();
                    }
                    i++;
                }
            }
        }
Exemplo n.º 5
0
        /// <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);
        }