इस ट्यूटोरियल में, हम सीखेंगे कि सी# का उपयोग करके वर्ड डॉक्यूमेंट कैसे बनाएं, अलग-अलग फॉन्ट फॉर्मेटिंग के साथ कुछ टेक्स्ट लिखें, उसमें हाइपरलिंक डालें, और डॉक्यूमेंट को DOCX फाइल फॉर्मेट में सेव करें।
निम्नलिखित चरण बताते हैं कि कैसे C# Word दस्तावेज़ फ़ाइल बनाता है।
C# का उपयोग करके Word दस्तावेज़ बनाने के चरण
- NuGet से Aspose.Words for .NET पैकेज इंस्टॉल करें
- संदर्भ जोड़ें Aspose.Words and Aspose.Words.Saving namespaces
- Document और DocumentBuilder कक्षाओं का उदाहरण बनाएं
- कुछ टेक्स्ट लिखें और दस्तावेज़ में हाइपरलिंक डालें
- दस्तावेज़ को DOCX फ़ाइल स्वरूप में सहेजें
पहले, हमने सी # का उपयोग कर DOCX में शीर्षलेख और पाद लेख कैसे सम्मिलित करें पर गौर किया था। जाने-माने DOCX फ़ाइल स्वरूप में C# जनरेट वर्ड दस्तावेज़ में निम्नलिखित कोड उदाहरण। आप अलग-अलग फ़ाइल स्वरूपों जैसे DOC, RTF, आदि में Word दस्तावेज़ बनाने के लिए Document.Save विधि का उपयोग करके एक ही दृष्टिकोण का उपयोग कर सकते हैं। इस कोड उदाहरण का उपयोग किया जा सकता है जहां .NET स्थापित है।
सी # का उपयोग कर वर्ड दस्तावेज़ जेनरेट करने के लिए कोड
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 का उपयोग कर सकते हैं।