Java poi3.9解析word文档

SHENZHOUCHEN91 2013-06-19 12:15:15
大家好,关于Java poi解析word2003 doc文档资料太少了,希望大家能帮个忙,提供点思路

我想要做一个题库网站,题目源是word文档(那种考试试卷电子版)

1. Java poi图片位置处理
HWPFDocument = new HWPFDocument(new FileInputStream("2011小学数学毕业模拟试卷1.doc"));
List picA = docA.getPicturesTable().getAllPictures();//获取所有文档中的图片

int length = doc.characterLength();
for(int m =0;m<length-1;m++){
Range range = new Range(m,m+1,doc);
for(int j=0;j<range.numCharacterRuns();j++){
CharacterRun cr=range.getCharacterRun(j);
if(cr.getPicOffset()!=-1){} //表明是图片?然后标注明是list中的第几张图片



2. 数学试卷的高等数学公式
解决思路一:poi是能解析成wmf图片(windows 画图板),但是wmf并不是网页图片显示格式
解决思路二:word安装mathtype工具后,可以将word文档中由mathtype公式编辑器编辑的数学公式转化成mathml,然后再解析word文本和mathml的混合文档
但是数学公式在数据库中怎么存储呢?

3. 是否能有其他的工具实现,
1.网上看到c#解析word文档更好?(微软自己的东西)
2.VBA来做?
3.我不知道用itext来解析pdf能否相对简单一点
...全文
5221 29 打赏 收藏 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
喘不过气来 2015-08-17
  • 打赏
  • 举报
回复
楼主,请问具体是想怎么从Word上把公式读出来的呢?
无知常问 2015-01-28
  • 打赏
  • 举报
回复
装逼又不分享,嘲讽别人
花木兰1闪21A 2015-01-17
  • 打赏
  • 举报
回复
引用 26 楼 guoluqiang 的回复:
private List<StartEnd> CutRange(List<StartEnd> fromList, List<StartEnd> cutList) { List<StartEnd> resultList = new List<StartEnd>(); for (int i = 0; i < fromList.Count; i++) { bool SAVE = true; for (int j = 0; j < cutList.Count; j++) { if (fromList[i].Start >= cutList[j].Start && fromList[i].End <= cutList[j].End) { SAVE = false; if(fromList[i].Start == cutList[j].Start) { resultList.Add(cutList[j]); } } } if (SAVE) resultList.Add(fromList[i]); } return resultList; } public string GetSelectionImg(string paperName) { cutTimes = 1; cutTimesCount = 0; const int MAX_height = 3000; //string timeResult = ""; Range range = Globals.ThisAddIn.Application.Selection.Range; Range range2 = Globals.ThisAddIn.Application.Selection.Range; //timeResult = timeResult + "Time1:" + DateTime.Now.ToString() + "\n"; string imgName = Guid.NewGuid().ToString()+".png"; if(!Directory.Exists(Globals.ThisAddIn.exerciseJsonPath+paperName)) { Directory.CreateDirectory(Globals.ThisAddIn.exerciseJsonPath + paperName); } double zoom = 0.33; const int imgWidth = 1188; Image imgTemp = Metafile.FromStream(new MemoryStream(range.EnhMetaFileBits)); //imgTemp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); if (MAX_height < imgTemp.Height) { Paragraphs paragraphs = range.Paragraphs; Tables tables = range.Tables; List<StartEnd> paragraphList = new List<StartEnd>(); List<StartEnd> tableList = new List<StartEnd>(); for(int i=0;i<paragraphs.Count;i++) { Paragraph paragraph=paragraphs[i+1]; StartEnd startEnd = new StartEnd(); startEnd.Start = paragraph.Range.Start; startEnd.End = paragraph.Range.End; paragraphList.Add(startEnd); } for(int i=0;i<tables.Count;i++) { Table table=tables[i+1]; StartEnd startEnd = new StartEnd(); startEnd.Start = table.Range.Start; startEnd.End = table.Range.End; tableList.Add(startEnd); } List<StartEnd> resultList = CutRange(paragraphList,tableList); List<StartEnd> finalImgRangeList = new List<StartEnd>(); for (int i = 0; i < resultList.Count;i++ ) { StartEnd startendImg = new StartEnd(); startendImg.Start = resultList[i].Start; startendImg.End = resultList[i].End; for(int j=i;j<resultList.Count;j++) { range2.SetRange((int)resultList[i].Start,(int)resultList[j].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if(img.Height<MAX_height) { startendImg.End = resultList[j].End; if (j == resultList.Count - 1) i = j; } else { if(i==j) { MessageBox.Show("请确定没有超过一页的段落或表格"); Globals.ThisAddIn.Application.ActiveWindow.ScrollIntoView(range2); return ""; } i = j-1; break; } } finalImgRangeList.Add(startendImg); } cutTimes = finalImgRangeList.Count; //timeResult = timeResult + "Time2:" + DateTime.Now.ToString() + "\n"; int allImgHeight = 0; int allImgWidth = 0; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start,(int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if (img.Width > allImgWidth) allImgWidth = img.Width; allImgHeight += img.Height; } //for(int i=0;i<resultList.Count;i++) //{ // int start=(int)resultList[i].Start; // int end=(int)resultList[i].End; // range2.SetRange(start,end); // if (i % 2 == 0) // range2.HighlightColorIndex = WdColorIndex.wdRed; // else // range2.HighlightColorIndex = WdColorIndex.wdYellow; //} //zoom = (double)imgWidth / (double)allImgWidth; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(allImgHeight * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; int startPosition = 0; double oldZoom = zoom; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start, (int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if ((double)imgWidth / (double)img.Width < zoom) zoom = (double)imgWidth / (double)img.Width; gx.FillRectangle(new SolidBrush(System.Drawing.Color.Transparent), 0, startPosition, (int)(img.Width * zoom), (int)(img.Height * zoom)); gx.DrawImage(img, new System.Drawing.Rectangle(0, startPosition, (int)(img.Width * zoom), (int)(img.Height*zoom))); startPosition += (int)(img.Height * zoom); zoom = oldZoom; cutTimesCount = i + 1; } //bmp = KiSharpen(bmp,(float)0.3); bmp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); } else { //zoom = (double)imgWidth / (double)imgTemp.Width; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(imgTemp.Height * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; gx.FillRectangle(new SolidBrush(System.Drawing.Color.Transparent), 0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom)); gx.DrawImage(imgTemp, new System.Drawing.Rectangle(0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom))); //imgTemp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); bmp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); } //timeResult = timeResult + "Time3:" + DateTime.Now.ToString() + "\n"; cutTimes = 1; cutTimesCount = 0; //MessageBox.Show(timeResult); return imgName; }
很明显你这不是跨平台的
guoluqiang 2014-07-29
  • 打赏
  • 举报
回复
private List<StartEnd> CutRange(List<StartEnd> fromList, List<StartEnd> cutList) { List<StartEnd> resultList = new List<StartEnd>(); for (int i = 0; i < fromList.Count; i++) { bool SAVE = true; for (int j = 0; j < cutList.Count; j++) { if (fromList[i].Start >= cutList[j].Start && fromList[i].End <= cutList[j].End) { SAVE = false; if(fromList[i].Start == cutList[j].Start) { resultList.Add(cutList[j]); } } } if (SAVE) resultList.Add(fromList[i]); } return resultList; } public string GetSelectionImg(string paperName) { cutTimes = 1; cutTimesCount = 0; const int MAX_height = 3000; //string timeResult = ""; Range range = Globals.ThisAddIn.Application.Selection.Range; Range range2 = Globals.ThisAddIn.Application.Selection.Range; //timeResult = timeResult + "Time1:" + DateTime.Now.ToString() + "\n"; string imgName = Guid.NewGuid().ToString()+".png"; if(!Directory.Exists(Globals.ThisAddIn.exerciseJsonPath+paperName)) { Directory.CreateDirectory(Globals.ThisAddIn.exerciseJsonPath + paperName); } double zoom = 0.33; const int imgWidth = 1188; Image imgTemp = Metafile.FromStream(new MemoryStream(range.EnhMetaFileBits)); //imgTemp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); if (MAX_height < imgTemp.Height) { Paragraphs paragraphs = range.Paragraphs; Tables tables = range.Tables; List<StartEnd> paragraphList = new List<StartEnd>(); List<StartEnd> tableList = new List<StartEnd>(); for(int i=0;i<paragraphs.Count;i++) { Paragraph paragraph=paragraphs[i+1]; StartEnd startEnd = new StartEnd(); startEnd.Start = paragraph.Range.Start; startEnd.End = paragraph.Range.End; paragraphList.Add(startEnd); } for(int i=0;i<tables.Count;i++) { Table table=tables[i+1]; StartEnd startEnd = new StartEnd(); startEnd.Start = table.Range.Start; startEnd.End = table.Range.End; tableList.Add(startEnd); } List<StartEnd> resultList = CutRange(paragraphList,tableList); List<StartEnd> finalImgRangeList = new List<StartEnd>(); for (int i = 0; i < resultList.Count;i++ ) { StartEnd startendImg = new StartEnd(); startendImg.Start = resultList[i].Start; startendImg.End = resultList[i].End; for(int j=i;j<resultList.Count;j++) { range2.SetRange((int)resultList[i].Start,(int)resultList[j].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if(img.Height<MAX_height) { startendImg.End = resultList[j].End; if (j == resultList.Count - 1) i = j; } else { if(i==j) { MessageBox.Show("请确定没有超过一页的段落或表格"); Globals.ThisAddIn.Application.ActiveWindow.ScrollIntoView(range2); return ""; } i = j-1; break; } } finalImgRangeList.Add(startendImg); } cutTimes = finalImgRangeList.Count; //timeResult = timeResult + "Time2:" + DateTime.Now.ToString() + "\n"; int allImgHeight = 0; int allImgWidth = 0; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start,(int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if (img.Width > allImgWidth) allImgWidth = img.Width; allImgHeight += img.Height; } //for(int i=0;i<resultList.Count;i++) //{ // int start=(int)resultList[i].Start; // int end=(int)resultList[i].End; // range2.SetRange(start,end); // if (i % 2 == 0) // range2.HighlightColorIndex = WdColorIndex.wdRed; // else // range2.HighlightColorIndex = WdColorIndex.wdYellow; //} //zoom = (double)imgWidth / (double)allImgWidth; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(allImgHeight * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; int startPosition = 0; double oldZoom = zoom; for (int i = 0; i < finalImgRangeList.Count; i++) { range2.SetRange((int)finalImgRangeList[i].Start, (int)finalImgRangeList[i].End); Image img = Metafile.FromStream(new MemoryStream(range2.EnhMetaFileBits)); if ((double)imgWidth / (double)img.Width < zoom) zoom = (double)imgWidth / (double)img.Width; gx.FillRectangle(new SolidBrush(System.Drawing.Color.Transparent), 0, startPosition, (int)(img.Width * zoom), (int)(img.Height * zoom)); gx.DrawImage(img, new System.Drawing.Rectangle(0, startPosition, (int)(img.Width * zoom), (int)(img.Height*zoom))); startPosition += (int)(img.Height * zoom); zoom = oldZoom; cutTimesCount = i + 1; } //bmp = KiSharpen(bmp,(float)0.3); bmp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); } else { //zoom = (double)imgWidth / (double)imgTemp.Width; System.Drawing.Bitmap bmp = new Bitmap(imgWidth, (int)(imgTemp.Height * zoom)); System.Drawing.Graphics gx = System.Drawing.Graphics.FromImage(bmp); // 创建Graphics对象 gx.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 gx.SmoothingMode = SmoothingMode.HighQuality; gx.CompositingQuality = CompositingQuality.HighQuality; gx.CompositingMode = CompositingMode.SourceOver; gx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; gx.FillRectangle(new SolidBrush(System.Drawing.Color.Transparent), 0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom)); gx.DrawImage(imgTemp, new System.Drawing.Rectangle(0, 0, (int)(imgTemp.Width * zoom), (int)(imgTemp.Height * zoom))); //imgTemp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); bmp.Save(Globals.ThisAddIn.exerciseJsonPath + paperName + "\\" + imgName, System.Drawing.Imaging.ImageFormat.Png); } //timeResult = timeResult + "Time3:" + DateTime.Now.ToString() + "\n"; cutTimes = 1; cutTimesCount = 0; //MessageBox.Show(timeResult); return imgName; }
白狐baby 2014-07-17
  • 打赏
  • 举报
回复
引用 22 楼 chen6013143 的回复:
求demo 只给一段代码搞不好
大神请问你弄好了吗,求帮助
白狐baby 2014-07-17
  • 打赏
  • 举报
回复
引用 16 楼 guoluqiang 的回复:
[quote=引用 14 楼 tlfu_12344 的回复:] 问题已经解决:
怎么解决的?大神快赐教!!!!最近急需这个功能[/quote] 怎么解决的呀,求帮助
「已注销」 2014-05-07
  • 打赏
  • 举报
回复
求demo 只给一段代码搞不好
记忆向前看 2014-03-25
  • 打赏
  • 举报
回复
的确mathjax很不错
花木兰1闪21A 2014-02-27
  • 打赏
  • 举报
回复
引用 19 楼 gangzai626919 的回复:
显然是把公式截成图片了啊
显然你不懂mathml.ooxml!
记忆向前看 2014-02-27
  • 打赏
  • 举报
回复
显然是把公式截成图片了啊
迷失lost 2014-01-09
  • 打赏
  • 举报
回复
引用 14 楼 tlfu_12344 的回复:
问题已经解决:
怎么解决的?
guoluqiang 2013-12-23
  • 打赏
  • 举报
回复
引用 14 楼 tlfu_12344 的回复:
问题已经解决:
联系方式Email:2213429531@qq.com
guoluqiang 2013-12-23
  • 打赏
  • 举报
回复
引用 14 楼 tlfu_12344 的回复:
问题已经解决:
怎么解决的?大神快赐教!!!!最近急需这个功能
seagod_wong 2013-11-08
  • 打赏
  • 举报
回复
楼主能详细说一下解决过程吗 我最近需要做这个东西。
花木兰1闪21A 2013-11-01
  • 打赏
  • 举报
回复
问题已经解决:
花木兰1闪21A 2013-10-29
  • 打赏
  • 举报
回复
引用 12 楼 SHENZHOUCHEN91 的回复:
[quote=引用 11 楼 tlfu_12344 的回复:] 关于解析word中公式你解决没,之前看有说是用VBA,可是我们是要求可跨平台的,这下伤不起了
后来因为没有实现,这个不了了之了!不知道现在有什么实现思路了吗? [/quote]嗯,有思路了,正在做!,用openoffice解析,对于docx文件,要自己改里面的配置文件
SHENZHOUCHEN91 2013-10-29
  • 打赏
  • 举报
回复
引用 11 楼 tlfu_12344 的回复:
关于解析word中公式你解决没,之前看有说是用VBA,可是我们是要求可跨平台的,这下伤不起了
后来因为没有实现,这个不了了之了!不知道现在有什么实现思路了吗?
花木兰1闪21A 2013-10-22
  • 打赏
  • 举报
回复
关于解析word中公式你解决没,之前看有说是用VBA,可是我们是要求可跨平台的,这下伤不起了
失落夏天 2013-10-17
  • 打赏
  • 举报
回复
第一问,也许资料少,但是官方API里肯定有例子,下载后的POI方法中也有demo。照着学,挨个测试一下效果。 第二问,数学公式中的任何符号都有对应的ASSIC编码吧,存储编码数字就可以了
花木兰1闪21A 2013-10-17
  • 打赏
  • 举报
回复
用openoffice看看,是跨平台的,word中的公司是作为图片处理的
加载更多回复(8)

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