C#'da Biçimlendirmeli Word Belgesi Nasil Okunur
Microsoft Word belgeleri, resmi iş raporlarından kişisel mektuplara kadar çeşitli amaçlar için yaygın olarak kullanılmaktadır. C#'da, geliştiriciler sıklıkla Microsoft Word belgelerini programlı olarak oluşturmalıdır. Windows uygulama geliştiricileri tarihten beri C# kullanarak Word belgeleri oluşturmak için Microsoft Office Interop'u kullanmışlardır.
Bununla birlikte, bu yaklaşım herkes için erişilebilir değildir ve Microsoft Office arayüzünün mevcut olmadığı bir işletim sistemi veya Linux makinesinde olan geliştiriciler de bulunmaktadır. Bu durumlarda, farklı platformlarda bağımsız olarak çalışabilen diğer kütüphaneleri araştırmak gereklidir. Bir C# kütüphanesi olan, IronWord, Iron Software'dan bu tür güçlü bir kütüphanedir.
IronWord, .NET uygulamalarında Word belgeleriyle çalışma için sağlam işlevsellik sağlar ve farklı platformlar ve Linux tabanlı docker görüntüleri/kapsayıcıları üzerinde çalışabilir. IronWord ile, C#, VB.NET Word ve Docx Belge API'sine sahip olan IronWord ile, Word belge dosyalarını oluşturmak, düzenlemek ve dışa aktarmak için Microsoft Office, Office otomasyonu veya Word Interop yüklemeye gerek yoktur. IronWord, .NET 8, 7, 6, Framework, Core ve Azure'u tam destekler.
Bu makale, IronWord kütüphanesini kullanarak C#'da Word belgeleri oluşturmayı keşfedecek.
How to Create Word Documents Without Office Interop in C
- Yeni bir C# projesi oluşturun.
- IronWord kütüphanesini yükleyin.
- IronWord kütüphanesini kullanarak bir Word belgesi oluşturun.
- Artık var olan belgeye içerik ekleyin.
- Oluşturulan Word belgesini kaydedin.
- Oluşturulan Word belgesini açın ve gösterin.
Ön koşullar:
- Visual Studio: Visual Studio veya başka bir C# geliştirme ortamı yüklendiğinden emin olun.
- NuGet Paket Yöneticisi: Projenizde paketleri yönetmek için NuGet kullanabileceğinizden emin olun.
Adım 1: Yeni C# Projesini Oluştur
Kelimenizi oluşturmak istediğiniz yeni bir C# konsol uygulaması oluşturun veya mevcut bir projeyi kullanın.
Konsol uygulama şablonunu seçin ve devam etmek için tıklayın.

Bir sonraki adımda, çözüm ve proje adlarını belirtebilirsiniz.

.NET Sürümünü seçin ve "Oluştur"a tıklayın.

Adım 2: IronWord Kütüphanesini Yükleyin
C# projenizi açın ve NuGet Paket Yöneticisi Konsolunu kullanarak IronWord kütüphanesini yükleyin:
Install-Package IronWord -Version 2024.1.2
NuGet paketi, aşağıda gösterildiği gibi, Visual Studio Paket Yöneticisi kullanılarak da yüklenebilir.

Adım 3: IronWord Kütüphanesini Kullanarak bir Word Belgesi Oluşturun ve Kaydedin
IronWord kütüphanesini kullanarak bir Word belgesi oluşturmaya dair basit bir örnekle başlayalım. Aşağıdaki kod, "Hello, World!" metnini içeren bir paragrafla temel bir Word belgesinin nasıl oluşturulacağını gösterir.
using IronWord;
using IronWord.Models;
// Create a text run instance with "Hello, World!" content
TextRun textRun = new TextRun("Hello, World!");
// Create a paragraph and add the text run to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Save the document as a .docx file
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
using IronWord;
using IronWord.Models;
// Create a text run instance with "Hello, World!" content
TextRun textRun = new TextRun("Hello, World!");
// Create a paragraph and add the text run to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Save the document as a .docx file
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
Imports IronWord
Imports IronWord.Models
' Create a text run instance with "Hello, World!" content
Private textRun As New TextRun("Hello, World!")
' Create a paragraph and add the text run to it
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a new Word document object with the paragraph
Dim doc As New WordDocument(paragraph)
' Save the document as a .docx file
doc.SaveAs("example.docx") ' Saves the file to disk with the name example.docx
Yukarıdaki kod örneği çalıştırıldıktan sonra, yeni belge dosyası example.docx oluşturulur ve çıktı aşağıda gösterildiği gibidir.

Bu, IronWord kullanarak bir Word belge dosyası üretmenin temel bir örneğidir. Daha fazla bilgi için, Dokümantasyon'u okuyabilirsiniz.
Bir Word Belgesine Stil ile Paragraflar Ekleme
Artık IronWord kullanarak nasıl basit bir Word belgesi oluşturulacağını bildiğimize göre, paragraflar ve stilize edilmiş metin ekleyelim.
TextRun ayrıca stil verileri alabilir ve metnin görsel temsilini geliştirir. Metin, üstü çizgili, kalın, italik ve altı çizili gibi stil ile ayarlanabilir.
Önceden yaptığınız programa aşağıdaki kodu ekleyin ve değiştirin.
using IronWord;
using IronWord.Models;
// Create text runs with different styles
TextRun textRun = new TextRun("Hello, World!"); // Simple string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure and add italic text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle);
// Configure and add bold text
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle);
// Add text runs to the paragraph
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create and save the new Word document
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
using IronWord;
using IronWord.Models;
// Create text runs with different styles
TextRun textRun = new TextRun("Hello, World!"); // Simple string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure and add italic text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle);
// Configure and add bold text
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle);
// Add text runs to the paragraph
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create and save the new Word document
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("example.docx"); // Saves the file to disk with the name example.docx
Imports IronWord
Imports IronWord.Models
' Create text runs with different styles
Private textRun As New TextRun("Hello, World!") ' Simple string
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Configure and add italic text
Dim introText As New TextRun("This is an example paragraph with italic and bold styling.")
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New TextRun("Italic example sentence.", italicStyle)
' Configure and add bold text
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New TextRun("Bold example sentence.", boldStyle)
' Add text runs to the paragraph
paragraph.AddTextRun(introText)
paragraph.AddTextRun(italicText)
paragraph.AddTextRun(boldText)
' Create and save the new Word document
Dim doc As New WordDocument(paragraph)
doc.SaveAs("example.docx") ' Saves the file to disk with the name example.docx
Bir Word Belgesine Tablo Ekleme
Verilerin net temsili için, bir ızgara içinde de temsil edilebilir. Veriler, bir ızgara şeklinde düzenlendiğinde, buna tablo denir. IronWord kullanarak, aşağıda gösterildiği gibi Word belgesine tablolar ve resimler ekleyebiliriz:
using IronWord;
using IronWord.Models;
// Create a table cell with sample text
TableCell cell = new TableCell();
TextRun textRun = new TextRun("Sample text");
// Add text run to the cell as a paragraph
cell.AddContent(new Paragraph(textRun));
// Configure cell border style
BorderStyle borderStyle = new BorderStyle
{
BorderColor = new IronColor(IronSoftware.Drawing.Color.Black),
BorderValue = IronWord.Models.Enums.BorderValues.Thick,
BorderSize = 5
};
// Set table borders
TableBorders tableBorders = new TableBorders
{
TopBorder = borderStyle,
RightBorder = borderStyle,
BottomBorder = borderStyle,
LeftBorder = borderStyle
};
cell.Borders = tableBorders;
// Create a table row and add cells to it
TableRow row = new TableRow();
row.AddCell(cell); // Add the first cell
row.AddCell(cell); // Add the second cell, duplicating to mimic a row with two identical cells
// Create a table and add the row to the table
Table table = new Table();
table.AddRow(row);
// Create and save a Word document using the table
WordDocument doc = new WordDocument(table);
doc.SaveAs("Document.docx"); // Saves the file to disk with the name Document.docx
using IronWord;
using IronWord.Models;
// Create a table cell with sample text
TableCell cell = new TableCell();
TextRun textRun = new TextRun("Sample text");
// Add text run to the cell as a paragraph
cell.AddContent(new Paragraph(textRun));
// Configure cell border style
BorderStyle borderStyle = new BorderStyle
{
BorderColor = new IronColor(IronSoftware.Drawing.Color.Black),
BorderValue = IronWord.Models.Enums.BorderValues.Thick,
BorderSize = 5
};
// Set table borders
TableBorders tableBorders = new TableBorders
{
TopBorder = borderStyle,
RightBorder = borderStyle,
BottomBorder = borderStyle,
LeftBorder = borderStyle
};
cell.Borders = tableBorders;
// Create a table row and add cells to it
TableRow row = new TableRow();
row.AddCell(cell); // Add the first cell
row.AddCell(cell); // Add the second cell, duplicating to mimic a row with two identical cells
// Create a table and add the row to the table
Table table = new Table();
table.AddRow(row);
// Create and save a Word document using the table
WordDocument doc = new WordDocument(table);
doc.SaveAs("Document.docx"); // Saves the file to disk with the name Document.docx
Imports IronWord
Imports IronWord.Models
' Create a table cell with sample text
Private cell As New TableCell()
Private textRun As New TextRun("Sample text")
' Add text run to the cell as a paragraph
cell.AddContent(New Paragraph(textRun))
' Configure cell border style
Dim borderStyle As New BorderStyle With {
.BorderColor = New IronColor(IronSoftware.Drawing.Color.Black),
.BorderValue = IronWord.Models.Enums.BorderValues.Thick,
.BorderSize = 5
}
' Set table borders
Dim tableBorders As New TableBorders With {
.TopBorder = borderStyle,
.RightBorder = borderStyle,
.BottomBorder = borderStyle,
.LeftBorder = borderStyle
}
cell.Borders = tableBorders
' Create a table row and add cells to it
Dim row As New TableRow()
row.AddCell(cell) ' Add the first cell
row.AddCell(cell) ' Add the second cell, duplicating to mimic a row with two identical cells
' Create a table and add the row to the table
Dim table As New Table()
table.AddRow(row)
' Create and save a Word document using the table
Dim doc As New WordDocument(table)
doc.SaveAs("Document.docx") ' Saves the file to disk with the name Document.docx
Yukarıdaki örnekte, kenarlıklarla bir Word belgesine bir tablo eklediğimizi gösterdik.
Bir Word Belgesine Resim Ekleme
Resimler, belgenin sunumunu geliştirir ve daha fazla renk ve görsel çekicilik ekleyebilir.
Aşağıdaki kodda gösterildiği gibi, IronWord kullanarak programlı olarak bir Word belgesine resimler eklenebilir:
using IronWord;
using IronWord.Models;
// Load a new document
WordDocument doc = new WordDocument();
// Configure and add image to the document
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg")
{
Width = 200, // Set image width in pixels
Height = 200 // Set image height in pixels
};
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image); // Add image to paragraph
doc.AddParagraph(paragraph); // Add paragraph to the document
// Save the document as a .docx file
doc.SaveAs("save_document.docx"); // Saves the file to disk with the name save_document.docx
using IronWord;
using IronWord.Models;
// Load a new document
WordDocument doc = new WordDocument();
// Configure and add image to the document
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg")
{
Width = 200, // Set image width in pixels
Height = 200 // Set image height in pixels
};
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image); // Add image to paragraph
doc.AddParagraph(paragraph); // Add paragraph to the document
// Save the document as a .docx file
doc.SaveAs("save_document.docx"); // Saves the file to disk with the name save_document.docx
Imports IronWord
Imports IronWord.Models
' Load a new document
Private doc As New WordDocument()
' Configure and add image to the document
Private image As New IronWord.Models.Image("SalesChart.jpg") With {
.Width = 200,
.Height = 200
}
Private paragraph As New Paragraph()
paragraph.AddImage(image) ' Add image to paragraph
doc.AddParagraph(paragraph) ' Add paragraph to the document
' Save the document as a .docx file
doc.SaveAs("save_document.docx") ' Saves the file to disk with the name save_document.docx
Burada, bir paragraf içine IronWord.Models.Image kullanarak 200 piksel Yükseklik ve Genişlik ile bir resim ekliyoruz.
Lisanslama (Ücretsiz Deneme Mevcuttur)
IronWord lisans gerektirir. Iron Software web sitesinden bir deneme anahtarı edinin. Bu anahtar, appsettings.json içine yerleştirilmelidir.
{
"IronWord.LicenseKey": "IRONWORD.MYLICENSE.KEY.TRIAL"
}
Deneme lisansı almak için e-posta adresinizi sağlayın. E-posta kimliğinizi gönderdikten sonra, anahtar e-posta yoluyla teslim edilecektir.

