Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Apr 03, 2019 5:36 am

I create a rectangle shape, but I want to add text in it from HTML.

If there is any other option to add shape with text from HTML please help me.

Find my code below by find text and hide it with shape having the new text:

Code: Select all
TextSelection[] texts = document.FindAllString("**##**", true, true);

                int count = 1;
                foreach(var txt in texts)
                {
                    var par = txt.GetAsOneRange().OwnerParagraph;
                    Spire.Doc.Fields.ShapeObject imageShape1 = par.AppendShape(800, 700, ShapeType.Rectangle);
                    imageShape1.DistanceLeft = -100;
                    imageShape1.DistanceRight = 0;
                    imageShape1.DistanceTop = 0;
                    imageShape1.DistanceBottom = 0;
                    imageShape1.Width = 800;
                    imageShape1.Height = 700;
                    imageShape1.LineStyle = ShapeLineStyle.ThinThick;
                    imageShape1.StrokeColor = Color.White;                   
                   
                    var footerParaghraph = new Paragraph(document);
                    var sss = $"<p>This Footer {count++}</p>";
                    footerParaghraph.AppendHTML(sss);
                    imageShape1.ChildObjects.Add(footerParaghraph);
                }

mkm090
 
Posts: 7
Joined: Tue Dec 01, 2015 10:25 am

Wed Apr 03, 2019 9:08 am

Hello,

Thanks for your post.
Sorry that our Spire.Doc doesn't support adding text into a shape, but it supports adding text into a TextBox. As for your scenario, you can try the following code to firstly add a TextBox and then change the type of shape to rectangle. If this is not what you want, please provide your testing Word file as well as your expected output to us, then we will look into it and guide you accordingly. You could send them to us via email (support@e-iceblue.com).
Code: Select all
 Document document = new Document();
 document.LoadFromFile(@"test.docx", Spire.Doc.FileFormat.Docx);
 TextSelection[] texts = document.FindAllString("**##**", true, true);
 foreach (var txt in texts)
 {
     TextRange range = txt.GetAsOneRange();
     Paragraph currentPar = txt.GetAsOneRange().OwnerParagraph;
     //add textBox
     TextBox tb = currentPar.AppendTextBox(120, 50);
     tb.Format.HorizontalOrigin = HorizontalOrigin.Margin;
     tb.Format.HorizontalPosition = 0;
     tb.Format.VerticalOrigin = VerticalOrigin.Margin;
     tb.Format.VerticalPosition = 50;
     tb.Format.LineStyle = TextBoxLineStyle.ThinThick;
     tb.Format.FillColor = Color.White;
     var sss = @"<p>This Footer {count++}</p>";
     //Append text from HTML
     Paragraph para = tb.Body.AddParagraph();
     para.AppendHTML(sss);
     //Change shape type
     tb.SetShapeType(ShapeType.Rectangle);
     range.OwnerParagraph.ChildObjects.Remove(range);
 }
 document.SaveToFile("temp.docx", Spire.Doc.FileFormat.Docx);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc