private bool GetParagraphChildElements(Shape shape, PPTParagraph par, bool hasText, OpenXmlElement obj) { if (obj is Run) { Run run = (Run)obj; hasText = true; PPTRunProperties runProp = new PPTRunProperties(par.defaultRunProperties); runProp.Text = run.Text.Text; runProp.SetRunProperties(run.RunProperties, shape, ref effectShapes); runProp.FontSize = Math.Round(fontScale * runProp.FontSize / Globals.PercentageConstant); par.RunPropList.Add(runProp); } else if (obj is Field) { Field run = (Field)obj; hasText = true; PPTRunProperties runProp = new PPTRunProperties(par.defaultRunProperties); runProp.Text = run.Text.Text; runProp.SetRunProperties(run.RunProperties, shape, ref effectShapes); runProp.FontSize = Math.Round(fontScale * runProp.FontSize / Globals.PercentageConstant); par.RunPropList.Add(runProp); } else if (obj is Break) { Break aBreak = (Break)obj; PPTRunProperties runProp = new PPTRunProperties(par.defaultRunProperties); runProp.SetRunProperties(aBreak.RunProperties, shape, ref effectShapes); runProp.FontSize = Math.Round(fontScale * runProp.FontSize / Globals.PercentageConstant); runProp.isBreak = true; par.RunPropList.Add(runProp); } return(hasText); }
//We have two similar methods. This is worse because it uses the Width property of each element instead of measuring the whole string. //There is mistake in the calculations coming from that and the difference is bigger when there are more elements. private void fixLeftSpacingForAlignment(List <HtmlText> textElements, PPTParagraph par) { int combinedWidth = 0; foreach (HtmlText textElement in textElements) { combinedWidth += textElement.width; } int bulletOffset = 0; if (par.bullet != null && textElements.Count > 0 && !textElements[0].isBullet) { combinedWidth += par.bullet.bulletSize; bulletOffset = par.bullet.bulletSize; } int currentLeft = 0; if ("Center".Equals(par.Align)) { currentLeft = ((this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight - Shape.LeftInset - Shape.RightInset) - combinedWidth) / 2; } else if ("Right".Equals(par.Align)) { currentLeft = (this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight - Shape.LeftInset - Shape.RightInset) - combinedWidth; } foreach (HtmlText textElement in textElements) { textElement.setLeft(currentLeft + par.Indent + bulletOffset + par.marginLeft + Shape.LeftInset); currentLeft += textElement.width; } }
private void SetSpecificProperties(SlidePart slidePart, Shape shape) { IsText = true; //Check if this is text if (shape.TextBody == null) { IsText = false; return; } if (shape.TextBody.BodyProperties != null) { if (shape.TextBody.BodyProperties.Anchor != null) { VerticalAlign = shape.TextBody.BodyProperties.Anchor; } if (shape.TextBody.BodyProperties.GetFirstChild <NormalAutoFit>() != null && shape.TextBody.BodyProperties.GetFirstChild <NormalAutoFit>().FontScale != null) { fontScale = shape.TextBody.BodyProperties.GetFirstChild <NormalAutoFit>().FontScale.Value; } } int index = 0; foreach (var paragraph in shape.TextBody.Descendants <Paragraph>()) { var par = new PPTParagraph(slide, placeholder) { Paragraph = index++ }; if (paragraph.ParagraphProperties != null) { int level = paragraph.ParagraphProperties.Level == null ? -1 : paragraph.ParagraphProperties.Level.Value; par.Level = level; } par.SetParagraphProperties(paragraph, slidePart, shapeListStyleMaster, shapeListStyleLayout); bool hasText = false; foreach (var obj in paragraph.ChildElements) { hasText = GetParagraphChildElements(shape, par, hasText, obj); } //This is because when we set paragraph properties we add the bullet to the text runs. //If we don't have text it still outputs the bullet character. if (par.bullet != null && hasText) { par.RunPropList.Insert(0, par.bullet); } Texts.AddLast(par); } }
//We have two similar methods - this one is better because it measures the whole string with the font. private void fixLeftSpacingForAlignment(List <HtmlText> textElements, PPTParagraph par, Font font) { int combinedWidth = 0; StringBuilder combinedText = new StringBuilder(); foreach (HtmlText textElement in textElements) { if (textElement.PictureBullet) { combinedWidth += textElement.bulletSize; } else { combinedText.Append(textElement.Text); } } int bulletOffset = 0; if (par.bullet != null && textElements.Count > 0 && !textElements[0].isBullet) { bulletOffset = par.bullet.bulletSize; combinedWidth += par.bullet.bulletSize; } combinedWidth += MeasureString(combinedText.ToString(), font); int firstLeft = 0; if ("Center".Equals(par.Align)) { firstLeft = ((this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight - Shape.LeftInset - Shape.RightInset) - combinedWidth) / 2; } else if ("Right".Equals(par.Align)) { firstLeft = (this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight - Shape.LeftInset - Shape.RightInset) - combinedWidth; } combinedText = new StringBuilder(); combinedWidth = 0; //Now used only for picture bullets! foreach (HtmlText textElement in textElements) { textElement.setLeft(firstLeft + par.Indent + bulletOffset + par.marginLeft + combinedWidth + Shape.LeftInset + MeasureString(combinedText.ToString(), font)); if (textElement.PictureBullet) { combinedWidth += textElement.bulletSize; } else { combinedText.Append(textElement.Text); } } }
private IEnumerable <PPTRunProperties> breakTextsToShape(PPTParagraph par) { List <Dom.PPTTexts.PPTRunProperties> list = par.RunPropList; List <PPTRunProperties> result = new List <PPTRunProperties>(); String previousToken = null; int bulletSize = 0; foreach (var text in list) { float points = float.Parse(text.FontSize.ToString()) * 72.0F / 96.0F; Font font = new System.Drawing.Font(text.FontFamily.ToString(), points); if (text.Bold) { font = new System.Drawing.Font(text.FontFamily.ToString(), points, FontStyle.Bold); } else if (text.Italic) { font = new System.Drawing.Font(text.FontFamily.ToString(), points, FontStyle.Italic); } else if (text.Underline != null && text.Underline.Equals("Single")) { font = new System.Drawing.Font(text.FontFamily.ToString(), points, FontStyle.Underline); } int size = 0; if (text.isBullet && text.Text != null && text.Text.Contains("rId")) { bulletSize = text.bulletSize; } else { size = MeasureString((previousToken == null ? "" : previousToken + " ") + text.Text, font); } if (text.isBreak || size + bulletSize < this.width - par.Indent - par.marginLeft - par.marginRight - Shape.LeftInset - Shape.RightInset) { if (text.Text != null && text.Text.Trim() != "" && !(text.isBullet && text.Text.Contains("rId"))) { previousToken = (previousToken == null ? "" : previousToken) + text.Text; } if (text.isBreak) { previousToken = null; } result.Add(text); continue; } // previousToken = null; string[] tokens = text.Text.Split(' '); int index = 0; foreach (string token in tokens) { index++; int combinedSize = MeasureString((previousToken == null ? "" : previousToken + " ") + token, font); if (combinedSize + bulletSize > this.width - par.Indent - par.marginLeft - par.marginRight - Shape.LeftInset - Shape.RightInset) { PPTRunProperties temp = new PPTRunProperties(text); temp.Text = ""; temp.isBreak = true; result.Add(temp); temp = new PPTRunProperties(text); temp.Text = index < tokens.Length? token + " ":token; result.Add(temp); previousToken = token; } else { PPTRunProperties temp = new PPTRunProperties(text); temp.Text = index < tokens.Length ? token + " " : token; result.Add(temp); previousToken = (previousToken == null ? "" : previousToken + " ") + token; } } } return(result); }