@nguyenphp/antigravity-marketing 1.0.18 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/README.md +130 -78
  2. package/package.json +4 -3
  3. package/templates/.agent/skills/marketing-report-expert/SKILL.md +70 -0
  4. package/templates/.agent/skills/minimax-docx/LICENSE +21 -0
  5. package/templates/.agent/skills/minimax-docx/SKILL.md +274 -0
  6. package/templates/.agent/skills/minimax-docx/assets/styles/academic_styles.xml +250 -0
  7. package/templates/.agent/skills/minimax-docx/assets/styles/corporate_styles.xml +284 -0
  8. package/templates/.agent/skills/minimax-docx/assets/styles/default_styles.xml +449 -0
  9. package/templates/.agent/skills/minimax-docx/assets/xsd/aesthetic-rules.xsd +470 -0
  10. package/templates/.agent/skills/minimax-docx/assets/xsd/business-rules.xsd +130 -0
  11. package/templates/.agent/skills/minimax-docx/assets/xsd/common-types.xsd +159 -0
  12. package/templates/.agent/skills/minimax-docx/assets/xsd/wml-subset.xsd +589 -0
  13. package/templates/.agent/skills/minimax-docx/references/cjk_typography.md +357 -0
  14. package/templates/.agent/skills/minimax-docx/references/cjk_university_template_guide.md +184 -0
  15. package/templates/.agent/skills/minimax-docx/references/comments_guide.md +191 -0
  16. package/templates/.agent/skills/minimax-docx/references/design_good_bad_examples.md +829 -0
  17. package/templates/.agent/skills/minimax-docx/references/design_principles.md +819 -0
  18. package/templates/.agent/skills/minimax-docx/references/openxml_element_order.md +308 -0
  19. package/templates/.agent/skills/minimax-docx/references/openxml_encyclopedia_part1.md +4061 -0
  20. package/templates/.agent/skills/minimax-docx/references/openxml_encyclopedia_part2.md +2820 -0
  21. package/templates/.agent/skills/minimax-docx/references/openxml_encyclopedia_part3.md +3381 -0
  22. package/templates/.agent/skills/minimax-docx/references/openxml_namespaces.md +82 -0
  23. package/templates/.agent/skills/minimax-docx/references/openxml_units.md +72 -0
  24. package/templates/.agent/skills/minimax-docx/references/scenario_a_create.md +284 -0
  25. package/templates/.agent/skills/minimax-docx/references/scenario_b_edit_content.md +295 -0
  26. package/templates/.agent/skills/minimax-docx/references/scenario_c_apply_template.md +456 -0
  27. package/templates/.agent/skills/minimax-docx/references/track_changes_guide.md +200 -0
  28. package/templates/.agent/skills/minimax-docx/references/troubleshooting.md +506 -0
  29. package/templates/.agent/skills/minimax-docx/references/typography_guide.md +294 -0
  30. package/templates/.agent/skills/minimax-docx/references/xsd_validation_guide.md +158 -0
  31. package/templates/.agent/skills/minimax-docx/scripts/doc_to_docx.sh +40 -0
  32. package/templates/.agent/skills/minimax-docx/scripts/docx_preview.sh +37 -0
  33. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Cli/MiniMaxAIDocx.Cli.csproj +19 -0
  34. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Cli/Program.cs +18 -0
  35. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/AnalyzeCommand.cs +147 -0
  36. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/ApplyTemplateCommand.cs +322 -0
  37. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/CreateCommand.cs +324 -0
  38. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/DiffCommand.cs +155 -0
  39. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/EditContentCommand.cs +487 -0
  40. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/FixOrderCommand.cs +108 -0
  41. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/MergeRunsCommand.cs +122 -0
  42. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Commands/ValidateCommand.cs +107 -0
  43. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/MiniMaxAIDocx.Core.csproj +15 -0
  44. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/OpenXml/CommentSynchronizer.cs +169 -0
  45. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/OpenXml/ElementOrder.cs +80 -0
  46. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/OpenXml/NamespaceConstants.cs +42 -0
  47. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/OpenXml/RunMerger.cs +81 -0
  48. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/OpenXml/StyleAnalyzer.cs +81 -0
  49. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/OpenXml/TrackChangesHelper.cs +99 -0
  50. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/OpenXml/UnitConverter.cs +23 -0
  51. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/AestheticRecipeSamples.cs +1832 -0
  52. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/AestheticRecipeSamples_Batch1.cs +910 -0
  53. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/AestheticRecipeSamples_Batch2.cs +999 -0
  54. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/AestheticRecipeSamples_Batch3.cs +1048 -0
  55. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/AestheticRecipeSamples_Batch4.cs +1038 -0
  56. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/CharacterFormattingSamples.cs +1020 -0
  57. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/DocumentCreationSamples.cs +1121 -0
  58. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/FieldAndTocSamples.cs +624 -0
  59. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/FootnoteAndCommentSamples.cs +675 -0
  60. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/HeaderFooterSamples.cs +838 -0
  61. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/ImageSamples.cs +917 -0
  62. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/ListAndNumberingSamples.cs +826 -0
  63. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/ParagraphFormattingSamples.cs +1199 -0
  64. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/StyleSystemSamples.cs +1487 -0
  65. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/TableSamples.cs +1163 -0
  66. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/TrackChangesSamples.cs +595 -0
  67. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Typography/CjkHelper.cs +39 -0
  68. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Typography/FontDefaults.cs +24 -0
  69. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Typography/PageSizes.cs +20 -0
  70. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Validation/BusinessRuleValidator.cs +224 -0
  71. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Validation/GateCheckValidator.cs +148 -0
  72. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Validation/ValidationResult.cs +23 -0
  73. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Validation/XsdValidator.cs +69 -0
  74. package/templates/.agent/skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.slnx +4 -0
  75. package/templates/.agent/skills/minimax-docx/scripts/env_check.sh +196 -0
  76. package/templates/.agent/skills/minimax-docx/scripts/setup.ps1 +274 -0
  77. package/templates/.agent/skills/minimax-docx/scripts/setup.sh +504 -0
  78. package/templates/.agent/skills/minimax-multimodal-toolkit/SKILL.md +359 -0
  79. package/templates/.agent/skills/minimax-pdf/README.md +222 -0
  80. package/templates/.agent/skills/minimax-pdf/SKILL.md +201 -0
  81. package/templates/.agent/skills/minimax-pdf/design/design.md +381 -0
  82. package/templates/.agent/skills/minimax-pdf/scripts/cover.py +1579 -0
  83. package/templates/.agent/skills/minimax-pdf/scripts/fill_inspect.py +200 -0
  84. package/templates/.agent/skills/minimax-pdf/scripts/fill_write.py +242 -0
  85. package/templates/.agent/skills/minimax-pdf/scripts/make.sh +491 -0
  86. package/templates/.agent/skills/minimax-pdf/scripts/merge.py +112 -0
  87. package/templates/.agent/skills/minimax-pdf/scripts/palette.py +559 -0
  88. package/templates/.agent/skills/minimax-pdf/scripts/reformat_parse.py +374 -0
  89. package/templates/.agent/skills/minimax-pdf/scripts/render_body.py +1055 -0
  90. package/templates/.agent/skills/minimax-pdf/scripts/render_cover.cjs +111 -0
  91. package/templates/.agent/skills/minimax-xlsx/SKILL.md +138 -0
  92. package/templates/.agent/skills/minimax-xlsx/references/create.md +691 -0
  93. package/templates/.agent/skills/minimax-xlsx/references/edit.md +684 -0
  94. package/templates/.agent/skills/minimax-xlsx/references/fix.md +37 -0
  95. package/templates/.agent/skills/minimax-xlsx/references/format.md +768 -0
  96. package/templates/.agent/skills/minimax-xlsx/references/ooxml-cheatsheet.md +231 -0
  97. package/templates/.agent/skills/minimax-xlsx/references/read-analyze.md +97 -0
  98. package/templates/.agent/skills/minimax-xlsx/references/validate.md +772 -0
  99. package/templates/.agent/skills/minimax-xlsx/scripts/formula_check.py +422 -0
  100. package/templates/.agent/skills/minimax-xlsx/scripts/libreoffice_recalc.py +248 -0
  101. package/templates/.agent/skills/minimax-xlsx/scripts/shared_strings_builder.py +163 -0
  102. package/templates/.agent/skills/minimax-xlsx/scripts/style_audit.py +575 -0
  103. package/templates/.agent/skills/minimax-xlsx/scripts/xlsx_add_column.py +395 -0
  104. package/templates/.agent/skills/minimax-xlsx/scripts/xlsx_insert_row.py +274 -0
  105. package/templates/.agent/skills/minimax-xlsx/scripts/xlsx_pack.py +87 -0
  106. package/templates/.agent/skills/minimax-xlsx/scripts/xlsx_reader.py +362 -0
  107. package/templates/.agent/skills/minimax-xlsx/scripts/xlsx_shift_rows.py +396 -0
  108. package/templates/.agent/skills/minimax-xlsx/scripts/xlsx_unpack.py +130 -0
  109. package/templates/.agent/skills/minimax-xlsx/templates/minimal_xlsx/[Content_Types].xml +9 -0
  110. package/templates/.agent/skills/minimax-xlsx/templates/minimal_xlsx/_rels/.rels +6 -0
  111. package/templates/.agent/skills/minimax-xlsx/templates/minimal_xlsx/xl/_rels/workbook.xml.rels +19 -0
  112. package/templates/.agent/skills/minimax-xlsx/templates/minimal_xlsx/xl/sharedStrings.xml +33 -0
  113. package/templates/.agent/skills/minimax-xlsx/templates/minimal_xlsx/xl/styles.xml +160 -0
  114. package/templates/.agent/skills/minimax-xlsx/templates/minimal_xlsx/xl/workbook.xml +30 -0
  115. package/templates/.agent/skills/minimax-xlsx/templates/minimal_xlsx/xl/worksheets/sheet1.xml +70 -0
  116. package/templates/.agent/skills/pptx-generator/SKILL.md +249 -0
  117. package/templates/.agent/skills/pptx-generator/references/design-system.md +392 -0
  118. package/templates/.agent/skills/pptx-generator/references/editing.md +162 -0
  119. package/templates/.agent/skills/pptx-generator/references/pitfalls.md +112 -0
  120. package/templates/.agent/skills/pptx-generator/references/pptxgenjs.md +420 -0
  121. package/templates/.agent/skills/pptx-generator/references/slide-types.md +413 -0
  122. package/templates/.agent/skills/tutorial-video-expert/SKILL.md +88 -0
  123. package/templates/.agent/skills/ui-ux-pro-max/SKILL.md +170 -585
  124. package/templates/.agent/skills/vision-analysis/SKILL.md +174 -0
  125. package/templates/.agent/workflows/analyze.md +3 -0
  126. package/templates/.agent/workflows/brand-report.md +44 -0
  127. package/templates/.agent/workflows/report.md +49 -0
  128. package/templates/.agent/agents/backend-specialist.md +0 -263
  129. package/templates/.agent/agents/database-architect.md +0 -226
  130. package/templates/.agent/agents/debugger.md +0 -225
  131. package/templates/.agent/agents/devops-engineer.md +0 -242
  132. package/templates/.agent/agents/frontend-specialist.md +0 -527
  133. package/templates/.agent/agents/game-developer.md +0 -162
  134. package/templates/.agent/agents/mobile-developer.md +0 -377
  135. package/templates/.agent/agents/penetration-tester.md +0 -188
  136. package/templates/.agent/agents/security-auditor.md +0 -170
  137. package/templates/.agent/agents/test-engineer.md +0 -158
  138. package/templates/.agent/skills/api-patterns/SKILL.md +0 -81
  139. package/templates/.agent/skills/api-patterns/api-style.md +0 -42
  140. package/templates/.agent/skills/api-patterns/auth.md +0 -24
  141. package/templates/.agent/skills/api-patterns/documentation.md +0 -26
  142. package/templates/.agent/skills/api-patterns/graphql.md +0 -41
  143. package/templates/.agent/skills/api-patterns/rate-limiting.md +0 -31
  144. package/templates/.agent/skills/api-patterns/response.md +0 -37
  145. package/templates/.agent/skills/api-patterns/rest.md +0 -40
  146. package/templates/.agent/skills/api-patterns/scripts/api_validator.py +0 -211
  147. package/templates/.agent/skills/api-patterns/security-testing.md +0 -122
  148. package/templates/.agent/skills/api-patterns/trpc.md +0 -41
  149. package/templates/.agent/skills/api-patterns/versioning.md +0 -22
  150. package/templates/.agent/skills/app-builder/SKILL.md +0 -75
  151. package/templates/.agent/skills/app-builder/agent-coordination.md +0 -71
  152. package/templates/.agent/skills/app-builder/feature-building.md +0 -53
  153. package/templates/.agent/skills/app-builder/project-detection.md +0 -34
  154. package/templates/.agent/skills/app-builder/scaffolding.md +0 -118
  155. package/templates/.agent/skills/app-builder/tech-stack.md +0 -40
  156. package/templates/.agent/skills/app-builder/templates/SKILL.md +0 -39
  157. package/templates/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +0 -76
  158. package/templates/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +0 -92
  159. package/templates/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +0 -88
  160. package/templates/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +0 -88
  161. package/templates/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +0 -83
  162. package/templates/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +0 -90
  163. package/templates/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +0 -90
  164. package/templates/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +0 -82
  165. package/templates/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +0 -100
  166. package/templates/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +0 -106
  167. package/templates/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +0 -101
  168. package/templates/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +0 -83
  169. package/templates/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +0 -93
  170. package/templates/.agent/skills/architecture/SKILL.md +0 -55
  171. package/templates/.agent/skills/architecture/context-discovery.md +0 -43
  172. package/templates/.agent/skills/architecture/examples.md +0 -94
  173. package/templates/.agent/skills/architecture/pattern-selection.md +0 -68
  174. package/templates/.agent/skills/architecture/patterns-reference.md +0 -50
  175. package/templates/.agent/skills/architecture/trade-off-analysis.md +0 -77
  176. package/templates/.agent/skills/bash-linux/SKILL.md +0 -199
  177. package/templates/.agent/skills/behavioral-modes/SKILL.md +0 -242
  178. package/templates/.agent/skills/clean-code/SKILL.md +0 -201
  179. package/templates/.agent/skills/code-review-checklist/SKILL.md +0 -109
  180. package/templates/.agent/skills/database-design/SKILL.md +0 -52
  181. package/templates/.agent/skills/database-design/database-selection.md +0 -43
  182. package/templates/.agent/skills/database-design/indexing.md +0 -39
  183. package/templates/.agent/skills/database-design/migrations.md +0 -48
  184. package/templates/.agent/skills/database-design/optimization.md +0 -36
  185. package/templates/.agent/skills/database-design/orm-selection.md +0 -30
  186. package/templates/.agent/skills/database-design/schema-design.md +0 -56
  187. package/templates/.agent/skills/database-design/scripts/schema_validator.py +0 -172
  188. package/templates/.agent/skills/deployment-procedures/SKILL.md +0 -241
  189. package/templates/.agent/skills/docker-expert/SKILL.md +0 -409
  190. package/templates/.agent/skills/game-development/2d-games/SKILL.md +0 -119
  191. package/templates/.agent/skills/game-development/3d-games/SKILL.md +0 -135
  192. package/templates/.agent/skills/game-development/SKILL.md +0 -167
  193. package/templates/.agent/skills/game-development/game-art/SKILL.md +0 -185
  194. package/templates/.agent/skills/game-development/game-audio/SKILL.md +0 -190
  195. package/templates/.agent/skills/game-development/game-design/SKILL.md +0 -129
  196. package/templates/.agent/skills/game-development/mobile-games/SKILL.md +0 -108
  197. package/templates/.agent/skills/game-development/multiplayer/SKILL.md +0 -132
  198. package/templates/.agent/skills/game-development/pc-games/SKILL.md +0 -144
  199. package/templates/.agent/skills/game-development/vr-ar/SKILL.md +0 -123
  200. package/templates/.agent/skills/game-development/web-games/SKILL.md +0 -150
  201. package/templates/.agent/skills/lint-and-validate/SKILL.md +0 -45
  202. package/templates/.agent/skills/lint-and-validate/scripts/lint_runner.py +0 -172
  203. package/templates/.agent/skills/lint-and-validate/scripts/type_coverage.py +0 -173
  204. package/templates/.agent/skills/mcp-builder/SKILL.md +0 -176
  205. package/templates/.agent/skills/nestjs-expert/SKILL.md +0 -552
  206. package/templates/.agent/skills/nextjs-best-practices/SKILL.md +0 -203
  207. package/templates/.agent/skills/nodejs-best-practices/SKILL.md +0 -333
  208. package/templates/.agent/skills/parallel-agents/SKILL.md +0 -175
  209. package/templates/.agent/skills/performance-profiling/SKILL.md +0 -143
  210. package/templates/.agent/skills/performance-profiling/scripts/lighthouse_audit.py +0 -76
  211. package/templates/.agent/skills/powershell-windows/SKILL.md +0 -167
  212. package/templates/.agent/skills/prisma-expert/SKILL.md +0 -355
  213. package/templates/.agent/skills/python-patterns/SKILL.md +0 -441
  214. package/templates/.agent/skills/react-patterns/SKILL.md +0 -198
  215. package/templates/.agent/skills/red-team-tactics/SKILL.md +0 -199
  216. package/templates/.agent/skills/server-management/SKILL.md +0 -161
  217. package/templates/.agent/skills/systematic-debugging/SKILL.md +0 -109
  218. package/templates/.agent/skills/tdd-workflow/SKILL.md +0 -149
  219. package/templates/.agent/skills/testing-patterns/SKILL.md +0 -178
  220. package/templates/.agent/skills/testing-patterns/scripts/test_runner.py +0 -219
  221. package/templates/.agent/skills/typescript-expert/SKILL.md +0 -429
  222. package/templates/.agent/skills/vue-expert/SKILL.md +0 -374
  223. package/templates/.agent/skills/vulnerability-scanner/SKILL.md +0 -276
  224. package/templates/.agent/skills/vulnerability-scanner/checklists.md +0 -121
  225. package/templates/.agent/skills/vulnerability-scanner/scripts/security_scan.py +0 -458
  226. package/templates/.agent/skills/webapp-testing/SKILL.md +0 -187
  227. package/templates/.agent/skills/webapp-testing/scripts/playwright_runner.py +0 -173
  228. package/templates/.agent/workflows/debug.md +0 -103
  229. package/templates/.agent/workflows/deploy.md +0 -176
  230. package/templates/.agent/workflows/enhance.md +0 -63
  231. package/templates/.agent/workflows/test.md +0 -144
