We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Write Html inside a textbox

Hello,

is it possible to insert Html into a textbox?
We would linke to insert a text with html tags with specific style information e.g. with <b> or <u> tags inside a textbox.

Thanks for your help,

Paul Bachmann

@B4chi,

Thanks for your inquiry. Please use DocumentBuilder.InsertHtml method to insert an HTML string into the document. If you want to insert HTML with tag, please use DocumentBuilder.Write method. Please check the following code example. Hope this helps you.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);
                
Shape textBox = new Shape(doc, ShapeType.TextBox);
Stroke stroke = textBox.Stroke;
textBox.WrapType = WrapType.None;
textBox.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
textBox.RelativeVerticalPosition = RelativeVerticalPosition.Page;

textBox.Width = 200;
textBox.Height = 200;
textBox.Left = 100;
textBox.Top = 100;

textBox.AppendChild(new Paragraph(doc));
builder.InsertNode(textBox);
builder.MoveTo(textBox.FirstChild);

builder.InsertHtml(
        "<b>Bold Text</b>" +
        "<b>Implicit paragraph</b>" +
        "<h1 align='left'>Heading 1 left.</h1>");


builder.Write(
        "<b>Bold Text</b>" +
        "<b>Implicit paragraph</b>" +
        "<h1 align='left'>Heading 1 left.</h1>");
doc.Save(MyDir + "Out.docx");

Thanks for your fast reply.

I already try this solution and it works fine for one textbox. But if I moving the builder to the documents end and try to insert another textbox I get no exceptions or something but the generated document (.docx) can not be opened.
There is an error while opening the generated docx file.

Hope you can understand my problem.

Ok I find a solution for that problem.
Here is my code for others with the same problem.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);

/* 
Styling of the textBox ...
*/

textBox.AppendChild(new Paragraph(doc));
builder.InsertNode(textBox);
builder.MoveTo(textBox.FirstChild);

builder.InsertHtml(
    "<b>Bold Text</b>" +
    "<b>Implicit paragraph</b>" +
    "<h1 align='left'>Heading 1 left.</h1>");

builder.moveTo(textBox.getParentNote());

Thanks for your help.

Greetings

Paul Bachmann

@B4chi,

It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Ok now I have another problem.
When I inserting other components into the textbox for example fields for pagenumbers the whole pagelayout breaks. That means that teh bextboxes are now on the wrong page of the document.
Have you any solution for this issue?

Thank you.

@B4chi,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.