翻譯|使用教程|編輯:胡濤|2023-03-08 09:37:16.903|閱讀 187 次
概述:本文將向您展示如何借助 Spire.Doc 在 C# 中的書簽位置插入圖像。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫(kù)。在于幫助開發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Word書簽廣泛用于指出指定位置或給出段落的簡(jiǎn)要信息。如果在書簽位置添加圖片,書簽會(huì)更加明顯和清晰。本文將向您展示如何借助 Spire.Doc 在 C# 中的書簽位置插入圖像。
Spire.Doc 提供了一個(gè) BookmarksNavigator 實(shí)例來查找書簽,然后開發(fā)人員使用 AppendPicture 添加圖像。下面進(jìn)入步驟:
第 1 步:加載帶有書簽的 word 文檔。
Document document = new Document(); document.LoadFromFile("Test.docx");
第 2 步:創(chuàng)建 BookmarksNavigator 的實(shí)例并找到要插入圖像的書簽。
//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 步:保存新文檔并進(jìn)行處理。
string output = "sample3.docx"; document.SaveToFile(output, FileFormat.Docx); System.Diagnostics.Process.Start(output);
Spire.Doc 還提供了以下屬性,可以根據(jù)開發(fā)者的需求設(shè)置圖片位置。
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 轉(zhuǎn)換中保留書簽,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn