我使用的Apache POI提取段落文本颜色,是它可以读取文本的背景色和前景色从MS Word段落如何从MS Word中使用Apache POI
1
A
回答
5
我得到了解决
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
Range range = doc.getRange();
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
System.out.println(pr.getEndOffset());
int j=0;
while (true) {
CharacterRun run = pr.getCharacterRun(j++);
System.out.println("-------------------------------");
System.out.println("Color---"+ run.getColor());
System.out.println("getFontName---"+ run.getFontName());
System.out.println("getFontSize---"+ run.getFontSize());
if(run.getEndOffset()==pr.getEndOffset()){
break;
}
}
}
2
我发现它在:
CharacterRun run = para.getCharacterRun(i)
i
应该是整数,应该增加这样的代码将是如下:
int c=0;
while (true) {
CharacterRun run = para.getCharacterRun(c++);
int x = run.getPicOffset();
System.out.println("pic offset" + x);
if (run.getEndOffset() == para.getEndOffset()) {
break;
}
}
0
if (paragraph != null)
{
int numberOfRuns = paragraph.NumCharacterRuns;
for (int runIndex = 0; runIndex < numberOfRuns; runIndex++)
{
CharacterRun run = paragraph.GetCharacterRun(runIndex);
string color = getColor24(run.GetIco24());
}
}
GetColor24函数进行转换颜色的十六进制格式的C#
public static String getColor24(int argbValue)
{
if (argbValue == -1)
return "";
int bgrValue = argbValue & 0x00FFFFFF;
int rgbValue = (bgrValue & 0x0000FF) << 16 | (bgrValue & 0x00FF00)
| (bgrValue & 0xFF0000) >> 16;
StringBuilder result = new StringBuilder("#");
String hex = rgbValue.ToString("X");
for (int i = hex.Length; i < 6; i++)
{
result.Append('0');
}
result.Append(hex);
return result.ToString();
}
0
如果您正在使用的docx(OOXML),你可能想看看这个:
import java.io.*
import org.apache.poi.xwpf.usermodel.XWPFDocument
fun test(){
try {
val file = File("file.docx")
val fis = FileInputStream(file.absolutePath)
val document = XWPFDocument(fis)
val paragraphs = document.paragraphs
for (para in paragraphs) {
println("-- ("+para.alignment+") " + para.text)
para.runs.forEach { it ->
println(
"text:" + it.text() + " "
+ "(color:" + it.color
+ ",fontFamily:" + it.fontFamily
+ ")"
)
}
}
fis.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
相关问题
- 1. Java:使用apache POI如何将ms word文件转换为pdf?
- 2. 如何使用Apache POI从MS Word文档的文本框中获取文本?
- 3. Apache POI Word教程。
- 4. 如何使用Apache Word POI在word文件中创建Combobox?
- 5. 阅读MS Excel中使用Apache POI
- 6. 如何使用POI语言添加表格MS Word
- 7. 如何使用Apache POI
- 8. 如何使用Apache POI
- 9. 如何使用Apache POI
- 10. 如何使用addMergedRegion apache poi?
- 11. 如何使用Apache POI提取word文档的格式信息?
- 12. 在JAVA中使用Apache POI和iText创建Word(DOC)中的PDF
- 13. 是否可以使用Apache POI解析MS Word并将其转换为XML?
- 14. 使用Apache POI的CTPageSZ类中的错误Java NetBeans Word文档
- 15. 使用Apache POI的Word表格中的单个间距
- 16. 使用Apache Poi解析Word文档中表格中的表格
- 17. 使用Apache POI从Excel中的空白单元读取颜色使用Apache POI
- 18. 使用Apache Felix的Apache POI
- 19. 使用Apache POI标识隐藏文本Word 2003/2007
- 20. 如何阅读来自apache poi的word文档中的注释?
- 21. Apache Poi - 如何删除Word文档中的所有链接
- 22. PatternSyntaxException使用apache poi
- 23. 如何在使用Apache POI的Word .docx文件中正确生成RSID属性?
- 24. 如何使用Apache POI Word中的文本添加内嵌多个方程式?
- 25. 使用Apache的POI
- 26. 添加页脚Word apache poi java
- 27. 从MS Word 2003中转换Word模板MS Word 2007中
- 28. Java:使用POI/HWPF解析ms-word文档
- 29. 如何使用Apache POI从Excel中获取货币代码?
- 30. 如何使用apache poi从.doc文件中提取文本?
要将代码格式化为代码 - 在行首添加4个空格。 – Artemix 2012-11-20 10:45:55