视图模式、文本语言与最后保存时间都属于文档的显示与元数据设置。视图模式决定 Word 打开文件时的版式与缩放,文本语言影响该段文字的拼写检查,最后保存时间则记录在文件属性中,供归档与检索使用。三者不改变正文内容,却直接影响打开体验与文档管理。
Spire.Doc for C++ 通过 Document->GetViewSetup() 设置视图类型与缩放,通过 CharacterFormat->SetLocaleIdASCII() 指定文本语言,通过 BuiltinDocumentProperties->SetLastSaveDate() 写入保存时间。全部操作在本地进程中完成,文档无需上传到任何服务。
本文介绍三个功能点:
示例使用《智慧仓储管理平台项目进展报告》(Template.docx),文档包含标题、三个正文小节与一张风险表:

安装 Spire.Doc for C++
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,也更推荐使用。
- 在 Visual Studio 中打开“NuGet 包管理器”,搜索并安装
Spire.Doc.Cpp。 - 或在项目所在目录执行
nuget install Spire.Doc.Cpp,再将包中的include与lib目录加入项目。
集成后的项目需要引用对应的导入库,并确保运行目录中存在 SpireDocCpp.dll、Spire.Doc.Base.dll 等依赖文件。更详细的配置说明请参考如何将 Spire.Doc for C++ 集成到 C++ 程序中。
设置视图模式与缩放
视图设置由 Document->GetViewSetup() 返回的 ViewSetup 对象管理。SetDocumentViewType() 指定版式,SetZoomPercent() 指定缩放百分比,SetZoomType() 指定缩放方式。示例把文档设为 Web 版式、150% 缩放,并使用 ZoomType::None 表示固定百分比。
| 操作 | 方法 | 说明 |
|---|---|---|
| 设置版式 | SetDocumentViewType(DocumentViewType) |
取值为 PrintLayout、WebLayout、OutlineLayout、NormalLayout |
| 设置缩放百分比 | SetZoomPercent(int) |
常用范围为 10 至 500 |
| 设置缩放方式 | SetZoomType(ZoomType) |
None 表示使用固定百分比 |
| 读取当前设置 | GetDocumentViewType()、GetZoomPercent()、GetZoomType() |
返回保存值 |
具体操作步骤如下。
- 创建 Document 对象并调用 LoadFromFile() 加载示例文档。
- 调用 GetViewSetup() 取得视图设置对象。
- 调用 SetDocumentViewType(DocumentViewType::WebLayout) 设置 Web 版式。
- 调用 SetZoomPercent(150) 与 SetZoomType(ZoomType::None) 设置固定缩放。
- 读取并输出三项设置,使用 SaveToFile() 保存文档。
设置视图模式与缩放的代码如下。
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/DocumentViewType.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/Formatting/ViewSetup.h"
#include "Spire/Doc/ZoomType.h"
#include "Spire/Doc/common/h/Stream.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Common;
using namespace Spire::Doc::Formatting;
int main()
{
SetConsoleOutputCP(CP_UTF8);
//加载项目进展报告
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("Template.docx");
//设置 Web 版式、150% 缩放并指定缩放类型
intrusive_ptr<ViewSetup> view = document->GetViewSetup();
view->SetDocumentViewType(DocumentViewType::WebLayout);
view->SetZoomPercent(150);
view->SetZoomType(ZoomType::None);
cout << "视图类型:" << (int)view->GetDocumentViewType() << endl;
cout << "缩放百分比:" << view->GetZoomPercent() << "%" << endl;
cout << "缩放类型:" << (int)view->GetZoomType() << endl;
//保存并关闭文档
document->SaveToFile("Output/SetWordViewModes.docx", FileFormat::Docx);
document->Close();
return 0;
}
控制台输出显示三项设置均已写入:
视图类型:5
缩放百分比:150%
缩放类型:0
用 WPS 打开保存后的文档,正文按 Web 版式连续排列,缩放为 150%:

