C# Belge Öğesi Öğreticisi
IronWord, .NET C# geliştiricilerine uygulamalarına Word ve DOCX belgeleri oluşturma, okuma ve düzenleme yeteneklerini entegre etme konusunda yardımcı olmak için tasarlanmış güçlü bir Word belge kütüphanesidir. Bir Word belgesi bağlamında, belge öğeleri içeriği oluşturan yapı taşlarıdır.
Hızlı Başlangıç: Şekillendirilmiş Bir Metin Çalıştırma (TextRun) ve Bir Resim Gömmeİşte nasıl IronWord kullanarak zengin içerik ekleyebileceğiniz - stil verilmiş metin ve bir belgeye gömülü resim birleştirerek. Bu örnek, stil verilmiş metin için Run sarmalayıcı modelini göstermektedir.
-
1IronWord ile NuGet Package Manager'u yükleyin
-
2Bu kod parçacığını kopyalayın ve çalıştırın.
using IronWord; using IronWord.Models; // Create document WordDocument doc = new WordDocument(); // Create styled Run Run textRun = new Run(new TextContent("Hello IronWord!")); textRun.Style = new TextStyle() { IsBold = true, Color = Color.Blue }; // Create paragraph and add Run Paragraph paragraph = new Paragraph(); paragraph.AddChild(textRun); paragraph.AddImage(new ImageContent("pic.png")); // Add to document and save doc.AddParagraph(paragraph); doc.SaveAs("output.docx");Imports IronWord Imports IronWord.Models ' Create document Dim doc As New WordDocument() ' Create styled Run Dim textRun As New Run(New TextContent("Hello IronWord!")) textRun.Style = New TextStyle() With {.IsBold = True, .Color = Color.Blue} ' Create paragraph and add Run Dim paragraph As New Paragraph() paragraph.AddChild(textRun) paragraph.AddImage(New ImageContent("pic.png")) ' Add to document and save doc.AddParagraph(paragraph) doc.SaveAs("output.docx")Imports IronWord Imports IronWord.Models ' Create document Dim doc As New WordDocument() ' Create styled Run Dim textRun As New Run(New TextContent("Hello IronWord!")) textRun.Style = New TextStyle() With {.IsBold = True, .Color = Color.Blue} ' Create paragraph and add Run Dim paragraph As New Paragraph() paragraph.AddChild(textRun) paragraph.AddImage(New ImageContent("pic.png")) ' Add to document and save doc.AddParagraph(paragraph) doc.SaveAs("output.docx") -
3Canlı ortamınızda test için dağıtım yapın
Projenizde IronWord'yi bugün ücretsiz deneme sürümü ile kullanmaya başlayın
İçindekiler
- Metin Ekle
- Metin İçeriği (Ekle, Ekleyin & Böl)
- Stil Ayarla (Yazı Tipi Ailesi & Boyutu, Renk, Kalın & İtalik, Üstü Çizili, Altı Çizili, Üstsimge & Altsimge)
- Görselleri Göm
- Resim Ekle
- Görsel Yükle (Dosya ve FileStream)
- Metin Sarma Ayarla
- Boyutları Ayarla (Genişlik & Yükseklik)
- Pozisyon Sonrası Ayarla
- Köşelerden Mesafe Ayarla
Anahtar Kavramlar
Belge Hiyerarşisi
IronWord, yapılandırılmış bir belge hiyerarşisini takip eder: Document → Paragraph → Run → TextContent. Bu hiyerarşiyi anlamak, şekillendirilmiş metinle çalışma konusunda önemlidir:
WordDocument: Tüm belgeyi temsil eden üst seviye konteynerParagraph: Belge içindeki içerik bloğuRun:TextContentiçeren ve stil uygulanabilen bir sarmalayıcı nesneTextContent: Gerçek metin dizgesi
Paragraflara İçerik Ekleme
IronWord, paragraflara içerik eklemenin iki yolu sunar:
AddText(TextContent): Stilsiz düz metin için kullanılır.TextContentdirekt olarak paragrafa ekler.AddChild(Run): Stil verilmiş metin için kullanılır.Runnesnesi (TextContentsaran ve stil tutan) paragrafa ekler.
Yazı tipi boyutu, renk veya kalın formatlama gibi stil uygulamanız gerektiğinde, bir Run nesnesi oluşturun, TextStyle'ı Run'e atayın ve paragrafa eklemek için AddChild kullanın.
Metin Çalışmalarını Ekleyin
Metin İçeriği
Split yöntemi, belirtilen bir ayırıcıya göre metin çalışmasını daha küçük metin çalışmaları listesine bölmek için kullanılır. Bu, belgede metin bilgilerini organize etme ve üzerinde işlem yapma olanağı sağlar.
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");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")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")Stil Ayarla
Metin için stil ayarlamak, Run sarmalayıcı modelinin kullanılmasını gerektirir. Run içeren bir TextContent nesnesi oluşturun, ardından TextStyle'ı Run'e (TextContent'e değil) atayın. TextStyle, FontSize, renk, kalın, italik, üstü çizili, altı çizili, üstsimge ve altsimge gibi görsel özellikleri tanımlamanıza olanak tanır.
FontSize'ın TextStyle seviyesinde yapılandırıldığını, oysa FontFamily'ın TextFont özelliği içinde ayarlandığını unutmayın. Stil verildikten sonra, AddChild kullanarak Run'i bir paragrafa ekleyin. Bu, belge hiyerarşisini takip eder: Document → Paragraph → Run → TextContent.
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
FontSize = 72,
TextFont = new Font()
{
FontFamily = "Caveat"
},
Color = Color.Red,
IsBold = true,
IsItalic = true,
Underline = new Underline(),
Strike = StrikeValue.Strike,
};
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddChild(textRun);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
FontSize = 72,
TextFont = new Font()
{
FontFamily = "Caveat"
},
Color = Color.Red,
IsBold = true,
IsItalic = true,
Underline = new Underline(),
Strike = StrikeValue.Strike,
};
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddChild(textRun);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
.FontSize = 72,
.TextFont = New Font() With {
.FontFamily = "Caveat"
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddChild(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
.FontSize = 72,
.TextFont = New Font() With {
.FontFamily = "Caveat"
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddChild(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")Metin Doldurma Rengini Edinme
Stil ayarlamanın yanı sıra, IronWord mevcut belgelerden tutarlılığı korumak için RGBA renk değerini elde etmenizin bir yolunu sağlar.
using IronWord;
using IronWord.Models;
using System;
// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");
TextContent content = doc.Texts[0];
// This will show the R G B A of the themecolor
var filledColor = content.Color;
// Print the filled color variable to the console
Console.WriteLine(filledColor);using IronWord;
using IronWord.Models;
using System;
// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");
TextContent content = doc.Texts[0];
// This will show the R G B A of the themecolor
var filledColor = content.Color;
// Print the filled color variable to the console
Console.WriteLine(filledColor);Imports IronWord
Imports IronWord.Models
Imports System
' Open existing Word
Dim doc As New WordDocument("Accent1TextThemcolor.docx")
Dim content As TextContent = doc.Texts(0)
' This will show the R G B A of the themecolor
Dim filledColor = content.Color
' Print the filled color variable to the console
Console.WriteLine(filledColor)Imports IronWord
Imports IronWord.Models
Imports System
' Open existing Word
Dim doc As New WordDocument("Accent1TextThemcolor.docx")
Dim content As TextContent = doc.Texts(0)
' This will show the R G B A of the themecolor
Dim filledColor = content.Color
' Print the filled color variable to the console
Console.WriteLine(filledColor)Renk değerlerini almak için, belgeyi WordDocument kullanarak yükleyin, ardından Texts koleksiyonuna erişin ve TextContent nesnesini alın. Color özelliği TextContent metninin mevcut renginin RGBA değerini döndürerek, belgeniz boyunca tutarlı bir stil korumanızı sağlar.
Görselleri Gömme
Bu özellik, içeriğe resimler eklemenize olanak tanır, belgenin genel görsel çekiciliğini ve iletişim gücünü artırır.
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(para);
// Export docx
doc.SaveAs("save_document.docx");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(para);
// Export docx
doc.SaveAs("save_document.docx");Imports IronWord
Imports IronWord.Models
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim 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(para)
' Export docx
doc.SaveAs("save_document.docx")Imports IronWord
Imports IronWord.Models
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim 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(para)
' Export docx
doc.SaveAs("save_document.docx")Resim Ekle
Görsel Yükleme
Görselleri yüklemek kritik bir süreçtir. Bu, harici görüntü dosyalarını belgeye getirmeyi içerir. Görsellerin yüklenebilmesi, ilgili görsellerin dahil edilmesini kolaylaştırarak daha ilgi çekici ve bilgilendirici bir belge sağlar.
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");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")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")Görseli Yapılandırma
Ayarlanabilir ayarlarla görselleri optimize edin. Bu, boyutlar, pozisyon ve köşelerden mesafe gibi özelliklerin ayarlanmasını içerir. Uygun yapılandırma, görsellerin görsel olarak hoş ve bağlamsal olarak uygun şekilde görüntülenmesini sağlar.
TextWrapBehavior özelliği (tip ITextWrapBehavior) sunar, ancak bir değer oluşturmak için halka açık bir sınıf veya fabrika şu anda belgelendirilmemiştir - bu örnek, ürün ekibi onayı beklerken çıkarılmıştır.using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
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");using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
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
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position As 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")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position As 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")Sıkça Sorulan Sorular
IronWord, C# geliştirmede ne için kullanılır?
.NET C# geliştiricileri için tasarlanmış olan IronWord, uygulamalarında Word ve DOCX belgeleri oluşturma, okuma ve düzenleme konusunda yardımcı olan güçlü bir kütüphanedir.
IronWord kullanarak bir Word belgesine biçimlendirilmiş metin nasıl eklenir?
Biçimlendirilmiş metin eklemek için 'Run' nesnesi oluşturarak 'TextContent' sarmalanır, ardından 'TextStyle' uygulanır ve IronWord'de 'AddChild' yöntemi kullanılarak bir paragrafa eklenir.
IronWord'deki 'Run' nesnesinin amacı nedir?
IronWord'deki 'Run' nesnesi, metni stilize etmeye izin veren 'TextContent'i sarmalayan bir nesne olarak işlev görür, örneğin yazı tipi boyutu, rengi ve diğer özelliklerini ayarlamak gibi.
IronWord kullanarak bir belgeye görsel nasıl gömülür?
Bir görseli gömmek için, 'ImageContent' nesnesi oluşturulur ve 'AddImage' yöntemi kullanılarak bir paragrafa eklenir. Bu, belgelerinize görsel eklemenizi sağlar.
IronWord'de bir görselin yapılandırılması için hangi adımlar gereklidir?
Bir görselin yapılandırılması, boyutlar, pozisyon ve köşelerden uzaklık gibi özellikleri ayarlamayı içerir, böylece görselin belge içinde doğru bir şekilde görüntülenmesini sağlar.
IronWord, mevcut bir Word belgesinden renk değerlerini alabilir mi?
Evet, IronWord, 'TextContent'in 'Color' özelliği aracılığıyla mevcut belgelerden RGBA renk değerlerini alabilir, böylece stil tutarlılığını korur.
IronWord, bir metin çalıştırmasını hangi yöntemle bölüyor?
IronWord, bir metin çalıştırmasını belirtilen bir ayırıcıya dayalı olarak daha küçük çalıştırmalara bölmek için 'Split' yöntemini kullanır, bu da metin içeriğini daha iyi organize etme ve manipüle etme imkanı sağlar.
IronWord'de belge hiyerarşisini anlamak neden önemlidir?
IronWord kullanarak bir Word belgesi içinde içeriği etkili bir şekilde yönetmek ve stilize etmek için belge hiyerarşisini (Belge → Paragraf → Çalıştır → Metin İçeriği) anlamak önemlidir.
IronWord ile oluşturulmuş bir Word belgesi nasıl kaydedilir?
IronWord ile oluşturulmuş bir Word belgesi, belgeyi kaydetmek için 'SaveAs' yöntemi kullanılarak dosya adı ve yolu belirtilerek kaydedilebilir.
IronWord'deki 'AddParagraph' yönteminin işlevi nedir?
IronWord'deki 'AddParagraph' yöntemi, tam yapılandırılmış paragrafları, metin ve görsellerle birlikte bir Word belgesine eklemek için kullanılır.
Curtis Chau, Bilgisayar Bilimleri alanında Lisans Derecesine (Carleton Üniversitesi) sahip ve Node.js, TypeScript, JavaScript ve React konularında uzmanlaşmış ön uç geliştirmeyle ilgileniyor. Sezgisel ve estetik açıdan hoş kullanıcı arayüzleri oluşturma tutkunu, Curtis modern çerçevelerle çalışmayı ve iyi yapılandırılmış, görsel olarak çekici kılavuzlar oluşturmayı seviyor.