导航:首页 > 文字图片 > java读取word文件图片

java读取word文件图片

发布时间:2022-12-27 09:47:10

A. java 中用poi读取word和用docx4j读取word

不知道你是具体读取Word里面的什么元素,下面以读取文字和图片为例吧,两个代码示例,你参考看看:

  1. 读取文本

import com.spire.doc.Document;

import java.io.FileWriter;

import java.io.IOException;

public class ExtractText {

public static void main(String[] args) throws IOException {

//加载Word文档
Document document = new Document();
document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");

//获取文档中的文本保存为String
String text=document.getText();

//将String写入Txt文件
writeStringToTxt(text,"ExtractedText.txt");
}

public static void writeStringToTxt(String content, String txtFileName) throws IOException {

FileWriter fWriter= new FileWriter(txtFileName,true);
try {
fWriter.write(content);
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

}


2. 读取图片


import com.spire.doc.Document;

import com.spire.doc.documents.DocumentObjectType;

import com.spire.doc.fields.DocPicture;

import com.spire.doc.interfaces.ICompositeObject;

import com.spire.doc.interfaces.IDocumentObject;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

import java.util.Queue;

public class ExtractImages {

public static void main(String[] args) throws IOException {

//加载Word文档

Document document = new Document();

document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");

//创建Queue对象

Queue nodes = new LinkedList();

nodes.add(document);

//创建List对象

List images = new ArrayList();

//遍历文档中的子对象

while (nodes.size() > 0) {

ICompositeObject node = nodes.poll();

for (int i = 0; i < node.getChildObjects().getCount(); i++) {

IDocumentObject child = node.getChildObjects().get(i);

if (child instanceof ICompositeObject) {

nodes.add((ICompositeObject) child);

//获取图片并添加到List

if (child.getDocumentObjectType() == DocumentObjectType.Picture) {

DocPicture picture = (DocPicture) child;

images.add(picture.getImage());

}

}

}

}

//将图片保存为PNG格式文件

for (int i = 0; i < images.size(); i++) {

File file = new File(String.format("output/图片-%d.png", i));

ImageIO.write(images.get(i), "PNG", file);

}

}

}

注意这里使用的jar包是spire.doc.jar,需要在java程序中先导入jar文件。

B. JAVA编辑WORD文件插入图片

试试这个代码,需要添加spire.doc jar依赖

importcom.spire.doc.Document;
importcom.spire.doc.FileFormat;
importcom.spire.doc.Section;
importcom.spire.doc.documents.*;
importcom.spire.doc.fields.DocPicture;

publicclassInsertImage{

publicstaticvoidmain(String[]args){

//实例化Document对象
Documentdoc=newDocument();
//加载文档
doc.loadFromFile("C:\Users\Administrator\Desktop\test.docx");
//获取第一个section
Sectionsection=doc.getSections().get(0);
//添加一个段落
Paragraphpara=section.addParagraph();
//添加图片到段落
DocPicturepicture=para.appendPicture("C:\Users\Administrator\Desktop\Cartoon.png");
//设置文字环绕方式(居于文字上方)
picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
//指定图片的相对位置
picture.setHorizontalOrigin(HorizontalOrigin.Page);
picture.setHorizontalPosition(250f);
picture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
picture.setVerticalPosition(150f);
//设置图片大小
picture.setWidth(80f);
picture.setHeight(80f);
//保存到文档
doc.saveToFile("output/InsertImage.docx",FileFormat.Docx);
}
}

生成的Word:

C. 用java怎么将word文档转成图片格式

使用 java 操作 openoffice 实现格式转换 辛苦了几天。 不敢独享成果

首先,要安装 openoffice (废话- -)
创建JAVA项目的时候 需要至少以下4个包

juh.jar,jurt.jar,ridl.jar,unoil.jar
以下是路径
..\OpenOffice.org 2.3\program\classes

我只实现了我要的功能,就是把一种文件格式转成另一种

Java代码
package testh;

import java.io.*;
import com.sun.star.uno.UnoRuntime;

public class testcls {

public static void readdoc(String paths, String savepaths)
{
File d = new File(paths);
//取得当前文件夹下所有文件和目录的列表
File lists[] = d.listFiles();
String pathss = new String("");

//对当前目录下面所有文件进行检索
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
//取得文件类型
filetype = filename.substring((filename.length() - 3), filename.length());

//判断是否为doc文件
if(filetype.equals("doc"))
{
System.out.println("当前正在检索....");
//打印当前目录路径
System.out.println(paths);
//打印doc文件名
String fname=filename.substring(0, (filename.length() - 4));
System.out.println("检索到文件"+fname);
try
{
//指定文件路径和名称
String path = savepaths+fname+".html";
File outfilename = new File(path);

/** *//**
* 检查文件是否存在.
* @throws IOException
*
*/

if (!outfilename.exists()) {
System.err.println("目标路径无同名文件,开始转换");

System.out.print("正在转换文件:"+fname);
Dump(paths,fname,savepaths);

}
else
{
System.out.print("文件已存在,放弃创建,请处理存在文件后再运行...\n");
continue;
}

RandomAccessFile mm = null;

}
catch(Exception e)
{
e.printStackTrace();
}
}

}

}

}

public static void Dump(String FilePath,String FileName,String OutPath)
{
com.sun.star.uno.XComponentContext xContext = null;

try {
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
System.out.println("Connected to a running office ...");

// get the remote office service manager
com.sun.star.lang.XMultiComponentFactory xMCF =
xContext.getServiceManager();

Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);

com.sun.star.frame.XComponentLoader xCompLoader =
(com.sun.star.frame.XComponentLoader)
UnoRuntime.queryInterface(
com.sun.star.frame.XComponentLoader.class, oDesktop);

java.io.File sourceFile = new java.io.File(FilePath+FileName+".doc");//读取的文件
StringBuffer sLoadUrl = new StringBuffer("file:///");
sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));

