Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit 547ec4f

Browse files
committed
feat: add TOC generate and style
need manual update
1 parent e49dcb8 commit 547ec4f

File tree

2 files changed

+154
-22
lines changed

2 files changed

+154
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- [x] 页边距
1515
- [x] 参考文献
1616
- [x] 封面
17-
- [ ] 目录
17+
- [x] 目录
1818

1919
以上为我心中该项目demo的应有功能,在此之前除了更新进度外不会做进一步的文档更新。
2020

md2docx/md2docx.cs

Lines changed: 153 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ private static void GenerateMainDocumentPart1Content(MainDocumentPart mainDocume
208208
Add_abstract(e_title, e_abs, e_kew, false, ref docBody);
209209
}
210210

211+
GenerateTOC(ref docBody);
212+
211213
// rendering body text(paragraph/heading, others are TBD)
212214
foreach (var element in document.Blocks)
213215
{
@@ -1430,6 +1432,108 @@ private static void GenerateCover(ref Body docBody)
14301432
docBody.Append(paragraph27);
14311433
}
14321434

1435+
private static void GenerateTOC(ref Body docBody)
1436+
{
1437+
Paragraph para = new Paragraph
1438+
{
1439+
ParagraphProperties = new ParagraphProperties
1440+
{
1441+
ParagraphStyleId = new ParagraphStyleId { Val = "Abs" },
1442+
PageBreakBefore = new PageBreakBefore()
1443+
}
1444+
};
1445+
Run run = new Run
1446+
{
1447+
RunProperties = new RunProperties()
1448+
};
1449+
Text txt = new Text { Text = "目录" };
1450+
run.Append(txt);
1451+
para.Append(run);
1452+
docBody.Append(para);
1453+
1454+
SdtBlock sdtBlock1 = new SdtBlock();
1455+
1456+
SdtProperties sdtProperties1 = new SdtProperties();
1457+
1458+
RunProperties runProperties1 = new RunProperties();
1459+
Languages languages1 = new Languages() { Val = "zh-CN" };
1460+
1461+
runProperties1.Append(languages1);
1462+
SdtId sdtId1 = new SdtId() { Val = 504253948 };
1463+
1464+
SdtContentDocPartObject sdtContentDocPartObject1 = new SdtContentDocPartObject();
1465+
DocPartGallery docPartGallery1 = new DocPartGallery() { Val = "Table of Contents" };
1466+
DocPartUnique docPartUnique1 = new DocPartUnique();
1467+
1468+
sdtContentDocPartObject1.Append(docPartGallery1);
1469+
sdtContentDocPartObject1.Append(docPartUnique1);
1470+
1471+
sdtProperties1.Append(runProperties1);
1472+
sdtProperties1.Append(sdtId1);
1473+
sdtProperties1.Append(sdtContentDocPartObject1);
1474+
1475+
SdtEndCharProperties sdtEndCharProperties1 = new SdtEndCharProperties();
1476+
1477+
RunProperties runProperties2 = new RunProperties();
1478+
RunFonts runFonts1 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "宋体", ComplexScript = "Times New Roman" };
1479+
Bold bold1 = new Bold();
1480+
BoldComplexScript boldComplexScript1 = new BoldComplexScript();
1481+
Color color1 = new Color() { Val = "auto" };
1482+
Kern kern1 = new Kern() { Val = (UInt32Value)2U };
1483+
FontSize fontSize1 = new FontSize() { Val = "24" };
1484+
FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript() { Val = "24" };
1485+
1486+
runProperties2.Append(runFonts1);
1487+
runProperties2.Append(bold1);
1488+
runProperties2.Append(boldComplexScript1);
1489+
runProperties2.Append(color1);
1490+
runProperties2.Append(kern1);
1491+
runProperties2.Append(fontSize1);
1492+
runProperties2.Append(fontSizeComplexScript1);
1493+
1494+
sdtEndCharProperties1.Append(runProperties2);
1495+
1496+
SdtContentBlock sdtContentBlock1 = new SdtContentBlock();
1497+
1498+
Paragraph paragraph3 = new Paragraph() { RsidParagraphAddition = "003863DC", RsidRunAdditionDefault = "003863DC", ParagraphId = "233F4095", TextId = "52301D4D" };
1499+
1500+
SimpleField simpleField1 = new SimpleField() { Instruction = " TOC \\o \"1-3\" \\h \\z \\u " };
1501+
1502+
Run run4 = new Run();
1503+
1504+
RunProperties runProperties5 = new RunProperties
1505+
{
1506+
FontSize = new FontSize { Val = "24" },
1507+
FontSizeComplexScript = new FontSizeComplexScript { Val = "24" }
1508+
};
1509+
RunFonts runFonts2 = new RunFonts() { Hint = FontTypeHintValues.EastAsia };
1510+
Bold bold2 = new Bold();
1511+
BoldComplexScript boldComplexScript2 = new BoldComplexScript();
1512+
NoProof noProof1 = new NoProof();
1513+
1514+
runProperties5.Append(runFonts2);
1515+
runProperties5.Append(bold2);
1516+
runProperties5.Append(boldComplexScript2);
1517+
runProperties5.Append(noProof1);
1518+
Text text3 = new Text();
1519+
text3.Text = "请手动点击<更新目录>按钮更新目录";
1520+
1521+
run4.Append(runProperties5);
1522+
run4.Append(text3);
1523+
1524+
simpleField1.Append(run4);
1525+
1526+
paragraph3.Append(simpleField1);
1527+
1528+
sdtContentBlock1.Append(paragraph3);
1529+
1530+
sdtBlock1.Append(sdtProperties1);
1531+
sdtBlock1.Append(sdtEndCharProperties1);
1532+
sdtBlock1.Append(sdtContentBlock1);
1533+
1534+
docBody.Append(sdtBlock1);
1535+
}
1536+
14331537
private static void GenerateImagePart1Content(ImagePart imagePart1)
14341538
{
14351539
System.IO.Stream data = GetBinaryDataStream(imagePart1Data);
@@ -1544,7 +1648,7 @@ private static void Deal_quote_refer(MarkdownBlock block, ref Body docBody)
15441648
private static void Add_abstract(string title, string abs, string keyWords, bool isCN, ref Body docBody)
15451649
{
15461650
string subtitle = isCN ? "摘要" : "ABSTRACT";
1547-
string keyWT = isCN ? "关键词:" : "Key words ";
1651+
string keyWT = isCN ? "关键词:" : "Key words: ";
15481652
Paragraph para = new Paragraph
15491653
{
15501654
ParagraphProperties = new ParagraphProperties
@@ -1625,7 +1729,7 @@ private static void Add_abstract(string title, string abs, string keyWords, bool
16251729
run.Append(txt);
16261730
para.Append(run);
16271731
run = new Run { RunProperties = new RunProperties() };
1628-
txt = new Text { Text = c_kew, Space = SpaceProcessingModeValues.Preserve };
1732+
txt = new Text { Text = keyWords, Space = SpaceProcessingModeValues.Preserve };
16291733
run.Append(txt);
16301734
para.Append(run);
16311735
docBody.Append(para);
@@ -2468,15 +2572,6 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
24682572
LatentStyleExceptionInfo latentStyleExceptionInfo17 = new LatentStyleExceptionInfo() { Name = "index 7", SemiHidden = true, UnhideWhenUsed = true };
24692573
LatentStyleExceptionInfo latentStyleExceptionInfo18 = new LatentStyleExceptionInfo() { Name = "index 8", SemiHidden = true, UnhideWhenUsed = true };
24702574
LatentStyleExceptionInfo latentStyleExceptionInfo19 = new LatentStyleExceptionInfo() { Name = "index 9", SemiHidden = true, UnhideWhenUsed = true };
2471-
LatentStyleExceptionInfo latentStyleExceptionInfo20 = new LatentStyleExceptionInfo() { Name = "toc 1", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2472-
LatentStyleExceptionInfo latentStyleExceptionInfo21 = new LatentStyleExceptionInfo() { Name = "toc 2", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2473-
LatentStyleExceptionInfo latentStyleExceptionInfo22 = new LatentStyleExceptionInfo() { Name = "toc 3", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2474-
LatentStyleExceptionInfo latentStyleExceptionInfo23 = new LatentStyleExceptionInfo() { Name = "toc 4", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2475-
LatentStyleExceptionInfo latentStyleExceptionInfo24 = new LatentStyleExceptionInfo() { Name = "toc 5", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2476-
LatentStyleExceptionInfo latentStyleExceptionInfo25 = new LatentStyleExceptionInfo() { Name = "toc 6", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2477-
LatentStyleExceptionInfo latentStyleExceptionInfo26 = new LatentStyleExceptionInfo() { Name = "toc 7", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2478-
LatentStyleExceptionInfo latentStyleExceptionInfo27 = new LatentStyleExceptionInfo() { Name = "toc 8", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
2479-
LatentStyleExceptionInfo latentStyleExceptionInfo28 = new LatentStyleExceptionInfo() { Name = "toc 9", UiPriority = 39, SemiHidden = true, UnhideWhenUsed = true };
24802575
LatentStyleExceptionInfo latentStyleExceptionInfo29 = new LatentStyleExceptionInfo() { Name = "Normal Indent", SemiHidden = true, UnhideWhenUsed = true };
24812576
LatentStyleExceptionInfo latentStyleExceptionInfo30 = new LatentStyleExceptionInfo() { Name = "footnote text", SemiHidden = true, UnhideWhenUsed = true };
24822577
LatentStyleExceptionInfo latentStyleExceptionInfo31 = new LatentStyleExceptionInfo() { Name = "annotation text", SemiHidden = true, UnhideWhenUsed = true };
@@ -2845,15 +2940,6 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
28452940
latentStyles1.Append(latentStyleExceptionInfo17);
28462941
latentStyles1.Append(latentStyleExceptionInfo18);
28472942
latentStyles1.Append(latentStyleExceptionInfo19);
2848-
latentStyles1.Append(latentStyleExceptionInfo20);
2849-
latentStyles1.Append(latentStyleExceptionInfo21);
2850-
latentStyles1.Append(latentStyleExceptionInfo22);
2851-
latentStyles1.Append(latentStyleExceptionInfo23);
2852-
latentStyles1.Append(latentStyleExceptionInfo24);
2853-
latentStyles1.Append(latentStyleExceptionInfo25);
2854-
latentStyles1.Append(latentStyleExceptionInfo26);
2855-
latentStyles1.Append(latentStyleExceptionInfo27);
2856-
latentStyles1.Append(latentStyleExceptionInfo28);
28572943
latentStyles1.Append(latentStyleExceptionInfo29);
28582944
latentStyles1.Append(latentStyleExceptionInfo30);
28592945
latentStyles1.Append(latentStyleExceptionInfo31);
@@ -3211,6 +3297,8 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
32113297
StyleRunProperties styleRunProperties1 = new StyleRunProperties();
32123298
RunFonts runFonts22 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "宋体", ComplexScript = "Times New Roman" };
32133299
Color color1 = new Color() { Val = "000000", ThemeColor = ThemeColorValues.Text1 };
3300+
styleRunProperties1.FontSize = new FontSize { Val = "24" };
3301+
styleRunProperties1.FontSizeComplexScript = new FontSizeComplexScript { Val = "24" };
32143302

32153303
styleRunProperties1.Append(runFonts22);
32163304
styleRunProperties1.Append(color1);
@@ -3493,9 +3581,50 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
34933581
}
34943582
};
34953583

3584+
Style style21 = new Style
3585+
{
3586+
Type = StyleValues.Paragraph,
3587+
StyleId = "TOC 1",
3588+
StyleName = new StyleName { Val = "TOC 1" },
3589+
BasedOn = new BasedOn { Val = "Normal" },
3590+
NextParagraphStyle = new NextParagraphStyle { Val = "Normal" },
3591+
AutoRedefine = new AutoRedefine(),
3592+
UIPriority = new UIPriority { Val = 39 }
3593+
};
3594+
3595+
Style style22 = new Style
3596+
{
3597+
Type = StyleValues.Paragraph,
3598+
StyleId = "TOC 2",
3599+
StyleName = new StyleName { Val = "TOC 2" },
3600+
BasedOn = new BasedOn { Val = "Normal" },
3601+
NextParagraphStyle = new NextParagraphStyle { Val = "Normal" },
3602+
AutoRedefine = new AutoRedefine(),
3603+
UIPriority = new UIPriority { Val = 39 },
3604+
StyleParagraphProperties = new StyleParagraphProperties
3605+
{
3606+
Indentation = new Indentation { Left = "420", LeftChars = 200}
3607+
}
3608+
};
3609+
3610+
Style style23 = new Style
3611+
{
3612+
Type = StyleValues.Paragraph,
3613+
StyleId = "TOC 3",
3614+
StyleName = new StyleName { Val = "TOC 3" },
3615+
BasedOn = new BasedOn { Val = "Normal" },
3616+
NextParagraphStyle = new NextParagraphStyle { Val = "Normal" },
3617+
AutoRedefine = new AutoRedefine(),
3618+
UIPriority = new UIPriority { Val = 39 },
3619+
StyleParagraphProperties = new StyleParagraphProperties
3620+
{
3621+
Indentation = new Indentation { Left = "840", LeftChars = 400 }
3622+
}
3623+
};
3624+
34963625
Style style34 = new Style() { Type = StyleValues.Character, StyleId = "VerbatimChar", CustomStyle = true };
34973626
StyleName styleName34 = new StyleName() { Val = "Verbatim Char" };
3498-
BasedOn basedOn23 = new BasedOn() { Val = "ad" };
3627+
BasedOn basedOn23 = new BasedOn() { Val = "Normal" };
34993628

35003629
StyleRunProperties styleRunProperties21 = new StyleRunProperties();
35013630
RunFonts runFonts18 = new RunFonts() { Ascii = "Consolas", HighAnsi = "Consolas" };
@@ -3534,6 +3663,9 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
35343663
styles1.Append(style18);
35353664
styles1.Append(style19);
35363665
styles1.Append(style20);
3666+
styles1.Append(style21);
3667+
styles1.Append(style22);
3668+
styles1.Append(style23);
35373669
styles1.Append(style34);
35383670
styles1.Append(style44);
35393671

0 commit comments

Comments
 (0)