在word中指定位置插入圖像
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。在 C#、VB.NET 中從 Word 中提取圖像。
圖像可以為您的 Word 文檔增添趣味并生效。假設你寫了一篇介紹一個城市的文章,介紹它美麗的風景。僅僅用文字描述,你無法為你的讀者呈現完美的風景,因為那一頁的文字看起來模糊而沉悶。你需要圖像來點綴你的風景。
Spire.Doc for .NET是一個專業的.NET word 組件,無需使用 MS Office 自動化即可快速生成、打開、修改和保存 Word 文檔,使用戶可以使用 C#.NET 將圖像插入 Word 文檔并根據頁面設置其大小. 本指南介紹了如何通過Spire.Doc for .NET插入圖像的簡單方法。
首先,新建 Word 文檔并為該文檔添加節、段。然后,在新創建的段落中插入圖像。您可以設置此圖像的大小、位置、旋轉它并選擇文本的環繞樣式。下載并安裝 Spire.Doc for .NET。使用以下代碼通過 C# 在 Word 中插入圖像。
示例演示展示了如何將圖像插入到文檔中。
using System; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace Insert_image_to_Word_Document { class Program { static void Main(string[] args) { //Open a blank word document as template Document document = new Document(@"Blank.doc"); Section section = document.Sections[0]; Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph(); paragraph.AppendText("The sample demonstrates how to insert an image into a document."); paragraph.ApplyStyle(BuiltinStyle.Heading2); paragraph = section.AddParagraph(); //get original image Bitmap p = new Bitmap(Image.FromFile(@"Word.jpg")); //rotate image and insert image to word document p.RotateFlip(RotateFlipType.Rotate90FlipX); DocPicture picture = document.Sections[0].Paragraphs[0].AppendPicture(p); //set image's position picture.HorizontalPosition = 50.0F; picture.VerticalPosition = 60.0F; //set image's size picture.Width = 200; picture.Height = 200; //set textWrappingStyle with image; picture.TextWrappingStyle = TextWrappingStyle.Through; //Save doc file. document.SaveToFile("Sample.doc", FileFormat.Doc); //Launching the MS Word file. System.Diagnostics.Process.Start("Sample.doc"); } } }
如果您已完成教程Spire.Doc 快速入門,上述步驟會更簡單。
使用Spire.Doc,您可以將圖像插入 Word 文檔,并在 ASP.NET、WPF 和 Silverlight 應用程序中執行更多相同操作,而無需 Word 自動化和任何其他第三方插件。
以上便是如何在 C#/VB.NET 中的指定位置插入圖像,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。