翻譯|使用教程|編輯:李顯亮|2020-06-23 10:14:29.720|閱讀 502 次
概述:在Aspose.Words中,DocumentBuilder.InsertField方法用于在文檔中插入新字段。本文將介紹如何使用DocumentBuilder將字段插入文檔。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words for .NET是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
>>Aspose.Words for .NET已經(jīng)更新至v20.6,Font.EmphasisMark向公眾公開,引入了MarkdownSaveOptions類,PDF版本1.5標(biāo)記為過(guò)時(shí),點(diǎn)擊下載體驗(yàn)
在Aspose.Words中,DocumentBuilder.InsertField方法用于在文檔中插入新字段。第一個(gè)參數(shù)接受要插入的字段的完整字段代碼。第二個(gè)參數(shù)是可選的,它允許手動(dòng)設(shè)置字段的字段結(jié)果。如果未提供,則該字段將自動(dòng)更新。您可以將null或empty傳遞給此參數(shù)以插入具有空字段值的字段。如果不確定特定的域代碼語(yǔ)法,請(qǐng)首先在Microsoft Word中創(chuàng)建該域,然后切換以查看其域代碼。
下面的示例演示如何使用DocumentBuilder將合并字段插入文檔中。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithFields(); Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.InsertField(@"MERGEFIELD MyFieldName \* MERGEFORMAT"); dataDir = dataDir + "InsertField_out.docx"; doc.Save(dataDir);
下面的示例使用DocumentBuilder將合并字段插入文檔中。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert content with German locale. builder.Font.LocaleId = 1031; builder.InsertField("MERGEFIELD Date1 \\@ \"dddd, d MMMM yyyy\""); builder.Write(" - "); builder.InsertField("MERGEFIELD Date2 \\@ \"dddd, d MMMM yyyy\"");
使用相同的技術(shù)來(lái)插入嵌套在其他字段中的字段。 下面的示例演示如何使用DocumentBuilder插入嵌套在另一個(gè)字段中的字段。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithFields(); Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a few page breaks (just for testing) for (int i = 0; i < 5; i++) builder.InsertBreak(BreakType.PageBreak); // Move the DocumentBuilder cursor into the primary footer. builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary); // We want to insert a field like this: // { IF {PAGE} <> {NUMPAGES} "See Next Page" "Last Page" } Field field = builder.InsertField(@"IF "); builder.MoveTo(field.Separator); builder.InsertField("PAGE"); builder.Write(" <> "); builder.InsertField("NUMPAGES"); builder.Write(" \"See Next Page\" \"Last Page\" "); // Finally update the outer field to recalcaluate the final value. Doing this will automatically update // The inner fields at the same time. field.Update(); dataDir = dataDir + "InsertNestedFields_out.docx"; doc.Save(dataDir);
語(yǔ)言標(biāo)識(shí)符是國(guó)家或地理區(qū)域中語(yǔ)言的標(biāo)準(zhǔn)國(guó)際數(shù)字縮寫。使用Aspose.Words,可以在字段級(jí)別指定區(qū)域設(shè)置。Field.LocaleId屬性獲取或設(shè)置字段的語(yǔ)言環(huán)境ID。下面的示例說(shuō)明了如何使用此選項(xiàng)。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithFields(); DocumentBuilder builder = new DocumentBuilder(); Field field = builder.InsertField(FieldType.FieldDate, true); field.LocaleId = 1049; builder.Document.Save(dataDir + "SpecifylocaleAtFieldlevel_out.docx");
如果要像MS Word允許的那樣插入無(wú)類型/空字段({}),則可以將DocumentBuilder.InsertField方法與參數(shù)FieldType.FieldNone一起使用。要在Word文檔中插入字段,可以按“ Ctrl + F9”組合鍵。下面的代碼示例演示如何在文檔中插入空白字段。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); FieldUnknown field = (FieldUnknown)builder.InsertField(FieldType.FieldNone, false); dataDir = dataDir + "InsertFieldNone_out.docx"; doc.Save(dataDir);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn