Skip to Main Content
Adding more elements to a Document - Part I

Adding more elements to a Document - Part I

Learn more about Xceed Words for .NET

 

Now that we know the basics of creating a new document and how to use different Sections, let’s look at the elements that can be added in a document. There is a lot to cover, so this will be split in multiple parts.

For this time, we will look at how to add Lists and Pictures.

Lists

Adding a List is done by calling AddList on the Document. There are 2 types of lists that can be added: Numbered and Bulleted. Once the list is created, we can add items to it by calling AddListItem on the Document.

  • Parameters when creating a list with AddList: listText, level, listType, startNumber, trackChanges, continueNumbering and formatting. All these parameters are optional.

  • Parameters when adding items to a list with AddListItem: list, listText, level, listType, startNumber, trackChanges, continueNumbering and formatting. Only list and listText are required, the others are optional.

Note: if you have an existing list, it can be added by calling AddList and using the other overload that takes only an existing list as parameter.

 

Numbered List

// Create a document
using( var document = DocX.Create( “AddNumberedList.docx” ) )
{
	// Add a numbered list where the first ListItem starts with number 1
	var numberedList = document.AddList(“Berries”, 0, ListItemType,Numbered, 1);
	
	// Add sub-items (level 1) to the preceding ListItem
	document.AddListItem( numberedList, “Strawberries”, 1 );
	document.AddListItem( numberedList, “Blueberries”, 1 );
	
	// Add an item (level 0)
	document.AddListItem( numberedList, “Banana” );

	// Add an item (level 0)
	document.AddListItem( numberedList, “Apples” );

	// Add sub-items (level 1) to the preceding item
	document.AddListItem( numberedList, “Gala”, 1 );
	document.AddListItem( numberedList, “Honeycrisp”, 1 );

	// Insert the list into the document
	document.InsertParagraph( “This is a Numbered List:n” );
	document.InsertList( numberedList );

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

 

Bulleted List

// Create a document
using( var document = DocX.Create( “AddBulletedList.docx” ) )
{
	// Add a bulleted list with its first item
	var bulletedList = document.AddList(“Canada”, 0, ListItemType.Bulleted );

	// Add sub-items (level 1) to the preceding ListItem
	document.AddListItem( bulletedList, “Montreal”, 1 );
	document.AddListItem( bulletedList, “Toronto”, 1 );

	// Add an item (level 0)
	document.AddListItem( bulletedList, “Brazil” );

	// Add an item (level 0)
	document.AddListItem( bulletedList, “USA” );

	// Add sub-items (level 1) to the preceding item
	document.AddListItem( bulletedList, “New York”, 1 );
	document.AddListItem( bulletedList, “Los Angeles”, 1 );

	// Insert the list into the document
	document.InsertParagraph( “This is a Bulleted List:n” );
	document.InsertList( bulletedList );

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

 

Pictures

Adding a picture is done in 3 steps:

  1. Add the image to the document with the AddImage method.

  2. Call the image’s CreatePicture method to create a picture object.

  3. Append the picture to the document by calling the AppendPicture method.

// Create a document
using( var document = DocX.Create( “AddPicture.docx” ) )
{
	// Add a simple image from disk
	var image = document.AddImage( “balloon.jpg”  );

	// Set Picture height and width
	var picture = image.CreatePicture( 150, 150 );

	// Insert Picture in paragraph
	var p = document.InsertParagraph( “Here is a simple picture added from disk:” );
	p.AppendPicture( picture );

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

 

By using the properties available on a picture object, you can also customize how a picture is displayed.

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

// Create a document
using( var document = DocX.Create( "AddPictureWithTextWrapping.docx" ) )
{
	// Add a simple image from disk
	var image = document.AddImage( "WordsIcon.png" );

	// Set Picture height and width, and set its wrapping as Square
	var picture = image.CreatePicture( 60, 60 );
	picture.WrappingStyle = PictureWrappingStyle.WrapSquare;
	picture.WrapText = PictureWrapText.bothSides;

	// Set horizontal alignment with Alignement centered on the page.
	picture.HorizontalAlignment = PictureHorizontalAlignment.CenteredRelativeToPage;

	// Set vertical alignement with an offset from top of paragraph.
	picture.VerticalOffsetAlignmentFrom = PictureVerticalOffsetAlignmentFrom.Paragraph;
	picture.VerticalOffset = 25d;

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

	// Add a paragraph and the picture in 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.InsertPicture( picture );

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

 

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