این آموزش گام به گام شما را در مورد نحوه افزودن تصویر به سند word با استفاده از سی شارپ راهنمایی می کند. ما از یک برنامه خط فرمان در سی شارپ استفاده خواهیم کرد که تصویر را به سند word اضافه می کنیم.
مراحل افزودن تصویر به سند Word با استفاده از سی شارپ
- ارجاع به مجموعه System.Drawing در محلول اضافه کنید
- سپس، Aspose.Words for .NET مرجع بسته NuGet باید اضافه شود
- افزودن با استفاده از دستورات برای فضاهای نام Aspose.Words و Aspose.Words.Drawing
- با روش License.SetLicense تماس بگیرید
- برای بارگیری Word DOC از سیستم فایل یا جریان حافظه، شی Document ایجاد کنید
- ایجاد شی کلاس DocumentBuilder برای نوشتن متن، تصاویر، جداول و غیره.
- مکان نما را به Header یا Footer یا هر موقعیت دلخواه در Word DOC منتقل کنید
- از DocumentBuilder.InsertImage برای افزودن تصویر از جریان یا فایل استفاده کنید
- از Shape class برای تنظیم بیشتر اندازه، موقعیت، پر کردن و غیره تصویر استفاده کنید
- برای ذخیره Word DOC در دیسک یا استریم، روش Document.Save را فراخوانی کنید
برای افزودن تصویر به سند word با استفاده از سی شارپ می توانید از مثال کد زیر در برنامه دات نت استفاده کنید.
کد برای افزودن تصویر در سند Word با استفاده از سی شارپ
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"); | |
} | |
} | |
} |
بنابراین، برنامه ویژوال استودیو فوق به شما امکان می دهد تصویر را به سند word سی شارپ اضافه کنید. یک فایل DOC موجود را بارگیری می کند، اما حتی می توانید به صورت برنامه نویسی ایجاد سند word در سی شارپ. کد دو روش برای افزودن تصویر به word DOC C# ارائه میکند - ابتدا تصویر را در هدر سند word C# وارد میکند و سپس تصویر را به عنوان تصویر پیوندی به کلمه اضافه میکند، یعنی تصویر در این حالت تعبیه نشده است بلکه به عنوان پیوند به فایل درج میشود.