ouchli
April 15, 2025, 2:25am
1
源文档:
aa.docx (39.8 KB)
这个源文档中有一个段落是:page break 1 引言
请问怎么处理可以将 page break 和 1 引言 分开为两个段落
我的处理:这样的处理会导致新增一个空标题
DocumentBuilder builder = new DocumentBuilder(document);
RunCollection runs = para.getRuns();
Arrays.stream(runs.toArray()).filter(run → run.getText().contains(ControlChar.PAGE_BREAK)).forEach(run → {
builder.moveTo(run);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
});
@ouchli 由于分页符是段落 ParagraphFormat.PageBreakBefore 或特殊字符 ControlChar.PageBreak 的格式属性,因此它不能出现在单独的段落中。 要模仿 MS Word 的行为,应使用以下代码来实现结果:
Arrays.stream(runs.toArray()).filter(run -> run.getText().contains(ControlChar.PAGE_BREAK)).forEach(run -> {
Node nextRun = run.getNextSibling();
while (nextRun.getNodeType() != NodeType.RUN) {
nextRun = nextRun.getNextSibling();
}
nextRun.getParentNode().insertBefore(new Run(doc, ControlChar.PAGE_BREAK), nextRun);
});
ouchli
April 18, 2025, 8:05am
3
你好,请问我如何为一个文档的第一页之前插入一个空白页,并在这个空白页中添加一个段落
@ouchli 您可以创建一个部分,并将其添加为首页:
Document doc = new Document("input.docx");
Section newFirstSection = new Section(doc);
newFirstSection.ensureMinimum();
doc.insertBefore(newFirstSection, doc.getFirstSection());
newFirstSection.getBody().appendParagraph("This is the new first page content");
doc.save("output.docx");
但我认为最简单的方法是创建一个包含段落的文档,然后附加另一个文档。
@ouchli 代码如下:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder();
builder.writeln("Simple paragraph");
Document newDoc = new Document("input.docx");
doc.appendDocument(newDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc.save("output.docx");
ouchli
June 20, 2025, 9:12am
7
你好,请问如何更新目录页码呢
我使用这个方法没有效果
document.updateFields();
document.updateListLabels();
ouchli
June 21, 2025, 3:14am
9
public static void uptPageNumbers(Document document) {
FieldCollection fields = document.getRange().getFields();
for (Field field : fields) {
if (FieldType.FIELD_TOC==field.getType()){
FieldToc toc = (FieldToc) field;
try {
toc.updatePageNumbers();
return;
} catch (Exception e) {
throw new BusinessException("目录页码更新失败:"+e.getMessage());
}
}
}
}
我用如下的方式发现也没有更新页码
ouchli
June 23, 2025, 12:47am
11
文件因为保密性不能上传,但是我测试发现有的文件可以正常刷新目录,有的不行,能否提供一些思路,哪些情况下调用这个方法并不会更新页码,我在本地验证一下,谢谢~
@ouchli 通常,大多数非 Aspose.Words 问题都可能与字体问题有关。 您可以尝试使用 IWarningCallback 或 MS Word 选项进行检查:
此外,类似的问题还可能与 WPS 和 MS Word 创建的文档显示结果不同有关。
在某些情况下,必须设置语言才能获得正确的结果:
LoadOptions loadOptions = new LoadOptions();
loadOptions.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.CHINESE_PRC);
如果 MS Word 2019 或以上版本显示文档在兼容模式下打开,您可以将此文档转换为最新格式,或尝试通过 doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010); 在 Aspose.Words 中更改兼容性设置:
ouchli
July 3, 2025, 2:17am
13
您好,如上图所示,请问如何获取到页面设置中每页的最大行数呢,谢谢~
pageSetup.getLinesPerPage()只能获取当前设置的行数
@ouchli 遗憾的是,没有办法获得这个最大值。 您可以尝试使用以下代码:
PageSetup pageSetup = doc.getFirstSection().getPageSetup();
// Get Normal style's font size (default is 11pt in Word)
double normalFontSize = doc.getStyles().getByStyleIdentifier(StyleIdentifier.NORMAL).getFont().getSize();
// Calculate available page height (in points) after subtracting margins
double pageHeight = pageSetup.getPageHeight() - pageSetup.getTopMargin() - pageSetup.getBottomMargin();
// Determine line pitch (spacing between lines)
double linePitch;
if (pageSetup.getLayoutMode() == SectionLayoutMode.GRID && pageSetup.getLinesPerPage() > 0) {
// If Document Grid is enabled, use its line pitch
linePitch = pageHeight / pageSetup.getLinesPerPage();
} else {
// Default to 1.5× font size (Word's default line spacing)
linePitch = normalFontSize * 1.5;
}
// Ensure line pitch respects minimum (136% of font size)
double minLinePitch = normalFontSize * 1.36;
if (linePitch < minLinePitch) {
linePitch = minLinePitch;
}
// Calculate max lines per page (floor to avoid partial lines)
int maxLinesPerPage = (int) Math.floor(pageHeight / linePitch);
System.out.println("Calculated max lines per page: " + maxLinesPerPage);
System.out.println("Line pitch used: " + linePitch + " points (" + (linePitch / normalFontSize) + "× font size)");