Añadir más elementos a un documento - Parte I

Ahora que conocemos los conceptos básicos de la creación de un nuevo documento y cómo utilizar las diferentes Secciones, vamos a ver los elementos que se pueden añadir en un documento. Hay mucho que cubrir, así que esto se dividirá en varias partes.

Más información Xceed Words para .NET

Ahora que conocemos los conceptos básicos de la creación de un nuevo documento y cómo utilizar las diferentes Secciones, vamos a ver los elementos que se pueden añadir en un documento. Hay mucho que cubrir, así que esto se dividirá en varias partes.

En esta ocasión, veremos cómo añadir Listas e Imágenes.

Listas

Para añadir una lista, llame a AddList en el Documento. Hay 2 tipos de listas que se pueden añadir: Numeradas y con viñetas. Una vez creada la lista, podemos añadirle elementos llamando a AddListItem en el Documento.

  • Parámetros al crear una lista con AddList: listText, level, listType, startNumber, trackChanges, continueNumbering y formatting. Todos estos parámetros son opcionales.
  • Parámetros al añadir elementos a una lista con AddListItem: list, listText, level, listType, startNumber, trackChanges, continueNumbering y formatting. Sólo list y listText son obligatorios, los demás son opcionales.

Nota: si ya dispone de una lista, puede añadirla llamando a AddList y utilizando la otra sobrecarga que sólo toma como parámetro una lista existente.

Lista numerada

// 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();
}

Lista con viñetas

// 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();
}

Fotos

Añadir una imagen se hace en 3 pasos:

  1. Añade la imagen al documento con el método AddImage.
  2. Llama al método CreatePicture de la imagen para crear un objeto imagen.
  3. Añade la imagen al documento llamando al método AppendPicture.
// 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();
}

Utilizando las propiedades disponibles en un objeto de imagen, también puede personalizar cómo se muestra una imagen.

Este es un ejemplo de cómo añadir una imagen con texto:

// 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();
}