翻譯|使用教程|編輯:胡濤|2023-05-04 10:15:45.190|閱讀 117 次
概述:本文介紹如何在spire.doc中,保存為 .docx 文件格式時(shí)將私有字體嵌入到 Word 文檔中,歡迎查閱
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專(zhuān)門(mén)對(duì) Word 文檔進(jìn)行操作的 .NET 類(lèi)庫(kù)。在于幫助開(kāi)發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專(zhuān)業(yè)開(kāi)發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開(kāi)發(fā)工具,專(zhuān)注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類(lèi)庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴(lài)第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
現(xiàn)在 Spire.Doc 支持在保存為 .docx 文件格式時(shí)將字體文件中的私有字體嵌入到 Word 文檔中。本文將向您展示如何使用 Spire.Doc 完成此任務(wù)的詳細(xì)步驟。
為了演示,我們使用了字體文件 DeeDeeFlowers.ttf。
在接下來(lái)的部分中,我們會(huì)將上述文件中的字體嵌入到 Word 文檔中,并使用它來(lái)創(chuàng)建文本。
第 1 步:創(chuàng)建一個(gè)空白 Word 文檔。
Document document = new Document();
第 2 步:在文檔中添加一個(gè)節(jié)和一個(gè)段落。
Section section = document.AddSection(); Paragraph p = section.AddParagraph();
第 3 步:在段落中添加文本,然后設(shè)置文本的字體名稱(chēng)和字體大小。
TextRange range = p.AppendText("Let life be beautiful like summer flowers\n" +"Life, thin and light-off time and time again\n" + "Frivolous tireless"); range.CharacterFormat.FontName = "DeeDeeFlowers"; range.CharacterFormat.FontSize = 20;
第 4 步:通過(guò)將 EmbedFontsInFile 屬性的布爾值設(shè)置為 true,允許在文檔中嵌入字體。
document.EmbedFontsInFile = true;
第 5 步:將字體文件中的私有字體嵌入到文檔中。
document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf"));
第 6 步:另存為 .docx 文件格式。
document.SaveToFile("result.docx", FileFormat.Docx);
運(yùn)行代碼后,我們將得到以下輸出:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Embed_private_font_into_Word { class Program { static void Main(string[] args) { Document document = new Document(); Section section = document.AddSection(); Paragraph p = section.AddParagraph(); TextRange range = p.AppendText("Let life be beautiful like summer flowers\n" +"Life, thin and light-off time and time again\n" + "Frivolous tireless"); range.CharacterFormat.FontName = "DeeDeeFlowers"; range.CharacterFormat.FontSize = 20; document.EmbedFontsInFile = true; document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf")); document.SaveToFile("result.docx", FileFormat.Docx); } } }
以上便如何保存為 .docx 文件格式時(shí)將私有字體嵌入到 Word 文檔中,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢(xún) ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn