En savoir plus sur Xceed Words pour .NET
Cette semaine, nous verrons comment ajouter du HTML/RTF et des formes à vos documents.
HTML/RTF
Il existe deux options pour insérer des données HTML ou RTF dans un document DocX : soit en insérant juste une section de texte, soit en insérant un autre document dans le document actuel.
L'insertion d'un extrait de texte se fait en appelant la fonction Insérer le contenu méthode.
Le Insérer le contenu attend les paramètres suivants :
- Le texte à insérer (avec les balises HTML ou RTF)
- A Type de contenu valeur permettant de spécifier si le texte à insérer contient du HTML ou du RTF
- Facultatif : le paragraphe après lequel insérer le contenu ; s'il est laissé nul, le texte sera inséré à la position actuelle dans le 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 );
L'insertion d'un document HTML ou RTF dans un document existant se fait en appelant la fonction Insérer un document sur le document de destination et en utilisant sa surcharge qui prend en charge les documents HTML/RTF.
Le Insérer un document attend les paramètres suivants :
- Le nom de fichier du document à insérer (avec les balises HTML ou RTF)
- A Type de contenu valeur permettant de spécifier si le document à insérer contient du HTML ou du RTF
- Facultatif : le paragraphe après lequel insérer le document ; s'il est laissé nul, le document sera ajouté à la position actuelle dans le document de destination.
// 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 );
Formes
Au moment de la rédaction de cet article, une forme ne peut être qu'un rectangle ou une TextBox.
Rectangle
L'ajout d'un rectangle se fait en deux étapes :
- Ajouter le rectangle à la collection Shape du document en appelant la fonction AddShape méthode.
- Ajouter le rectangle à un paragraphe en appelant la fonction InsertShape méthode.
// 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 );
Le AddShape nécessite une largeur et une hauteur, mais elle accepte également des paramètres facultatifs supplémentaires : fillColor, outlineColor, outlineWidth et 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 );
En utilisant les différentes propriétés disponibles sur un objet de forme, vous pouvez personnaliser l'affichage d'un rectangle.
Voici un exemple montrant comment vous pouvez ajouter une forme de rectangle avec un texte enveloppant :
// 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
L'ajout d'une forme TextBox se fait en 2 étapes :
- Ajouter la zone de texte à la collection TextBoxes du document en appelant la fonction AddTextBox méthode.
- Ajouter la zone de texte à un paragraphe en appelant la fonction InsertShape méthode.
// 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 );
Le AddTextBox nécessite une largeur et une hauteur, mais elle accepte également des paramètres facultatifs supplémentaires : texte, formatage, fillColor, outlineColor, outlineWidth et 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 : La classe Shape possède de nombreuses autres propriétés qui peuvent être modifiées séparément après la création de la forme. Voir la documentation pour plus d'informations.
Pour plus d'informations, veuillez vous référer à la la documentation.