यह ट्यूटोरियल आपको चरण-दर-चरण मार्गदर्शन करेगा कि C# का उपयोग करके वर्ड डॉक्यूमेंट में इमेज कैसे जोड़ें। हम वर्ड डॉक्यूमेंट में C# ऐड इमेज में कमांड-लाइन एप्लिकेशन का उपयोग करेंगे।
सी # का उपयोग कर वर्ड दस्तावेज़ में छवि जोड़ने के लिए कदम
- सिस्टम में संदर्भ जोड़ें। समाधान में असेंबली ड्राइंग
- इसके बाद, Aspose.Words for .NET NuGet पैकेज संदर्भ जोड़ने की जरूरत है
- Aspose.Words और Aspose.Words.Drawing namespaces के लिए निर्देशों का उपयोग करके जोड़ें
- कॉल लाइसेंस।सेट लाइसेंस विधि
- फ़ाइल सिस्टम या मेमोरी स्ट्रीम से Word DOC लोड करने के लिए दस्तावेज़ ऑब्जेक्ट बनाएँ
- टेक्स्ट, इमेज, टेबल आदि लिखने के लिए DocumentBuilder क्लास ऑब्जेक्ट बनाएं।
- Word DOC में कर्सर को शीर्षलेख या पाद लेख या किसी वांछित स्थिति में ले जाएँ
- स्ट्रीम या फ़ाइल से छवि जोड़ने के लिए DocumentBuilder.InsertImage का उपयोग करें
- छवि के आकार, स्थिति, भरण आदि को और अधिक सेट करने के लिए Shape class का उपयोग करें
- दस्तावेज़ को कॉल करें। Word DOC को डिस्क या स्ट्रीम में सहेजने के लिए विधि सहेजें
C# का उपयोग करके वर्ड डॉक्यूमेंट में इमेज जोड़ने के लिए आप .NET एप्लिकेशन में निम्न कोड उदाहरण का उपयोग कर सकते हैं।
सी # का उपयोग कर वर्ड दस्तावेज़ में छवि जोड़ने के लिए कोड
using Aspose.Words; | |
using Aspose.Words.Drawing; | |
namespace HowtoAddImageinWordDocumentUsingCsharp | |
{ | |
class AddImageToWordDOC | |
{ | |
static void Main(string[] args) | |
{ | |
// Set license prior to adding image in Word document using C# | |
License setupPriorAddingImages = new License(); | |
setupPriorAddingImages.SetLicense("path to license.lic"); | |
// Load Word DOC document that you want to add images to | |
Document AddImagesToWordDOC = new Document("input.doc"); | |
// Instantiate DocumentBuilder class object to write text, images, tables etc. | |
DocumentBuilder imageWriter = new DocumentBuilder(AddImagesToWordDOC); | |
// Move cursor to Primary Header in Word DOC | |
imageWriter.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); | |
// Insert image in word document header c# | |
Shape headerImage = imageWriter.InsertImage("C:\\Add Image in Word Header.jpg"); | |
// Set Image Size in Header | |
headerImage.Width = 1 * 72; // equals to one inch | |
headerImage.Height = 1 * 72; | |
// Now, move cursor to last Paragraph in Word Document | |
imageWriter.MoveTo(AddImagesToWordDOC.LastSection.Body.LastParagraph); | |
// Add Image to Word Document and Link to File | |
Shape imageAsLinkToFile = imageWriter.InsertImage("C:\\Add Image as Link to File.jpg"); | |
imageAsLinkToFile.ImageData.SourceFullName = "C:\\Add Image as Link to File.jpg"; | |
// Save As DOCX | |
AddImagesToWordDOC.Save("C:\\Word with Embeded and Linked Images.docx"); | |
} | |
} | |
} |
तो, उपरोक्त विजुअल स्टूडियो एप्लिकेशन आपको शब्द दस्तावेज़ सी # में छवि जोड़ने की अनुमति देगा। यह एक मौजूदा DOC फ़ाइल को लोड करता है लेकिन आप प्रोग्रामेटिक रूप से सी # में शब्द दस्तावेज़ बनाएं भी कर सकते हैं। कोड डीओसी सी # शब्द में छवि जोड़ने के दो तरीके प्रस्तुत करता है - यह पहले शब्द दस्तावेज़ शीर्षलेख सी # में छवि सम्मिलित करता है और फिर यह छवि को लिंक की गई छवि के रूप में जोड़ता है यानी इस मामले में छवि एम्बेडेड नहीं है बल्कि फ़ाइल के लिंक के रूप में डाली गई है।