C# NPOI 列表编号与段落的样式不同
C# NPOI list numbering gets a different style from paragraphs
我制作了一个带有自定义编号的列表,但编号本身与段落 运行 的样式不同。如何使编号与段落 XWPFRun 具有相同的样式?
这是我的编号
XWPFNumbering numbering = document.CreateNumbering();
var ct_abn = new CT_AbstractNum();
var mlt = new CT_MultiLevelType();
mlt.val = ST_MultiLevelType.multilevel;
ct_abn.multiLevelType = mlt;
ct_abn.lvl = new System.Collections.Generic.List<CT_Lvl> {
new CT_Lvl {
ilvl = "0", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
pPr = new CT_PPr {ind = new CT_Ind {left = "360", hanging = 360}, }
},
new CT_Lvl {
ilvl = "1", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1.%2."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
pPr = new CT_PPr {ind = new CT_Ind {left = "792", hanging = 792, firstLineSpecified = true}, }
},
new CT_Lvl {
ilvl = "2", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1.%2.%3."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
pPr = new CT_PPr {ind = new CT_Ind {left = "792", hanging = 792}}
},
}
它是这样使用的:
var paragraph = document.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.SetNumID(numId, "0"); // applying numbering with lvl = 0
var run = paragraph.CreateRun();
run.SetText("ТЕРМІНИ, ЩО ВЖИВАЮТЬСЯ В ГАРАНТІЙНИХ УМОВАХ");
run.IsBold = true;
run.FontFamily = "Times New Roman";
run.FontSize = 12;
这就是我得到的:段落本身有正确的样式,但数字有不同
如有任何帮助,我将不胜感激
如果有人有同样的问题,您应该将您的新样式添加到文档中,而不是像这样将其 StyleID 用于 XWPFParagraph:
CT_Fonts ctFonts = new CT_Fonts();
ctFonts.ascii = "Times New Roman";
var styles = document.CreateStyles();
styles.AddStyle(
new XWPFStyle(
new CT_Style() {
styleId = "FirstLvlStyle",
rPr = new CT_RPr() {rFonts = ctFonts, b = new CT_OnOff() {val = true}, szCs = new CT_HpsMeasure() {val = 24}}, // gives val / 2 font size
})
);
var paragraph = document.CreateParagraph();
paragraph.Style = "FirstLvlStyle"; // use your StyleId to apply your style
paragraph.SetNumID(numId, "0"); // applying numbering with lvl = 0
已更新
或者您可以在编号配置 pPr(段落属性)或 rPr(运行 属性)中指定您想要的样式,如下所示:
var ct_abn = new CT_AbstractNum();
var mlt = new CT_MultiLevelType();
mlt.val = ST_MultiLevelType.multilevel;
ct_abn.multiLevelType = mlt;
ct_abn.lvl = new System.Collections.Generic.List<CT_Lvl> {
new CT_Lvl {
ilvl = "0", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
rPr = new CT_RPr {b = new CT_OnOff {val = true}}, //make text bold
pPr = new CT_PPr {ind = new CT_Ind {left = "360", hanging = 360}}
},
}
我制作了一个带有自定义编号的列表,但编号本身与段落 运行 的样式不同。如何使编号与段落 XWPFRun 具有相同的样式?
这是我的编号
XWPFNumbering numbering = document.CreateNumbering();
var ct_abn = new CT_AbstractNum();
var mlt = new CT_MultiLevelType();
mlt.val = ST_MultiLevelType.multilevel;
ct_abn.multiLevelType = mlt;
ct_abn.lvl = new System.Collections.Generic.List<CT_Lvl> {
new CT_Lvl {
ilvl = "0", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
pPr = new CT_PPr {ind = new CT_Ind {left = "360", hanging = 360}, }
},
new CT_Lvl {
ilvl = "1", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1.%2."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
pPr = new CT_PPr {ind = new CT_Ind {left = "792", hanging = 792, firstLineSpecified = true}, }
},
new CT_Lvl {
ilvl = "2", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1.%2.%3."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
pPr = new CT_PPr {ind = new CT_Ind {left = "792", hanging = 792}}
},
}
它是这样使用的:
var paragraph = document.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.SetNumID(numId, "0"); // applying numbering with lvl = 0
var run = paragraph.CreateRun();
run.SetText("ТЕРМІНИ, ЩО ВЖИВАЮТЬСЯ В ГАРАНТІЙНИХ УМОВАХ");
run.IsBold = true;
run.FontFamily = "Times New Roman";
run.FontSize = 12;
这就是我得到的:段落本身有正确的样式,但数字有不同
如有任何帮助,我将不胜感激
如果有人有同样的问题,您应该将您的新样式添加到文档中,而不是像这样将其 StyleID 用于 XWPFParagraph:
CT_Fonts ctFonts = new CT_Fonts();
ctFonts.ascii = "Times New Roman";
var styles = document.CreateStyles();
styles.AddStyle(
new XWPFStyle(
new CT_Style() {
styleId = "FirstLvlStyle",
rPr = new CT_RPr() {rFonts = ctFonts, b = new CT_OnOff() {val = true}, szCs = new CT_HpsMeasure() {val = 24}}, // gives val / 2 font size
})
);
var paragraph = document.CreateParagraph();
paragraph.Style = "FirstLvlStyle"; // use your StyleId to apply your style
paragraph.SetNumID(numId, "0"); // applying numbering with lvl = 0
已更新
或者您可以在编号配置 pPr(段落属性)或 rPr(运行 属性)中指定您想要的样式,如下所示:
var ct_abn = new CT_AbstractNum();
var mlt = new CT_MultiLevelType();
mlt.val = ST_MultiLevelType.multilevel;
ct_abn.multiLevelType = mlt;
ct_abn.lvl = new System.Collections.Generic.List<CT_Lvl> {
new CT_Lvl {
ilvl = "0", start = new CT_DecimalNumber() {val = "1"}, numFmt = new CT_NumFmt() {val = ST_NumberFormat.@decimal},
lvlText = new CT_LevelText() {val = "%1."}, lvlJc = new CT_Jc() {val = ST_Jc.left},
rPr = new CT_RPr {b = new CT_OnOff {val = true}}, //make text bold
pPr = new CT_PPr {ind = new CT_Ind {left = "360", hanging = 360}}
},
}