Skip to Main Content
Adding more elements to your Document – Part VI

Adding more elements to your Document – Part VI

Learn more about Xceed Words for .NET

 

This week, we will look at how to add HTML/RTF and Shapes, to your documents.

HTML/RTF

There are two options available to insert HTML or RTF data into a DocX document: either by inserting just a section of text, or by inserting another document into the current one.

 

Inserting a snippet of text is done by calling the document's InsertContent method.

The InsertContent method expects the following parameters:

  • The text to insert (with the HTML or RTF tags)

  • A ContentType value to specify if the text to insert contains HTML or RTF

  • Optional: the paragraph after which to insert the content, if left null, the text will be inserted at the current position in the document.

// Append HTML text at the end of the document
document.InsertContent( htmlData1, ContentType.Html);

// Append HTML text after a specific paragraph
document.InsertContent( htmlData2, ContentType.Html, p1 );

// Append RTF text at the end of the document
document.InsertContent( rtfData1, ContentType.Rtf);

// Append RTF text after a specific paragraph
document.InsertContent( rtfData2, ContentType.Rtf, p2 );

 

Inserting an HTML or RTF document into an existing document is done by calling the InsertDocument method on the destination document and using its overload that supports HTML/RTF documents.

The InsertDocument method expects the following parameters:

  • The filename of the document to insert (with the HTML or RTF tags)

  • A ContentType value to specify if the document to insert contains HTML or RTF

  • Optional: the paragraph after which to insert the document, if left null, the document will be added at the current position in the destination document.

// Append an HTML document at the end of the current document
document.InsertDocument( htmlFilename1, ContentType.Html);

// Append an HTML document after a specific paragraph in the current document
document.InsertDocument( htmlFilename2, ContentType.Html, p1 );

// Append an RTF document at the end of the current document
document.InsertDocument( rtfFilename1, ContentType.Rtf);

// Append an RTF document text after a specific paragraph in the current document
document.InsertDocument( rtfFilename2, ContentType.Rtf, p2 );

 

Shapes

 At the time of writing this article, a Shape can only be a Rectangle or a TextBox

 

Rectangle

Adding a rectangle shape is done in 2 steps:

  1. Add the rectangle to the document’s Shape collection by calling the document’s AddShape method.

  2. Append the rectangle to a paragraph by calling the InsertShape method.

// Add a shape and specify at least a width and height
var shape = document.AddShape( 100, 50 );

// Insert the Shape in a paragraph
var p = document.InsertParagraph( “Here is a simple default rectangle, positioned on the 16th character of this paragraph:” );
p.InsertShape( shape, 16 );

 

The AddShape method requires a width and height, but it also accepts additional optional parameters: fillColor, outlineColor, outlineWidth and outlineDash.

// Add a shape and specify all its properties
var shape2 = document.AddShape( 100, 50, Color.Orange, Color.Black, 4f, DashStyle.Dot );

// Insert the Shape at the end of a paragraph
var p2 = document.InsertParagraph( “Here is a custom rectangle appended to this paragraph:” );
p2.InsertShape( shape2 );

 

By using the various properties available on a shape object, you can customize how a rectangle shape is displayed.

Here is an example showing how you could add a rectangle shape with text wrapping:

// Create a document.
using( var document = DocX.Create( "AddShapeWithTextWrapping.docx" ) )
{
	// Add a title
	document.InsertParagraph( "Add a shape with Text Wrapping" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;

	// Add a shape and set its wrapping as Square.
	var shape = document.AddShape( 45, 45, Color.LightGray );
	shape.WrappingStyle = PictureWrappingStyle.WrapSquare;
	shape.WrapText = PictureWrapText.bothSides;

	// Set horizontal alignment with Alignment centered on the page.
	shape.HorizontalAlignment = WrappingHorizontalAlignment.CenteredRelativeToPage;

	// Set vertical alignment with an offset from top of paragraph.
	shape.VerticalOffsetAlignmentFrom = WrappingVerticalOffsetAlignmentFrom.Paragraph;
	shape.VerticalOffset = 20d;

	// Set a buffer on left and right of shape where no text will be drawn.
	shape.DistanceFromTextLeft = 5d;
	shape.DistanceFromTextRight = 5d;

	// Create a paragraph and append the shape to it.
	var p = document.InsertParagraph( "With its easy to use API, Xceed Words for .NET lets your application create new Microsoft Word .docx or PDF documents, or modify existing .docx documents. It gives you complete control over all content in a Word document, and lets you add or remove all commonly used element types, such as paragraphs, bulleted or numbered lists, images, tables, charts, headers and footers, sections, bookmarks, and more. Create PDF documents using the same API for creating Word documents." );
	p.Alignment = Alignment.both;
	p.AppendShape( shape );
	p.SpacingAfter( 50 );

	// Save the document
	document.Save();
}

 

TextBox

Adding a TextBox shape is done in 2 steps:

  1. Add the textbox to the document’s TextBoxes collection by calling the document’s AddTextBox method.

  2. Append the textbox to a paragraph by calling the InsertShape method.

// Add a TextBox and specify at least a width and height
var textBox = document.AddTextBox( 100, 50 );

// Insert the TextBox in a new paragraph
var p = document.InsertParagraph( “Here is a simple TextBox positioned on the 16th character of this paragraph:” );
p.InsertShape( textbox, 16 );

 

The AddTextBox method requires a width and height, but it also accepts additional optional parameters: text, formatting, fillColor, outlineColor, outlineWidth and outlineDash.

// Add a shape and specify all its properties
var textBox2 = document.AddTextBox( 100, 50, “My TextBox”, new Formatting() { FontColor = Color.Green } );
textBox2.TextVerticalAlignment = VerticalAlignment.Bottom;
textBox2.TextMarginBottom = 5d;
textBox2.TextMarginTop = 5d;
textBox2.TextMarginLeft = 5d;
textBox2.TextMarginRight = 5d;

// Insert the TextBox in a new paragraph
var p = document.InsertParagraph( "Here is a simple TextBox positioned on the 16th character of this paragraph." );
p.InsertShape( textBox, 16 );
p.SpacingAfter( 30 );

// Add a bold paragraph to the TextBox.
document.TextBoxes[ 0 ].InsertParagraph( "My New Paragraph" ).Bold();

 

Note: The Shape class has many other properties that can be modified separately after the Shape is first created. See the documentation for more information.

 

For more information, please refer to the documentation.

Join more than 100,000 satisfied customers now!

IBM
Deloitte
Microsoft
NASA
Bank of America
JP Morgan
Apple