Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何插入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 this issue Apr 11, 2024 · 5 comments

Comments

@jeremyliu17
Copy link

No description provided.

@jeremyliu17 jeremyliu17 changed the title 如何插入word中表格或者图片的题注,就表序号和图的序号(How to insert the table or picture in the word, the table number and the number of the figure) 如何插入word中表格或者图片的题注,就表序号和图的序号(How to insert the table or picture caption in the word, the table number and the number of the figure) Apr 11, 2024
@AlexNosk
Copy link

@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
Copy link
Author

@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
Copy link

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

@jeremyliu17
Copy link
Author

期望输出.docx

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

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

@AlexNosk
Copy link

@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");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants