实现术语内容的打印

aspose for java 22.1.0
现有两段文字paraA 和 paraB,现在需要通过代码将两端文字写入到word 中,具体样式如下:
paraA…paraB
两端文字长度不固定,中间的点动态调整,类似文档目录的展现形式。
image.png (5.4 KB)

Please!
Thanks!

@wangyan 请检查以下代码:

Document doc = new Document("Document.docx");

// Get paragraphs you need from the document.
Paragraph p1 = doc.getFirstSection().getBody().getParagraphs().get(0);
Paragraph p2 = doc.getFirstSection().getBody().getParagraphs().get(1);

p1.appendChild(new Run(doc, "..."));
// Copy all nodes from one paragraph to another.
while (p2.hasChildNodes())
    p1.appendChild(p2.getFirstChild());
// Remove an empty paragraph.
p2.remove();

doc.save("Document.docx");

@wangyan 我看到了图片,我认为您需要为文档创建一个内容表。请查看此链接

通过查看相关资料,实际上我是想要通过word 中的制表位来实现我想要的效果,并不是通过目录来实现,具体效果如下图,
image.png (8.7 KB)
所以如何使用aspose 来实现制表位样式的设置,请提供对应的实例代码!

@wangyan 谢谢您的澄清。我认为最好的办法是用 TabStop 定义自定义样式并将其添加到段落中:

Document doc = new Document("Insert.docx");

Style tabStopStyle = doc.getStyles().add(StyleType.PARAGRAPH, "TabStopStyle");
// 72 点在 Microsoft Word 的制表符停止标尺上是一 "英寸"。
tabStopStyle.getParagraphFormat().getTabStops().add(72, TabAlignment.LEFT, TabLeader.DOTS);

// 从文件中获取所需的段落。
Paragraph p1 = doc.getFirstSection().getBody().getParagraphs().get(0);
Paragraph p2 = doc.getFirstSection().getBody().getParagraphs().get(1);

p1.appendChild(new Run(doc, ControlChar.TAB));
// 将一个段落中的所有节点复制到另一个段落中。
while (p2.hasChildNodes())
    p1.appendChild(p2.getFirstChild());

p1.getParagraphFormat().setStyle(tabStopStyle);

doc.save("Out.docx");

image.png (3.4 KB)

我想让这个位置自动适配整个word 文档的宽度,如何设置?

@wangyan 请使用此代码:

Document doc = new Document("Insert.docx");

Paragraph p1 = doc.getFirstSection().getBody().getParagraphs().get(0);
Paragraph p2 = doc.getFirstSection().getBody().getParagraphs().get(1);

double tabStopPosition = doc.getFirstSection().getPageSetup().getPageWidth();

Style tabStopStyle = doc.getStyles().add(StyleType.PARAGRAPH, "TabStopStyle");
tabStopStyle.getParagraphFormat().getTabStops().add(tabStopPosition, TabAlignment.RIGHT, TabLeader.DOTS);

p1.appendChild(new Run(doc, ControlChar.TAB));
while (p2.hasChildNodes())
    p1.appendChild(p2.getFirstChild());

p1.getParagraphFormat().setStyle(tabStopStyle);

doc.save("Out.docx");

不好意思,可能给您没说清楚,具体是两边文本加上中间的制表位,能合理铺满文本内容宽度,因为根据您上面的方法,右侧内容已经超出word的正常宽度;具体如图所示:
image.png (10.7 KB)
确保内容在两边的页边距之内!!! Thanks!

@wangyan 将 tabStopPosition 计算改为

PageSetup pageSetup = doc.getFirstSection().getPageSetup();
double tabStopPosition = pageSetup.getPageWidth() - pageSetup.getLeftMargin() - pageSetup.getRightMargin();

但为了避免出错,您可以提供测试代码和 word 文档。