翻譯|使用教程|編輯:胡濤|2023-03-08 09:37:16.903|閱讀 187 次
概述:本文將向您展示如何借助 Spire.Doc 在 C# 中的書簽位置插入圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Word書簽廣泛用于指出指定位置或給出段落的簡要信息。如果在書簽位置添加圖片,書簽會更加明顯和清晰。本文將向您展示如何借助 Spire.Doc 在 C# 中的書簽位置插入圖像。
Spire.Doc 提供了一個 BookmarksNavigator 實例來查找書簽,然后開發人員使用 AppendPicture 添加圖像。下面進入步驟:
第 1 步:加載帶有書簽的 word 文檔。
Document document = new Document(); document.LoadFromFile("Test.docx");
第 2 步:創建 BookmarksNavigator 的實例并找到要插入圖像的書簽。
//Create an instance of BookmarksNavigator BookmarksNavigator bn = new BookmarksNavigator(document); //Find a bookmark and its name is Spire bn.MoveToBookmark("Spire", true, true);
第 3 步:在找到的書簽位置插入圖片。
//Add a section and named it section0 Section section0 = document.AddSection(); //Add a paragraph for section0 Paragraph paragraph = section0.AddParagraph(); Image image = Image.FromFile("step.png"); //Add a picture into paragraph DocPicture picture = paragraph.AppendPicture(image); //Add a paragraph with picture at the position of bookmark bn.InsertParagraph(paragraph); document.Sections.Remove(section0);
第 4 步:保存新文檔并進行處理。
string output = "sample3.docx"; document.SaveToFile(output, FileFormat.Docx); System.Diagnostics.Process.Start(output);
Spire.Doc 還提供了以下屬性,可以根據開發者的需求設置圖片位置。
picture.TextWrappingStyle picture.HorizontalAlignment picture.HorizontalOrigin picture.HorizontalPosition picture.VerticalAlignment picture.VerticalOrigin picture.VerticalPosition
有效截圖:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertImage { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("Test.docx"); BookmarksNavigator bn = new BookmarksNavigator(document); bn.MoveToBookmark("Spire", true, true); Section section0 = document.AddSection(); Paragraph paragraph = section0.AddParagraph(); Image image = Image.FromFile("step.png"); DocPicture picture = paragraph.AppendPicture(image); bn.InsertParagraph(paragraph); document.Sections.Remove(section0); string output = "sample.docx"; document.SaveToFile(output, FileFormat.Docx); System.Diagnostics.Process.Start(output); } } }
以上便是如何在 DOCX 到 PDF 轉換中保留書簽,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn