项目组采购了aspose,需要对word的一些字符进行替换操作,替换代码如下
image.png (12.1 KB)
测试发现,替换普通字符是可以的,处理代码中的字符无效。
测试word文本如下:
SQS867概率500指增专享1号私募证券投资基金私募基金合同20210615定稿清洁版.docx (159.5 KB)
无效字符见下图,位于word的第2页
image.jpg (90.2 KB)
为确保及时准确的响应,请在此处附上以下资源进行测试:
- 请附上显示不良行为的输出 Word 文件。
- 请附上显示所需行为的预期输出 Word 文件。
- 请创建一个示例应用程序(没有编译错误的源代码),以帮助我们在我们端重现您的问题,并将其附在此处进行测试。
一旦您准备好这些信息,我们将开始调查您的问题并为您提供更多信息。 谢谢你的配合。
PS:要附上这些资源,请压缩并上传它们。
您想从段落中删除项目符号。 在这种情况下,您可以使用 ListFormat.RemoveNumbers 方法,如下所示。 希望这对你有帮助。
Document doc = new Document(MyDir + "3.docx");
doc.updateListLabels();
for(Paragraph paragraph :(Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if(paragraph.isListItem())
{
if(toHex(paragraph.getListLabel().getLabelString()).equals("ef82a0"))
{
paragraph.getListFormat().removeNumbers();
}
}
}
doc.save(MyDir + "output.docx");
public static String toHex(String arg) {
return String.format("%x", new BigInteger(1, arg.getBytes(StandardCharsets.UTF_8)));
}
谢谢帮忙解决问题,采用以上替换字符操作可以,但引入一个其他问题,替换字符以后,该段落出现了段落不对齐,aspose是否有解决方案,我们期待结构见下图新建 Microsoft Word 文档.docx (97.7 KB)
在这种情况下,您需要获取段落的 ParagraphFormat.LeftIndent 并在删除列表标签后重新设置。
Document doc = new Document(MyDir + "3.docx");
doc.updateListLabels();
for(Paragraph paragraph :(Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
if(paragraph.isListItem())
{
if(toHex(paragraph.getListLabel().getLabelString()).equals("ef82a0"))
{
double indent = paragraph.getParagraphFormat().getLeftIndent();
paragraph.getListFormat().removeNumbers();
paragraph.getParagraphFormat().setLeftIndent(indent);
}
}
}
doc.save(MyDir + "output.docx");
我这边需要的段落对齐应该是首行缩进,调用paragraph.getParagraphFormat.getFirstLineIndent(),没获取到想要的效果
代码如下:
image.png (9.3 KB)
期望的word输出已经在上面的zip包里4.docx中,即第2章去除点号以后,段落要进行首行缩进
请检查第 4 页上要删除项目符号的第一段。 它留下了 7.1 pt 的缩进和 14.2 pt 的悬挂缩进。 请检查所附的输入屏幕截图。
3.docx.png (63.0 KB)
使用以下代码片段删除项目符号并设置悬挂缩进后,输出具有正确的左缩进和悬挂缩进。 请检查附加的输出截图。
output.png (75.4 KB)
double indent = paragraph.getParagraphFormat().getFirstLineIndent();
System.out.println(indent);
paragraph.getListFormat().removeNumbers();
paragraph.getParagraphFormat().setFirstLineIndent(indent);
要获得与 4.docx 中共享的相同输出,您可以使用以下代码片段。
paragraph.getListFormat().removeNumbers();
paragraph.getParagraphFormat().setFirstLineIndent(21.0);
paragraph.getParagraphFormat().setLeftIndent(0.0);