aspose-words / Aspose.Words-for-Java

Aspose.Words for Java examples, plugins and showcases
https://products.aspose.com/words/java
MIT License
401 stars 206 forks source link

如何插入word中表格或者图片的题注,就表序号和图的序号(How to insert the table or picture caption in the word, the table number and the number of the figure) #106

Open jeremyliu17 opened 4 months ago

AlexNosk commented 4 months ago

@jeremyliu17 图片或表格阳离子作为简单文本和带有适当标签的 SEQ 字段插入。 所以你可以使用这样的代码:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.write("Table ");
builder.insertField("SEQ Table");

doc.save("C:\\Temp\\out.docx");
jeremyliu17 commented 4 months ago

@jeremyliu17 图片或表格阳离子作为简单文本和带有适当标签的 SEQ 字段插入。 所以你可以使用这样的代码:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.write("Table ");
builder.insertField("SEQ Table");

doc.save("C:\\Temp\\out.docx");

非常感谢,这样能够生成表1,表2...连续的序列,如果想生成这种形式表1-1,表1-2,第一个1表示跟随章节标题去变化,第二个数字为表的顺序,这种有好的办法解决吗 企业微信截图_20240411142646

AlexNosk commented 4 months ago

@jeremyliu17 如果可能的话,您能否使用 MS Word 创建预期的输出并附在此处供我们参考? 通过截图很难看出文档中使用了哪些字段。

jeremyliu17 commented 4 months ago

期望输出.docx

@jeremyliu17 如果可能的话,您能否使用 MS Word 创建预期的输出并附在此处供我们参考? 通过截图很难看出文档中使用了哪些字段。

期望输出.docx 最终想生成一个这样的文档,不同测试类型的标题,和里面的表标题及表内容,希望实现的效果,我加在文档批注中了,希望得到您的回复

AlexNosk commented 4 months ago

@jeremyliu17 感谢您提供更多信息。 在表格标题中有“STYLEREF”和“SEQ”字段: image

因此,您应该插入两者以获得所需的输出:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1).getListFormat().applyNumberDefault();
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("This is some styled paragraph");
builder.getParagraphFormat().clearFormatting();

builder.write("Table ");
builder.insertField("STYLEREF \"Heading 1\" \\s");
builder.write("-");
builder.insertField("SEQ Table");
builder.write(" Some table caption");

doc.save("C:\\Temp\\out.docx");