视图设置是文件级显示属性
视图模式保存在文档设置部分,Word 打开文件时读取该值并据此显示。它不影响打印结果,也不改变分页位置。若文件由服务端生成并在客户端打开,应根据使用场景选择版式:需要与纸质输出一致时使用 PrintLayout,需要在窄窗口中连续阅读时使用 WebLayout。
缩放方式优先于百分比
SetZoomType() 取值不是 None 时,Word 会按窗口大小重新计算缩放,SetZoomPercent() 写入的百分比可能被覆盖。示例同时设置 SetZoomPercent(150) 与 SetZoomType(ZoomType::None),使 150% 成为固定值。只调整百分比而不指定缩放方式时,应先确认当前 ZoomType 的值。
版式与缩放不写入正文
ViewSetup 只修改文档的显示设置,段落、表格与页眉页脚保持原样。示例保存后再次加载,正文对象与分页均未改变。需要调整纸张、页边距或分栏时,应使用 Section->GetPageSetup(),与视图设置属于不同对象。
设置文本的拼写检查语言
文本语言由字符格式的三个区域设置 ID 控制:LocaleIdASCII 作用于西文字符,LocaleIdFarEast 作用于中日韩字符,LocaleIdBi 作用于双向文本。示例写入一句缺少重音符号的西班牙语,并把西文语言设为 10250,即秘鲁西班牙语。设置后,Word 按该语言检查拼写。
| 操作 | 方法 | 说明 |
|---|---|---|
| 设置西文语言 | SetLocaleIdASCII(short) |
Windows 区域设置 ID |
| 读取西文语言 | GetLocaleIdASCII() |
返回 short |
| 设置东亚语言 | SetLocaleIdFarEast(short) |
中文、日文、韩文 |
| 设置双向文本语言 | SetLocaleIdBi(short) |
阿拉伯语、希伯来语等 |
具体操作步骤如下。
- 创建 Document 对象并添加一节。
- 在节中添加段落,调用 AppendText() 写入西班牙语文本。
- 调用 GetCharacterFormat() 取得该文本范围的字符格式。
- 调用 SetLocaleIdASCII(10250) 指定秘鲁西班牙语。
- 读取并输出语言 ID,使用 SaveToFile() 保存文档。
设置文本语言的代码如下。
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/Formatting/CharacterFormat.h"
#include "Spire/Doc/Section.h"
#include "Spire/Doc/common/h/Stream.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Common;
using namespace Spire::Doc::Documents;
using namespace Spire::Doc::Fields;
using namespace Spire::Doc::Formatting;
int main()
{
SetConsoleOutputCP(CP_UTF8);
//新建文档并添加一节
intrusive_ptr<Document> document = new Document();
intrusive_ptr<Section> section = document->AddSection();
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
//写入西班牙语文本,并把校对语言设为秘鲁西班牙语
intrusive_ptr<TextRange> range = paragraph->AppendText(
"corrige segun diccionario en ingles");
range->GetCharacterFormat()->SetLocaleIdASCII(10250);
cout << "LocaleIdASCII:" << range->GetCharacterFormat()->GetLocaleIdASCII() << endl;
//保存并关闭文档
document->SaveToFile("Output/SetTextLanguage.docx", FileFormat::Docx);
document->Close();
return 0;
}
控制台输出显示语言 ID 已写入字符格式:
LocaleIdASCII:10250
语言设置作用于文本范围
SetLocaleIdASCII() 是字符格式的方法,只影响调用它的 TextRange。同一段落中后续追加的文字不会自动继承该语言,需要对新范围再次设置。示例在 AppendText() 返回的对象上直接调用,语言覆盖整句文本。
区域设置 ID 来自 Windows
10250 是秘鲁西班牙语的 LCID。常用值还包括 1033(英语-美国)、1036(法语-法国)、2052(中文-简体)与 1041(日语)。传入未安装的语言 ID 时,Word 可能回退到默认校对语言。中文文本应使用 SetLocaleIdFarEast(2052),仅设置 LocaleIdASCII 不会改变中文的校对语言。
语言设置不改变文字内容
语言仅影响拼写与语法检查,不改变字符本身,也不触发自动更正。示例中的西班牙语仍保留原有拼写,只是在 Word 中按秘鲁西班牙语词典检查。需要在保存前执行拼写检查时,应通过 Word 的校对功能完成,Spire.Doc 不提供拼写检查引擎。
设置最后保存时间
最后保存时间属于内置文档属性,通过 BuiltinDocumentProperties->SetLastSaveDate() 写入。该方法接收 UTC 时间;直接写入本地时间会产生时区偏移。示例先取得当前本地时间,再通过 TimeZone::GetCurrentTimeZone() 与 GetUtcOffset() 计算偏移量,最后用 DateTime::op_Subtraction() 得到 UTC 时间。
| 操作 | 方法 | 说明 |
|---|---|---|
| 取得当前时间 | DateTime::GetNow() |
本地时间 |
| 取得当前时区 | TimeZone::GetCurrentTimeZone() |
返回 TimeZone |
| 计算 UTC 偏移 | GetUtcOffset(DateTime) |
返回 TimeSpan |
| 时间相减 | DateTime::op_Subtraction(time, span) |
本地时间减偏移量 |
| 写入保存时间 | SetLastSaveDate(DateTime) |
接收 UTC 时间 |
具体操作步骤如下。
- 创建 Document 对象并调用 LoadFromFile() 加载示例文档。
- 调用 DateTime::GetNow() 取得本地时间。
- 调用 GetCurrentTimeZone() 与 GetUtcOffset() 计算当前时区偏移。
- 调用 DateTime::op_Subtraction() 把本地时间换算为 UTC。
- 调用 SetLastSaveDate() 写入,使用 SaveToFile() 保存文档。
设置最后保存时间的代码如下。
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/BuiltinDocumentProperties.h"
#include "Spire/Doc/Document.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/common/h/DateTime.h"
#include "Spire/Doc/common/h/Stream.h"
#include "Spire/Doc/common/h/TimeSpan.h"
#include "Spire/Doc/common/h/TimeZone.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Common;
//把本地时间换算为格林尼治时间
intrusive_ptr<DateTime> LocalTimeToGreenwichTime(intrusive_ptr<DateTime> localTime)
{
intrusive_ptr<TimeZone> zone = TimeZone::GetCurrentTimeZone();
intrusive_ptr<TimeSpan> offset = zone->GetUtcOffset(localTime);
return DateTime::op_Subtraction(localTime, offset);
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
//加载项目进展报告
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("Template.docx");
//把当前本地时间换算为 UTC 后写入最后保存时间
intrusive_ptr<DateTime> localNow = DateTime::GetNow();
intrusive_ptr<DateTime> utcNow = LocalTimeToGreenwichTime(localNow);
document->GetBuiltinDocumentProperties()->SetLastSaveDate(utcNow);
cout << "本地时间:" << localNow->ToString() << endl;
cout << "写入的 UTC 时间:" << utcNow->ToString() << endl;
//保存并关闭文档
document->SaveToFile("Output/UpdateLastSavedDate.docx", FileFormat::Docx);
document->Close();
return 0;
}
控制台同时输出本地时间与换算后的 UTC 时间,两者相差 8 小时:
本地时间:2026/11/30 11:58:08
写入的 UTC 时间:2026/11/30 3:58:08
保存后查看文件属性的“详细信息”,最后一次保存的日期显示为换算后的时间:

SetLastSaveDate 接收 UTC 时间
SetLastSaveDate() 按 UTC 解释传入的 DateTime。若直接写入 DateTime::GetNow(),文件属性中的时间会比实际时间多出一个时区偏移。示例通过 GetUtcOffset() 取得当前时区的偏移量,再从本地时间中减去该偏移量。偏移量随夏令时变化,因此应对当前时间调用 GetUtcOffset(),不要使用固定的 8 小时。
文件系统时间与文档属性不同
保存文档时,操作系统会更新文件的修改时间;SetLastSaveDate() 修改的是文档内部记录的最后保存时间,两者来源不同。示例写入的 UTC 时间在“详细信息”中按本地时区显示,因此与本地时间一致。需要让归档系统按统一时区排序时,应以文档属性为准。
保存时间由保存操作写入
SetLastSaveDate() 修改的是内存中的属性值,调用 SaveToFile() 后才写入文件。若在保存前再次加载其他文档或覆盖属性,最终结果以最后一次设置为准。创建时间可通过 SetCreateDate() 单独设置。
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







