OpenXML - add textbox to a slide Adds text to a slide. Usage: create your paragraphs using CreateParagraph and then call AddTextbox. using System; using System.Collections.Generic; using System.IO; using System.Linq; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; using C = DocumentFormat.OpenXml.Drawing.Charts; using Drawing = DocumentFormat.OpenXml.Drawing; /// <summary> /// Add a textbox to pptx slide /// </summary> /// <param name="slidePart"></param> /// <param name="boxName"></param> /// <param name="id"></param> /// <param name="xPos"></param> /// <param name="yPos"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="paragraphs">Use CreateParagraph to create paragraphs for your text body.</param> public static void AddTextbox(SlidePart slidePart, string boxName, uint id, long xPos, long yPos, long width, long height, List<Drawing.Paragraph> paragraphs) { var textBody = new TextBody(); Drawing.BodyProperties bodyProperties20 = new Drawing.BodyProperties() { Wrap = Drawing.TextWrappingValues.Square, RightToLeftColumns = false }; Drawing.ShapeAutoFit shapeAutoFit1 = new Drawing.ShapeAutoFit(); bodyProperties20.Append(shapeAutoFit1); Drawing.ListStyle listStyle20 = new Drawing.ListStyle(); textBody.Append(bodyProperties20); textBody.Append(listStyle20); foreach(var p in paragraphs) textBody.Append(p); Shape shape8 = new Shape(); NonVisualShapeProperties nonVisualShapeProperties8 = new NonVisualShapeProperties(); NonVisualDrawingProperties nonVisualDrawingProperties11 = new NonVisualDrawingProperties() { Id = (UInt32Value)id, Name = boxName }; NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties8 = new NonVisualShapeDrawingProperties() { TextBox = true }; ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties11 = new ApplicationNonVisualDrawingProperties(); nonVisualShapeProperties8.Append(nonVisualDrawingProperties11); nonVisualShapeProperties8.Append(nonVisualShapeDrawingProperties8); nonVisualShapeProperties8.Append(applicationNonVisualDrawingProperties11); ShapeProperties shapeProperties8 = new ShapeProperties(); var transform2D8 = new Drawing.Transform2D(); Drawing.Offset offset11 = new Drawing.Offset() { X = xPos, Y = yPos }; Drawing.Extents extents11 = new Drawing.Extents() { Cx = width, Cy = height }; transform2D8.Append(offset11); transform2D8.Append(extents11); Drawing.PresetGeometry presetGeometry7 = new Drawing.PresetGeometry() { Preset = Drawing.ShapeTypeValues.Rectangle }; Drawing.AdjustValueList adjustValueList7 = new Drawing.AdjustValueList(); presetGeometry7.Append(adjustValueList7); Drawing.NoFill noFill2 = new Drawing.NoFill(); shapeProperties8.Append(transform2D8); shapeProperties8.Append(presetGeometry7); shapeProperties8.Append(noFill2); shape8.Append(nonVisualShapeProperties8); shape8.Append(shapeProperties8); shape8.Append(textBody); slidePart.Slide.CommonSlideData.ShapeTree.Append(shape8); } public static Drawing.Paragraph CreateParagraph(string text, bool isLastParagraph, bool isBold) { Drawing.Paragraph paragraph25 = new Drawing.Paragraph(); Drawing.Run run21 = new Drawing.Run(); Drawing.RunProperties runProperties24 = new Drawing.RunProperties() { Language = "en-US", FontSize = 1400, Dirty = false, Bold = isBold }; Drawing.LatinFont latinFont24 = new Drawing.LatinFont() { Typeface = "Verdana", PitchFamily = 34, CharacterSet = 0 }; Drawing.EastAsianFont eastAsianFont24 = new Drawing.EastAsianFont() { Typeface = "Verdana", PitchFamily = 34, CharacterSet = 0 }; Drawing.ComplexScriptFont complexScriptFont24 = new Drawing.ComplexScriptFont() { Typeface = "Verdana", PitchFamily = 34, CharacterSet = 0 }; runProperties24.Append(latinFont24); runProperties24.Append(eastAsianFont24); runProperties24.Append(complexScriptFont24); Drawing.Text text24 = new Drawing.Text(); text24.Text = text; run21.Append(runProperties24); run21.Append(text24); paragraph25.Append(run21); if (isLastParagraph) { Drawing.EndParagraphRunProperties endParagraphRunProperties20 = new Drawing.EndParagraphRunProperties() { Language = "en-US", FontSize = 1400, Dirty = false }; Drawing.LatinFont latinFont26 = new Drawing.LatinFont() { Typeface = "Verdana", PitchFamily = 34, CharacterSet = 0 }; Drawing.EastAsianFont eastAsianFont26 = new Drawing.EastAsianFont() { Typeface = "Verdana", PitchFamily = 34, CharacterSet = 0 }; Drawing.ComplexScriptFont complexScriptFont26 = new Drawing.ComplexScriptFont() { Typeface = "Verdana", PitchFamily = 34, CharacterSet = 0 }; endParagraphRunProperties20.Append(latinFont26); endParagraphRunProperties20.Append(eastAsianFont26); endParagraphRunProperties20.Append(complexScriptFont26); paragraph25.Append(endParagraphRunProperties20); } return paragraph25; }
Created By: amos 5/8/2014 1:19:05 PM Updated: 5/8/2014 1:19:20 PM
|
|