Sonuç
C# kullanarak IronWord kütüphanesi ile Word belgeleri oluşturmak, Microsoft Office'e bağlı kalmadan belge oluşturma için esnek ve etkili bir yol sağlar. İster basit mektuplar ister tablolar ve resimlerle karmaşık raporlar oluşturmanız gereksin, IronWord bunu programlı olarak gerçekleştirmenizi sağlar. Bu makale, Word belgeleri oluşturma konusunda kapsamlı bir öğretici sunmaktadır. IronWord ile, Word belgelerinin oluşturulmasını otomatikleştirme gücüne sahipsiniz, bu da C# uygulamalarınızı daha çok yönlü ve üretken hale getirir.
Ve oluşturulan Word belgeleriyle birlikte çalışacak PDF dosyası manipülasyonu arayan geliştiriciler için, Iron Software tarafından üretilmiş başka bir C# kitaplığı olan IronPDF'a daha fazla bakmayın.
Sıkça Sorulan Sorular
Microsoft Office Interop kullanmadan C# içinde Word belgelerini nasıl oluşturabilirim?
IronWORD kütüphanesini kullanarak C# içinde Microsoft Office Interop kullanmadan Word belgeleri oluşturabilirsiniz. Bu kütüphane, Word belgelerini programatik olarak oluşturmanıza, düzenlemenize ve kaydetmenize olanak tanır.
IronWORD kullanmanın Microsoft Office Interop'a göre avantajları nelerdir?
IronWORD, Microsoft Office Interop'un mevcut olmadığı Linux gibi Windows dışı platformlarda çalışabilme esnekliği sağlar. Ayrıca, Microsoft Office kurulu olma bağımlılığını ortadan kaldırarak geliştirme sürecini kolaylaştırır.
IronWORD kullanarak bir Word belgesi oluşturmaya nasıl başlayabilirim?
IronWORD kullanarak bir Word belgesi oluşturmaya başlamak için, ilk olarak C# projenizdeki NuGet Paket Yöneticisi aracılığıyla IronWORD kütüphanesini yükleyin. Daha sonra IronWORD API'sini kullanarak programatik olarak Word belgeleri oluşturun ve manipüle edin.
IronWORD'u en son .NET sürümleriyle kullanabilir miyim?
Evet, IronWORD çeşitli .NET sürümleriyle uyumludur, .NET 8, 7, 6, Framework, Core ve Azure dahil, modern C# uygulamalarınıza entegre etmenizi sağlar.
IronWORD ile bir Word belgesine stilize metin nasıl ekleyebilirim?
IronWORD, TextRun ve TextStyle sınıflarını kullanarak stilize metin eklemenize olanak tanır. Bu sınıflar, belge içindeki belirli metin segmentlerine kalın, italik ve altı çizili gibi stiller uygulamanızı sağlar.
IronWORD kullanarak Word belgelerine tablolar eklemek mümkün mü?
Evet, IronWORD kullanarak Word belgelerine tablolar ekleyebilirsiniz. Kütüphane, satır ve hücrelerle tablolar tanımlamanıza olanak tanır ve bunların görünümünü kenarlıklar ve diğer stillerle özelleştirebilirsiniz.
IronWORD kullanarak bir Word belgesine resimler nasıl eklerim?
IronWORD kullanarak bir Word belgesine resimler eklemek için IronWord.Models.Image sınıfını kullanarak, belirli boyutlarla paragrafların içine resimler gömerek belgenizin görsel içeriğini artırabilirsiniz.
IronWORD için bir deneme sürümü mevcut mu?
Evet, Iron Software, IronWORD'un ücretsiz bir deneme sürümünü sunar, web sitesinden indirebilirsiniz. Bu deneme sürümü, özelliklerini ve satın alma öncesi kapasitelerini değerlendirmenize olanak tanır.
Iron Software başka hangi belge manipülasyon kütüphanelerini sağlar?
IronWORD dışında, Iron Software IronPDF adlı bir PDF dosyalarını manipüle etme kütüphanesi sağlar. Bu kütüphaneler bir arada, C# uygulamalarında hem Word hem de PDF belgelerini işlemek için kapsamlı çözümler sunar.



