Dokumentenelement-Tutorial
IronWord ist eine leistungsstarke Word-Dokumentenbibliothek, die .NET C#-Entwicklern dabei helfen soll, die Funktionen zum Erstellen, Lesen und Bearbeiten von Word- und DOCX-Dokumenten in ihre Anwendungen zu integrieren. Im Kontext eines Word-Dokuments sind die Dokumentelemente die Bausteine, aus denen der Inhalt besteht.
Inhaltsübersicht
-
Text hinzufügen
-
Text Inhalt (Hinzufügen, Anhängen & Teilen)
- Styling einstellen (Schriftfamilie und -größe, Farbe, fett und kursiv, durchgestrichen, unterstrichen, hochgestellt und tiefgestellt)
-
- Bilder einbetten
-
Bilder hinzufügen
-
Bild laden (Datei & FileStream)
-
Satz Abmessungen (Breite und Höhe)
- Abstand von den Ecken einstellen
Erste Schritte mit IronWORD
Beginnen Sie noch heute mit der Verwendung von IronWord in Ihrem Projekt mit einer kostenlosen Testversion.
-
TextRuns hinzufügen
Text Inhalt
Die Methode Split
wird verwendet, um den Textlauf in eine Liste kleinerer TextRuns zu unterteilen, die auf einem bestimmten Trennzeichen basieren. Dies ermöglicht die Organisation und Manipulation von Textinformationen innerhalb des Dokuments.
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.cs
using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
// Add text
TextContent addText = new TextContent("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));
// Append text
TextContent appendText = new TextContent("The first text.");
appendText.Append(new TextContent("The second text."));
doc.AddParagraph(new Paragraph(appendText));
// Split text
TextContent splitText = new TextContent("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));
// Export docx
doc.SaveAs("textrun.docx");
Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
' Add text
Private addText As New TextContent("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))
' Append text
Dim appendText As New TextContent("The first text.")
appendText.Append(New TextContent("The second text."))
doc.AddParagraph(New Paragraph(appendText))
' Split text
Dim splitText As New TextContent("Use split to split the sentence.")
splitText.Split(" ")
doc.AddParagraph(New Paragraph(splitText))
' Export docx
doc.SaveAs("textrun.docx")
Styling einstellen
Mit der Einstellung des Stylings für TextRuns können Sie die visuelle Darstellung von Text festlegen. Dazu gehört die Angabe von Attributen wie Schriftgröße, Farbe, Stil, Durchstreichen, Unterstreichen, Hoch- und Tiefstellung. Durch das Einstellen der Formatierung wird das Gesamterscheinungsbild des Textes im Dokument verbessert.
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
TextContent textRun = new TextContent();
textRun.Text = "Add text using IronWord";
textRun.Style = new TextStyle()
{
TextFont = new Font()
{
FontFamily = "Caveat",
FontSize = 72,
},
Color = Color.Red,
IsBold = true,
IsItalic = true,
Underline = new Underline(),
Strike = StrikeValue.Strike,
};
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddText(textRun);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Private doc As New WordDocument("document.docx")
' Configure text
Private textRun As New TextContent()
textRun.Text = "Add text using IronWord"
textRun.Style = New TextStyle() With {
.TextFont = New Font() With {
.FontFamily = "Caveat",
.FontSize = 72
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddText(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")
Bilder einbetten
Mit dieser Funktion können Sie Bilder nahtlos in den Inhalt einfügen und so die visuelle Anziehungskraft und die Kommunikationskraft des Dokuments insgesamt verbessern.
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-embed-images.cs
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
TextContent textRun = new TextContent();
// Add image
Paragraph para = new Paragraph(textRun);
para.AddImage(image);
// Add paragraph
doc.AddParagraph(new Paragraph(textRun));
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
' Configure image
Private image As New ImageContent("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New TextContent()
' Add image
Dim para As New Paragraph(textRun)
para.AddImage(image)
' Add paragraph
doc.AddParagraph(New Paragraph(textRun))
' Export docx
doc.SaveAs("save_document.docx")
Bilder hinzufügen
Bild laden
Das Laden von Bildern ist ein wichtiger Prozess. Dabei werden externe Bilddateien in das Dokument eingefügt. Die Möglichkeit, Bilder zu laden, erleichtert die Einbeziehung relevanter visueller Elemente und trägt so zu einem ansprechenden und informativen Dokument bei.
:path=/static-assets/word/content-code-examples/tutorials/add-image-load-image.cs
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage("image.jpg");
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
Private paragraph As New Paragraph()
' Add image
paragraph.AddImage("image.jpg")
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")
Bild konfigurieren
Optimieren Sie die Bilder mit konfigurierbaren Einstellungen. Dazu gehört das Festlegen von Eigenschaften wie Textumbruch, Abmessungen, Position und Abstand zu den Ecken. Die richtige Konfiguration stellt sicher, dass die Bilder in einer optisch ansprechenden und kontextgerechten Weise angezeigt werden.
:path=/static-assets/word/content-code-examples/tutorials/add-image-configure-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.WrapText = WrapText.Square;
image.Width = 100;
image.Height = 100;
image.DistanceFromTop = 50;
var position = new ElementPosition();
position.X = 50;
position.Y = 50;
image.Position = position;
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Private doc As New WordDocument()
' Configure image
Private image As New ImageContent("image.jpg")
image.WrapText = WrapText.Square
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position = New ElementPosition()
position.X = 50
position.Y = 50
image.Position = position
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")