Bu eğitimde, C# kullanarak Word belgesi oluşturmayı, farklı yazı tipi biçimlendirmesiyle metin yazmayı, içine bir köprü eklemeyi ve belgeyi DOCX dosya biçimine kaydetmeyi öğreneceğiz.
Aşağıdaki adımlar C#‘ın Word belge dosyasını nasıl oluşturduğunu açıklar.
C# Kullanarak Word Belgesi Oluşturma Adımları
- NuGet’ten Aspose.Words for .NET paketini yükleyin
- Aspose.Words ve Aspose.Words.Saving ad alanlarını referans ekleyin
- Document ve DocumentBuilder sınıflarının örneğini oluşturun
- Biraz metin yazın ve belgeye köprü ekleyin
- Belgeyi DOCX dosya biçimine kaydedin
Daha önce C# kullanarak DOCX’te Üstbilgi ve Altbilgi Nasıl Eklenir konusuna baktık. C# içindeki aşağıdaki kod örneği, iyi bilinen DOCX dosya biçiminde Word belgesi oluşturur. Aynı yaklaşımı, Document.Save yöntemini kullanarak DOC, RTF vb. gibi farklı dosya formatlarında Word belgesi oluşturmak için kullanabilirsiniz. Bu kod örneği, .NET’in kurulu olduğu yerlerde kullanılabilir.
C# Kullanarak Word Belgesi Oluşturma Kodu
using System.Drawing; | |
using Aspose.Words; | |
using Aspose.Words.Saving; | |
namespace KBCodeExamples | |
{ | |
class how_to_create_word_document_using_c_sharp | |
{ | |
public static void Main(string[] args) | |
{ | |
//Set Aspose license before creating blank Word document | |
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License(); | |
AsposeWordsLicense.SetLicense(@"Aspose.Words.lic"); | |
// Create a blank Word document | |
Document doc = new Document(); | |
// Initialize a new instance of DocumentBuilder class | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert a string surrounded by a border | |
builder.Font.Border.Color = Color.Green; | |
builder.Font.Border.LineWidth = 2.5d; | |
builder.Font.Border.LineStyle = LineStyle.DashDotStroker; | |
builder.Write("Text surrounded by green border."); | |
// Remove all font formatting specified explicitly | |
builder.Font.ClearFormatting(); | |
builder.InsertBreak(BreakType.ParagraphBreak); | |
builder.Write("For more information, please visit the "); | |
// Insert a hyperlink and emphasize it with custom formatting | |
// The hyperlink will be a clickable piece of text which will take us to the location specified in the URL | |
builder.Font.Color = Color.Blue; | |
builder.Font.Underline = Underline.Single; | |
builder.InsertHyperlink("Aspose Knowledge Base", "https://kb.aspose.com/", false); | |
builder.Font.ClearFormatting(); | |
builder.Writeln("."); | |
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions | |
{ | |
Compliance = OoxmlCompliance.Iso29500_2008_Strict, | |
SaveFormat = SaveFormat.Docx | |
}; | |
// Save the document with strict compliance level | |
doc.Save("create word document using C#.docx", saveOptions); | |
} | |
} | |
} |
*c# içindeki yukarıdaki kod örneği, sıfırdan Word belgesi oluşturur. Belge sınıfı, boş bir Word belgesini temsil eder. DocumentBuilder’ı Document ile ilişkilendirmeniz gerekir. Word belgesine tablo, resim, metin vb. gibi farklı içerik türleri eklemek için DocumentBuilder’ı kullanabilirsiniz.