Dalam tutorial ini, kita akan belajar cara membuat dokumen Word menggunakan C#, menulis beberapa teks dengan format font yang berbeda, menyisipkan hyperlink ke dalamnya, dan menyimpan dokumen ke format file DOCX.
Langkah-langkah berikut menjelaskan bagaimana C# membuat file dokumen Word.
Langkah-langkah Membuat Dokumen Word Menggunakan C#
- Instal paket Aspose.Words for .NET dari NuGet
- Tambahkan referensi Aspose.Words dan Aspose.Words.Menyimpan ruang nama
- Buat instance kelas Document dan DocumentBuilder
- Tulis beberapa teks dan masukkan hyperlink ke dalam dokumen
- Simpan dokumen ke format file DOCX
Sebelumnya, kami melihat Cara Menyisipkan Header dan Footer di DOCX menggunakan C#. Contoh kode berikut dalam C# menghasilkan dokumen Word dalam format file DOCX yang terkenal. Anda dapat menggunakan pendekatan yang sama untuk membuat dokumen Word dalam format file yang berbeda misalnya DOC, RTF, dll menggunakan metode Document.Save. Contoh kode ini dapat digunakan di mana .NET diinstal.
Kode untuk Menghasilkan Dokumen Word Menggunakan C#
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); | |
} | |
} | |
} |
Contoh kode di atas dalam c# membuat dokumen Word dari awal. Kelas dokumen mewakili dokumen Word kosong. Anda perlu mengaitkan DocumentBuilder dengan Document. Anda dapat menggunakan DocumentBuilder untuk menyisipkan berbagai jenis konten ke dalam dokumen Word misalnya tabel, gambar, teks, dll.