@@ -0,0 +1,624 @@
1
+ using DocumentFormat.OpenXml;
2
+ using DocumentFormat.OpenXml.Packaging;
3
+ using DocumentFormat.OpenXml.Wordprocessing;
4
+
5
+ namespace MiniMaxAIDocx.Core.Samples;
6
+
7
+ /// <summary>
8
+ /// Reference implementations for field codes and Table of Contents (TOC).
9
+ ///
10
+ /// KEY CONCEPTS:
11
+ /// - SimpleField: single-element shorthand, e.g. &lt;w:fldSimple w:instr="PAGE"/&gt;
12
+ /// - Complex field: three FieldChar elements (Begin / Separate / End) with FieldCode between them.
13
+ /// Word always writes complex fields; SimpleField is only used for trivial cases.
14
+ /// - TOC is a structured document tag (SdtBlock) wrapping a complex field.
15
+ /// - UpdateFieldsOnOpen tells Word to recalculate all fields when opening.
16
+ /// </summary>
17
+ public static class FieldAndTocSamples
18
+ {
19
+ // ──────────────────────────────────────────────
20
+ // 1. InsertToc — TOC levels 1-3 inside SdtBlock
21
+ // ──────────────────────────────────────────────
22
+
23
+ /// <summary>
24
+ /// Inserts a Table of Contents covering heading levels 1-3.
25
+ /// Uses an SdtBlock wrapper with a complex field code:
26
+ /// TOC \o "1-3" \h \z \u
27
+ ///
28
+ /// Switches:
29
+ /// \o "1-3" — outline levels 1-3
30
+ /// \h — hyperlinks
31
+ /// \z — hide tab leaders / page numbers in Web Layout
32
+ /// \u — use applied paragraph outline level
33
+ /// </summary>
34
+ public static SdtBlock InsertToc(Body body)
35
+ {
36
+ var sdtBlock = new SdtBlock();
37
+
38
+ // SdtProperties — mark as TOC
39
+ var sdtPr = new SdtProperties();
40
+ sdtPr.Append(new SdtContentDocPartObject(
41
+ new DocPartGallery { Val = "Table of Contents" },
42
+ new DocPartUnique()));
43
+ sdtBlock.Append(sdtPr);
44
+
45
+ // SdtContent — contains the field code paragraph(s)
46
+ var sdtContent = new SdtContentBlock();
47
+
48
+ // TOC title paragraph
49
+ var titlePara = new Paragraph(
50
+ new ParagraphProperties(new ParagraphStyleId { Val = "TOCHeading" }),
51
+ new Run(new Text("Table of Contents")));
52
+ sdtContent.Append(titlePara);
53
+
54
+ // Complex field paragraph for TOC
55
+ var fieldPara = new Paragraph();
56
+ InsertComplexFieldInline(fieldPara, " TOC \\o \"1-3\" \\h \\z \\u ");
57
+ sdtContent.Append(fieldPara);
58
+
59
+ sdtBlock.Append(sdtContent);
60
+ body.Append(sdtBlock);
61
+ return sdtBlock;
62
+ }
63
+
64
+ // ──────────────────────────────────────────────
65
+ // 2. InsertTocWithCustomLevels — TOC 1-4 levels
66
+ // ──────────────────────────────────────────────
67
+
68
+ /// <summary>
69
+ /// Inserts a TOC covering heading levels 1-4.
70
+ /// Identical structure to <see cref="InsertToc"/> but with "\o 1-4".
71
+ /// </summary>
72
+ public static SdtBlock InsertTocWithCustomLevels(Body body)
73
+ {
74
+ var sdtBlock = new SdtBlock();
75
+
76
+ var sdtPr = new SdtProperties();
77
+ sdtPr.Append(new SdtContentDocPartObject(
78
+ new DocPartGallery { Val = "Table of Contents" },
79
+ new DocPartUnique()));
80
+ sdtBlock.Append(sdtPr);
81
+
82
+ var sdtContent = new SdtContentBlock();
83
+
84
+ var titlePara = new Paragraph(
85
+ new ParagraphProperties(new ParagraphStyleId { Val = "TOCHeading" }),
86
+ new Run(new Text("Table of Contents")));
87
+ sdtContent.Append(titlePara);
88
+
89
+ // 1-4 levels instead of 1-3
90
+ var fieldPara = new Paragraph();
91
+ InsertComplexFieldInline(fieldPara, " TOC \\o \"1-4\" \\h \\z \\u ");
92
+ sdtContent.Append(fieldPara);
93
+
94
+ sdtBlock.Append(sdtContent);
95
+ body.Append(sdtBlock);
96
+ return sdtBlock;
97
+ }
98
+
99
+ // ──────────────────────────────────────────────
100
+ // 3. InsertSimpleField — PAGE, NUMPAGES, DATE, etc.
101
+ // ──────────────────────────────────────────────
102
+
103
+ /// <summary>
104
+ /// Inserts a SimpleField element into a paragraph.
105
+ ///
106
+ /// SimpleField is the compact form: &lt;w:fldSimple w:instr=" PAGE "&gt;&lt;w:r&gt;...&lt;/w:r&gt;&lt;/w:fldSimple&gt;
107
+ ///
108
+ /// Common instructions: "PAGE", "NUMPAGES", "DATE", "TIME", "FILENAME".
109
+ /// The run inside is the cached display value; Word recalculates on open.
110
+ /// </summary>
111
+ public static SimpleField InsertSimpleField(Paragraph para, string instruction)
112
+ {
113
+ var simpleField = new SimpleField { Instruction = $" {instruction} " };
114
+
115
+ // Cached display value — Word replaces this on recalculation
116
+ simpleField.Append(new Run(
117
+ new RunProperties(new NoProof()),
118
+ new Text("«" + instruction + "»")));
119
+
120
+ para.Append(simpleField);
121
+ return simpleField;
122
+ }
123
+
124
+ // ──────────────────────────────────────────────
125
+ // 4. InsertComplexField — Begin/Separate/End
126
+ // ──────────────────────────────────────────────
127
+
128
+ /// <summary>
129
+ /// Inserts a complex field into a paragraph using the FieldChar Begin/Separate/End pattern.
130
+ ///
131
+ /// Structure:
132
+ /// Run1: FieldChar(Begin) + FieldCode(" PAGE ")
133
+ /// Run2: FieldChar(Separate)
134
+ /// Run3: Text("1") ← cached display value
135
+ /// Run4: FieldChar(End)
136
+ ///
137
+ /// Use complex fields when you need dirty flags, lock, or nested fields.
138
+ /// </summary>
139
+ public static void InsertComplexField(Paragraph para, string instruction)
140
+ {
141
+ InsertComplexFieldInline(para, $" {instruction} ");
142
+ }
143
+
144
+ // ──────────────────────────────────────────────
145
+ // 5. InsertDateField — DATE with format switch
146
+ // ──────────────────────────────────────────────
147
+
148
+ /// <summary>
149
+ /// Inserts a DATE field with a format switch: DATE \@ "yyyy-MM-dd"
150
+ ///
151
+ /// The \@ switch specifies the date/time picture.
152
+ /// Common formats:
153
+ /// \@ "yyyy-MM-dd" → 2026-03-22
154
+ /// \@ "MMMM d, yyyy" → March 22, 2026
155
+ /// \@ "M/d/yyyy h:mm am/pm" → 3/22/2026 2:30 PM
156
+ /// </summary>
157
+ public static void InsertDateField(Paragraph para, string format)
158
+ {
159
+ // Field instruction with date-time picture switch
160
+ string instruction = $" DATE \\@ \"{format}\" ";
161
+ InsertComplexFieldInline(para, instruction);
162
+ }
163
+
164
+ // ──────────────────────────────────────────────
165
+ // 6. InsertCrossReference — REF field
166
+ // ──────────────────────────────────────────────
167
+
168
+ /// <summary>
169
+ /// Inserts a REF cross-reference field that refers to a bookmark.
170
+ ///
171
+ /// Instruction: REF bookmarkName \h
172
+ /// \h — creates a hyperlink to the bookmark
173
+ /// \p — inserts "above" or "below" relative position
174
+ /// \n — inserts paragraph number of the bookmark
175
+ /// </summary>
176
+ public static void InsertCrossReference(Paragraph para, string bookmarkName)
177
+ {
178
+ string instruction = $" REF {bookmarkName} \\h ";
179
+ InsertComplexFieldInline(para, instruction);
180
+ }
181
+
182
+ // ──────────────────────────────────────────────
183
+ // 7. InsertSequenceField — SEQ for numbering
184
+ // ──────────────────────────────────────────────
185
+
186
+ /// <summary>
187
+ /// Inserts a SEQ (sequence) field for auto-numbering figures, tables, etc.
188
+ ///
189
+ /// Usage pattern for "Figure 1":
190
+ /// 1. Append a run with text "Figure " to the paragraph
191
+ /// 2. Call InsertSequenceField(para, "Figure")
192
+ ///
193
+ /// Usage pattern for "Table 1":
194
+ /// 1. Append a run with text "Table " to the paragraph
195
+ /// 2. Call InsertSequenceField(para, "Table")
196
+ ///
197
+ /// Each unique seqName maintains its own counter across the document.
198
+ /// </summary>
199
+ public static void InsertSequenceField(Paragraph para, string seqName)
200
+ {
201
+ string instruction = $" SEQ {seqName} \\* ARABIC ";
202
+ InsertComplexFieldInline(para, instruction);
203
+ }
204
+
205
+ // ──────────────────────────────────────────────
206
+ // 8. InsertMergeField — MERGEFIELD for mail merge
207
+ // ──────────────────────────────────────────────
208
+
209
+ /// <summary>
210
+ /// Inserts a MERGEFIELD for mail merge scenarios.
211
+ ///
212
+ /// Instruction: MERGEFIELD fieldName \* MERGEFORMAT
213
+ /// \* MERGEFORMAT — preserves formatting applied to the field result
214
+ /// \b "text" — text before if field is non-empty
215
+ /// \f "text" — text after if field is non-empty
216
+ ///
217
+ /// The cached display shows «fieldName» as a placeholder.
218
+ /// </summary>
219
+ public static void InsertMergeField(Paragraph para, string fieldName)
220
+ {
221
+ string instruction = $" MERGEFIELD {fieldName} \\* MERGEFORMAT ";
222
+
223
+ // Begin
224
+ para.Append(new Run(
225
+ new FieldChar { FieldCharType = FieldCharValues.Begin }));
226
+
227
+ // Field code
228
+ para.Append(new Run(
229
+ new FieldCode(instruction) { Space = SpaceProcessingModeValues.Preserve }));
230
+
231
+ // Separate
232
+ para.Append(new Run(
233
+ new FieldChar { FieldCharType = FieldCharValues.Separate }));
234
+
235
+ // Cached value — shows merge field placeholder
236
+ para.Append(new Run(
237
+ new RunProperties(new NoProof()),
238
+ new Text($"\u00AB{fieldName}\u00BB") { Space = SpaceProcessingModeValues.Preserve }));
239
+
240
+ // End
241
+ para.Append(new Run(
242
+ new FieldChar { FieldCharType = FieldCharValues.End }));
243
+ }
244
+
245
+ // ──────────────────────────────────────────────
246
+ // 9. InsertConditionalField — IF field
247
+ // ──────────────────────────────────────────────
248
+
249
+ /// <summary>
250
+ /// Inserts an IF conditional field.
251
+ ///
252
+ /// Syntax: IF expression1 operator expression2 "true-text" "false-text"
253
+ /// Example: IF { MERGEFIELD Gender } = "Male" "Mr." "Ms."
254
+ ///
255
+ /// This example checks if MERGEFIELD Amount > 1000 and displays different text.
256
+ /// Nested fields (MERGEFIELD inside IF) require nested Begin/End pairs.
257
+ /// </summary>
258
+ public static void InsertConditionalField(Paragraph para)
259
+ {
260
+ // Outer IF field Begin
261
+ para.Append(new Run(
262
+ new FieldChar { FieldCharType = FieldCharValues.Begin }));
263
+
264
+ para.Append(new Run(
265
+ new FieldCode(" IF ") { Space = SpaceProcessingModeValues.Preserve }));
266
+
267
+ // Nested MERGEFIELD inside the IF condition
268
+ para.Append(new Run(
269
+ new FieldChar { FieldCharType = FieldCharValues.Begin }));
270
+ para.Append(new Run(
271
+ new FieldCode(" MERGEFIELD Amount ") { Space = SpaceProcessingModeValues.Preserve }));
272
+ para.Append(new Run(
273
+ new FieldChar { FieldCharType = FieldCharValues.Separate }));
274
+ para.Append(new Run(
275
+ new Text("0") { Space = SpaceProcessingModeValues.Preserve }));
276
+ para.Append(new Run(
277
+ new FieldChar { FieldCharType = FieldCharValues.End }));
278
+
279
+ // Continuation of IF instruction
280
+ para.Append(new Run(
281
+ new FieldCode(" > \"1000\" \"High Value\" \"Standard\" ") { Space = SpaceProcessingModeValues.Preserve }));
282
+
283
+ // Separate — cached result
284
+ para.Append(new Run(
285
+ new FieldChar { FieldCharType = FieldCharValues.Separate }));
286
+ para.Append(new Run(
287
+ new RunProperties(new NoProof()),
288
+ new Text("Standard") { Space = SpaceProcessingModeValues.Preserve }));
289
+
290
+ // End
291
+ para.Append(new Run(
292
+ new FieldChar { FieldCharType = FieldCharValues.End }));
293
+ }
294
+
295
+ // ──────────────────────────────────────────────
296
+ // 10. InsertStyleRef — STYLEREF for running headers
297
+ // ──────────────────────────────────────────────
298
+
299
+ /// <summary>
300
+ /// Inserts a STYLEREF field, commonly used in headers/footers
301
+ /// to display the current chapter or section title.
302
+ ///
303
+ /// Instruction: STYLEREF "Heading 1"
304
+ /// Displays the text of the nearest paragraph with style "Heading 1".
305
+ /// \l — search from bottom of page up (for last instance on page)
306
+ /// \n — insert the paragraph number, not text
307
+ /// </summary>
308
+ public static void InsertStyleRef(Paragraph para)
309
+ {
310
+ string instruction = " STYLEREF \"Heading 1\" ";
311
+ InsertComplexFieldInline(para, instruction);
312
+ }
313
+
314
+ // ──────────────────────────────────────────────
315
+ // 11. EnableUpdateFieldsOnOpen
316
+ // ──────────────────────────────────────────────
317
+
318
+ /// <summary>
319
+ /// Sets the UpdateFieldsOnOpen property so Word recalculates
320
+ /// all fields (PAGE, TOC, SEQ, etc.) when the document is opened.
321
+ ///
322
+ /// Without this, TOC and cross-references show stale cached values
323
+ /// until the user manually presses Ctrl+A, F9 to update.
324
+ /// </summary>
325
+ public static void EnableUpdateFieldsOnOpen(DocumentSettingsPart settingsPart)
326
+ {
327
+ settingsPart.Settings ??= new Settings();
328
+ var existing = settingsPart.Settings.GetFirstChild<UpdateFieldsOnOpen>();
329
+ if (existing != null)
330
+ {
331
+ existing.Val = true;
332
+ }
333
+ else
334
+ {
335
+ settingsPart.Settings.Append(new UpdateFieldsOnOpen { Val = true });
336
+ }
337
+ settingsPart.Settings.Save();
338
+ }
339
+
340
+ // ──────────────────────────────────────────────
341
+ // 12. CreateTocStyles — TOC1/2/3 with tab leaders
342
+ // ──────────────────────────────────────────────
343
+
344
+ /// <summary>
345
+ /// Creates TOC1, TOC2, TOC3 paragraph styles with right-aligned tab stops
346
+ /// and dot leaders (the "....." between entry text and page number).
347
+ ///
348
+ /// Each TOC level is indented further:
349
+ /// TOC1 — 0 indent
350
+ /// TOC2 — 240 twips (1/6 inch)
351
+ /// TOC3 — 480 twips (1/3 inch)
352
+ ///
353
+ /// Tab leader: dot-filled right tab at 9360 twips (6.5 inches for letter paper).
354
+ /// </summary>
355
+ public static void CreateTocStyles(StyleDefinitionsPart stylesPart)
356
+ {
357
+ stylesPart.Styles ??= new Styles();
358
+
359
+ string[] tocStyleIds = ["TOC1", "TOC2", "TOC3"];
360
+ string[] tocStyleNames = ["toc 1", "toc 2", "toc 3"];
361
+ int[] indents = [0, 240, 480]; // twips
362
+
363
+ // Right tab position: 6.5 inches = 9360 twips (standard for US Letter)
364
+ const int tabPosition = 9360;
365
+
366
+ for (int i = 0; i < tocStyleIds.Length; i++)
367
+ {
368
+ var style = new Style
369
+ {
370
+ Type = StyleValues.Paragraph,
371
+ StyleId = tocStyleIds[i],
372
+ CustomStyle = false
373
+ };
374
+
375
+ style.Append(new StyleName { Val = tocStyleNames[i] });
376
+ style.Append(new BasedOn { Val = "Normal" });
377
+ style.Append(new NextParagraphStyle { Val = "Normal" });
378
+ style.Append(new UIPriority { Val = 39 });
379
+
380
+ var pPr = new StyleParagraphProperties();
381
+
382
+ // Indentation for nested levels
383
+ if (indents[i] > 0)
384
+ {
385
+ pPr.Append(new Indentation { Left = indents[i].ToString() });
386
+ }
387
+
388
+ // Spacing: no space after for compact TOC
389
+ pPr.Append(new SpacingBetweenLines { After = "0", Line = "276", LineRule = LineSpacingRuleValues.Auto });
390
+
391
+ // Right-aligned tab with dot leader
392
+ var tabs = new Tabs();
393
+ tabs.Append(new TabStop
394
+ {
395
+ Val = TabStopValues.Right,
396
+ Leader = TabStopLeaderCharValues.Dot,
397
+ Position = tabPosition
398
+ });
399
+ pPr.Append(tabs);
400
+
401
+ style.Append(pPr);
402
+ stylesPart.Styles.Append(style);
403
+ }
404
+
405
+ stylesPart.Styles.Save();
406
+ }
407
+
408
+ // ──────────────────────────────────────────────
409
+ // 13. CreateMixedTocStructure — Real-world TOC
410
+ // ──────────────────────────────────────────────
411
+
412
+ /// <summary>
413
+ /// Real-world TOC structure: Mixed SDT block + static entries + field code.
414
+ ///
415
+ /// IMPORTANT: Most templates do NOT have a clean TOC field code alone.
416
+ /// Instead, they contain:
417
+ /// 1. An SDT (Structured Document Tag) wrapper with alias "TOC"
418
+ /// 2. Inside the SDT: a field code BEGIN + SEPARATE + static example entries + END
419
+ /// 3. The static entries are placeholder text (e.g., "第1章 绪论...........1")
420
+ /// that Word replaces when user presses "Update Fields"
421
+ ///
422
+ /// When applying a template (Scenario C), you should:
423
+ /// - KEEP the entire SDT block from the template (don't rebuild it)
424
+ /// - DO NOT replace static entries with programmatic content
425
+ /// - The entries will auto-update when the user opens in Word and updates fields
426
+ /// - If you must update entries programmatically, replace the content INSIDE
427
+ /// the SDT between fldChar separate and fldChar end
428
+ ///
429
+ /// Common mistake: Treating TOC as pure field code and rebuilding it from scratch,
430
+ /// which destroys the SDT wrapper and breaks Word's "Update Table" functionality.
431
+ /// </summary>
432
+ public static void CreateMixedTocStructure(string outputPath)
433
+ {
434
+ using var doc = WordprocessingDocument.Create(outputPath, WordprocessingDocumentType.Document);
435
+ var mainPart = doc.AddMainDocumentPart();
436
+ mainPart.Document = new Document();
437
+ var body = new Body();
438
+ mainPart.Document.Append(body);
439
+
440
+ // Add styles part with TOC styles
441
+ var stylesPart = mainPart.AddNewPart<StyleDefinitionsPart>();
442
+ CreateTocStyles(stylesPart);
443
+
444
+ // ─── SDT Block wrapping the entire TOC ───
445
+ var sdtBlock = new SdtBlock();
446
+
447
+ // SDT Properties: alias "TOC", tag, and DocPartGallery
448
+ var sdtPr = new SdtProperties();
449
+ sdtPr.Append(new SdtAlias { Val = "TOC" });
450
+ sdtPr.Append(new Tag { Val = "TOC" });
451
+ sdtPr.Append(new SdtContentDocPartObject(
452
+ new DocPartGallery { Val = "Table of Contents" },
453
+ new DocPartUnique()));
454
+ sdtBlock.Append(sdtPr);
455
+
456
+ // SDT Content: field code + static entries
457
+ var sdtContent = new SdtContentBlock();
458
+
459
+ // ─── TOC title paragraph ───
460
+ var titlePara = new Paragraph(
461
+ new ParagraphProperties(new ParagraphStyleId { Val = "TOCHeading" }),
462
+ new Run(new Text("目 录")));
463
+ sdtContent.Append(titlePara);
464
+
465
+ // ─── Field code BEGIN paragraph ───
466
+ var fieldBeginPara = new Paragraph();
467
+
468
+ // fldChar Begin
469
+ fieldBeginPara.Append(new Run(
470
+ new FieldChar { FieldCharType = FieldCharValues.Begin }));
471
+
472
+ // instrText: TOC \o "1-3" \h \z \u
473
+ fieldBeginPara.Append(new Run(
474
+ new FieldCode(" TOC \\o \"1-3\" \\h \\z \\u ") { Space = SpaceProcessingModeValues.Preserve }));
475
+
476
+ // fldChar Separate
477
+ fieldBeginPara.Append(new Run(
478
+ new FieldChar { FieldCharType = FieldCharValues.Separate }));
479
+
480
+ sdtContent.Append(fieldBeginPara);
481
+
482
+ // ─── Static placeholder entries (TOC1/TOC2/TOC3) ───
483
+ // These are the example entries that Word will replace when user clicks "Update Table".
484
+ // In real templates, these show example chapter titles with dot leaders and page numbers.
485
+
486
+ // TOC level 1 entry: "第1章 绪论...........1"
487
+ sdtContent.Append(CreateStaticTocEntry("TOC1", "第1章 绪论", "1"));
488
+
489
+ // TOC level 2 entry: "1.1 研究背景...........1"
490
+ sdtContent.Append(CreateStaticTocEntry("TOC2", "1.1 研究背景", "1"));
491
+
492
+ // TOC level 2 entry: "1.2 研究目的...........2"
493
+ sdtContent.Append(CreateStaticTocEntry("TOC2", "1.2 研究目的", "2"));
494
+
495
+ // TOC level 1 entry: "第2章 文献综述...........3"
496
+ sdtContent.Append(CreateStaticTocEntry("TOC1", "第2章 文献综述", "3"));
497
+
498
+ // TOC level 2 entry: "2.1 国内研究现状...........3"
499
+ sdtContent.Append(CreateStaticTocEntry("TOC2", "2.1 国内研究现状", "3"));
500
+
501
+ // TOC level 3 entry: "2.1.1 早期研究...........4"
502
+ sdtContent.Append(CreateStaticTocEntry("TOC3", "2.1.1 早期研究", "4"));
503
+
504
+ // TOC level 1 entry: "第3章 研究方法...........5"
505
+ sdtContent.Append(CreateStaticTocEntry("TOC1", "第3章 研究方法", "5"));
506
+
507
+ // ─── Field code END paragraph ───
508
+ var fieldEndPara = new Paragraph();
509
+ fieldEndPara.Append(new Run(
510
+ new FieldChar { FieldCharType = FieldCharValues.End }));
511
+ sdtContent.Append(fieldEndPara);
512
+
513
+ sdtBlock.Append(sdtContent);
514
+ body.Append(sdtBlock);
515
+
516
+ // ─── Actual heading paragraphs (what the TOC references) ───
517
+ body.Append(new Paragraph(
518
+ new ParagraphProperties(new ParagraphStyleId { Val = "Heading1" }),
519
+ new Run(new Text("第1章 绪论"))));
520
+
521
+ body.Append(new Paragraph(
522
+ new ParagraphProperties(new ParagraphStyleId { Val = "Heading2" }),
523
+ new Run(new Text("1.1 研究背景"))));
524
+
525
+ body.Append(new Paragraph(
526
+ new Run(new Text("本研究旨在探讨……"))));
527
+
528
+ body.Append(new Paragraph(
529
+ new ParagraphProperties(new ParagraphStyleId { Val = "Heading2" }),
530
+ new Run(new Text("1.2 研究目的"))));
531
+
532
+ body.Append(new Paragraph(
533
+ new Run(new Text("研究目的包括……"))));
534
+
535
+ body.Append(new Paragraph(
536
+ new ParagraphProperties(new ParagraphStyleId { Val = "Heading1" }),
537
+ new Run(new Text("第2章 文献综述"))));
538
+
539
+ body.Append(new Paragraph(
540
+ new ParagraphProperties(new ParagraphStyleId { Val = "Heading2" }),
541
+ new Run(new Text("2.1 国内研究现状"))));
542
+
543
+ body.Append(new Paragraph(
544
+ new ParagraphProperties(new ParagraphStyleId { Val = "Heading3" }),
545
+ new Run(new Text("2.1.1 早期研究"))));
546
+
547
+ body.Append(new Paragraph(
548
+ new Run(new Text("早期研究表明……"))));
549
+
550
+ body.Append(new Paragraph(
551
+ new ParagraphProperties(new ParagraphStyleId { Val = "Heading1" }),
552
+ new Run(new Text("第3章 研究方法"))));
553
+
554
+ body.Append(new Paragraph(
555
+ new Run(new Text("本章介绍研究方法……"))));
556
+
557
+ // ─── Enable UpdateFieldsOnOpen so TOC auto-refreshes ───
558
+ var settingsPart = mainPart.AddNewPart<DocumentSettingsPart>();
559
+ EnableUpdateFieldsOnOpen(settingsPart);
560
+
561
+ mainPart.Document.Save();
562
+ }
563
+
564
+ /// <summary>
565
+ /// Helper: creates a single static TOC entry paragraph with style, text, tab leader, and page number.
566
+ /// This mirrors what Word generates inside a TOC SDT block.
567
+ /// </summary>
568
+ private static Paragraph CreateStaticTocEntry(string tocStyleId, string entryText, string pageNumber)
569
+ {
570
+ var para = new Paragraph();
571
+
572
+ // Paragraph properties: TOC style + right-aligned tab with dot leader
573
+ var pPr = new ParagraphProperties();
574
+ pPr.Append(new ParagraphStyleId { Val = tocStyleId });
575
+ para.Append(pPr);
576
+
577
+ // Run with entry text
578
+ para.Append(new Run(
579
+ new RunProperties(new NoProof()),
580
+ new Text(entryText) { Space = SpaceProcessingModeValues.Preserve }));
581
+
582
+ // Tab character (creates the dot leader between text and page number)
583
+ para.Append(new Run(new TabChar()));
584
+
585
+ // Page number
586
+ para.Append(new Run(
587
+ new RunProperties(new NoProof()),
588
+ new Text(pageNumber)));
589
+
590
+ return para;
591
+ }
592
+
593
+ // ──────────────────────────────────────────────
594
+ // Private helper: insert complex field inline
595
+ // ──────────────────────────────────────────────
596
+
597
+ /// <summary>
598
+ /// Shared helper that appends Begin / FieldCode / Separate / CachedValue / End
599
+ /// runs to a paragraph.
600
+ /// </summary>
601
+ private static void InsertComplexFieldInline(Paragraph para, string instruction)
602
+ {
603
+ // Run 1: FieldChar Begin
604
+ para.Append(new Run(
605
+ new FieldChar { FieldCharType = FieldCharValues.Begin }));
606
+
607
+ // Run 2: FieldCode (the instruction text)
608
+ para.Append(new Run(
609
+ new FieldCode(instruction) { Space = SpaceProcessingModeValues.Preserve }));
610
+
611
+ // Run 3: FieldChar Separate
612
+ para.Append(new Run(
613
+ new FieldChar { FieldCharType = FieldCharValues.Separate }));
614
+
615
+ // Run 4: Cached display value (placeholder until Word recalculates)
616
+ para.Append(new Run(
617
+ new RunProperties(new NoProof()),
618
+ new Text("1") { Space = SpaceProcessingModeValues.Preserve }));
619
+
620
+ // Run 5: FieldChar End
621
+ para.Append(new Run(
622
+ new FieldChar { FieldCharType = FieldCharValues.End }));
623
+ }
624
+ }