sourceFile = new java.io.File(OutPath+FileName+".html");//输出的文件
System.out.print(OutPath+" "+FileName);
StringBuffer sSaveUrl = new StringBuffer("file:///");
sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));

com.sun.star.beans.PropertyValue[] propertyValue =
new com.sun.star.beans.PropertyValue[1];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Hidden";
propertyValue[0].Value = new Boolean(true);

Object oDocToStore = xCompLoader.loadComponentFromURL(
sLoadUrl.toString(), "_blank", 0, propertyValue );
com.sun.star.frame.XStorable xStorable =
(com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
com.sun.star.frame.XStorable.class, oDocToStore );

propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Overwrite";
propertyValue[0].Value = new Boolean(true);
propertyValue[1] = new com.sun.star.beans.PropertyValue();
propertyValue[1].Name = "FilterName";
propertyValue[1].Value = "HTML (StarWriter)";//你一定发现了,把这里改成其他参数,可以保存为不同的文件 MS Word 97,writer_pdf_Export
xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );

System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
sSaveUrl + "\"\n");

com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
oDocToStore );

if (xCloseable != null ) {
xCloseable.close(false);
} else
{
com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
UnoRuntime.queryInterface(
com.sun.star.lang.XComponent.class, oDocToStore );
xComp.dispose();
}
System.out.println("document closed!");
}
catch( Exception e ) {
e.printStackTrace(System.err);
System.exit(1);
}
}

public static void main(String args[]) throws Exception
{
String paths = new String("c:\\a\\");
String savepaths = new String ("f:\\");
readdoc(paths,savepaths);

}

}

D. 我想要实现的功能是用java读取word中的自定义图片

建议使用PageOffice很简单就能实现这些。

E. 用Java从word中提取出图片

因为太长了, 我放我空间了
思路是2003以后, word就可存为xml, 二进制数据按base64编码
然后可以按解析xml文档方式获取图片数据
然后对它进行解码--

F. java:一个文件夹里里面有很多图片有很多word文件有我要如何读取word文件名,该如何做

可以用过滤器,也可以循环自己判断,如FourQueue 写的那样。
下边是过滤器的例子。
------------------------------------------------------------------------------------
import java.io.File;
import java.io.FilenameFilter;

public class FileDoc {

public static void main(String[] args) {
File root = new File("D:\\test");
File[] files = root.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith("doc")) {
// 2003 word
return true;
}
if (name.toLowerCase().endsWith("docx")) {
// 2007 word
return true;
}
return false;
}
});

for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getPath());
}
}

}

G. 有关Java POI问题:从数据库读取word excel 或者是html文件,如何读取文件内容,创建包含内容的图片文件

你可以把word文件用二进制的方式保存到数据库,再把他读取出来,
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
这个in 你可以把读取出来的二进制转为ByteArrayInputStream 对象。

H. java中如何读取word中的某一张图片及读取word中的公式有没有人做过类似的功能啊在线等。

有操作office的包,不过我只操作过Excel,没试过Word
你在Google搜 “java Excel”或“java office”即可找到。

I. 用java程序如何读取word里面的图片

试试jacob,这个操作word的功能还是蛮多的。

阅读全文

与java读取word文件图片相关的资料

热点内容
啦啦操衣服图片 浏览:368
如何将几张图片放在word同一页 浏览:547
伞兵一号准备就绪文字图片 浏览:138
素描画图片简单又好画的少女 浏览:387
微博空白图片怎么发 浏览:406
名车竖版高清壁纸图片 浏览:560
互怼的小可爱图片 浏览:972
短头发穿衣服好看图片 浏览:98
好看简单的背景图片 浏览:699
卡通女孩长发背影图片 浏览:679
文字手机壁纸高清图片 浏览:659
动漫道姑图片 浏览:525
美女的配图片大全 浏览:503
女生光腿丝巾图片 浏览:460
龙女的卡通画图片大全可爱 浏览:537
传琪越野价格和图片 浏览:203
穿猫衣服的情侣图片 浏览:923
胖子男生图片 浏览:191
两棵树简笔画简单又好看图片 浏览:684
微信怎么复制不了图片不显示图片 浏览:423