Ինչպես ստեղծել Word փաստաթուղթ C#-ով

Այս ձեռնարկում մենք կսովորենք ինչպես ստեղծել Word փաստաթուղթ C#-ի միջոցով, գրել տառատեսակի տարբեր ձևաչափով տեքստ, տեղադրել դրա մեջ հիպերհղում և պահպանել փաստաթուղթը DOCX ֆայլի ձևաչափով:

Հետևյալ քայլերը բացատրում են, թե ինչպես է C#-ը ստեղծում Word փաստաթղթի ֆայլ:

Word փաստաթուղթ ստեղծելու քայլեր C#-ով

  1. Տեղադրեք Aspose.Words for .NET փաթեթը NuGet-ից
  2. Ավելացրեք հղում Aspose.Words և Aspose.Words.Saving անվանումների տարածքները
  3. Ստեղծեք Document և DocumentBuilder դասերի օրինակ
  4. Գրեք որոշ տեքստ և տեղադրեք հիպերհղում փաստաթղթում
  5. Փաստաթուղթը պահեք DOCX ֆայլի ձևաչափով

Նախկինում մենք ուսումնասիրել ենք Ինչպես տեղադրել վերնագիր և ստորագիր DOCX-ում՝ օգտագործելով C#-ը: Հաջորդ կոդի օրինակը C#-ում առաջացնում է Word փաստաթուղթ հայտնի DOCX ֆայլի ձևաչափով: Դուք կարող եք օգտագործել նույն մոտեցումը Word փաստաթուղթ ստեղծելու համար տարբեր ֆայլերի ձևաչափերով, օրինակ՝ DOC, RTF և այլն՝ օգտագործելով Document.Save մեթոդը: Այս կոդի օրինակը կարող է օգտագործվել այնտեղ, որտեղ տեղադրված է .NET:

Կոդ՝ C#-ով Word փաստաթուղթ ստեղծելու համար

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#-ում զրոյից ստեղծեք Word փաստաթուղթ: Փաստաթղթերի դասը ներկայացնում է դատարկ Word փաստաթուղթ: Դուք պետք է միացնեք DocumentBuilder-ը Document-ի հետ: Դուք կարող եք օգտագործել DocumentBuilder-ը Word փաստաթղթում տարբեր տեսակի բովանդակություն մտցնելու համար, օրինակ՝ աղյուսակ, պատկերներ, տեքստ և այլն:

 Հայերեն