【发布时间】:2020-12-02 04:52:06
【问题描述】:
我正在开发字体转换器应用程序,它将 Unicode 字体文本转换为 Krutidev/Shree Lipi(马拉地语/印地语)字体文本。在原始 docx 文件中有格式化的单词(即颜色、字体、文本大小、超链接等)。 在将单词从 Unicode 转换为另一种字体后,我想保持最终 docx 的格式与原始 docx 相同。
PFA。
这是我的代码
try {
fileInputStream = new FileInputStream("StartDoc.docx");
document = new XWPFDocument(fileInputStream);
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
List<XWPFParagraph> paragraph = document.getParagraphs();
Converter data = new Converter() ;
for(XWPFParagraph p :document.getParagraphs())
{
for(XWPFRun r :p.getRuns())
{
String string2 = r.getText(0);
data.uniToShree(string2);
r.setText(string2,0);
}
}
//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("Output.docx");
document.write(out);
out.close();
System.out.println("Output.docx written successully");
}
catch (IOException e) {
System.out.println("We had an error while reading the Word Doc");
}
【问题讨论】:
-
在段落中逐个处理它?
-
@Gagravarr 如果我在上面的代码中做错了什么,请指导我。
-
如果你想保持格式,你需要逐段运行而不是逐段工作。像现在一样获取您的段落,然后在这些段落中运行
-
@Gagravarr 我尝试了此代码,但出现错误。 Converter.uniToKrutidev 的空指针异常。 for(XWPFParagraph p :document.getParagraphs()){ for(XWPFRun r :p.getRuns()){ String string2 = r.getText(0);字符串数据a; dataa = Converter.uniToKrutidev(string2); r.setText(dataa,0); } }
-
运行保持格式。如果你想保持格式,你需要坚持替换运行中的文本。至于页面,你的代码肯定还有bug,Word是基于运行而不是基于页面的格式,所以文件格式没有页面变化!
标签: java apache-poi xwpf