javadocx
❶ java 中用poi讀取word和用docx4j讀取word
不知道你是具體讀取Word裡面的什麼元素,下面以讀取文字和圖片為例吧,兩個代碼示例,你參考看看:
讀取文本
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文件。
❷ java怎麼給word文檔加水印
可以使用Free Spire.Doc for Java在word文檔中添加文本水印或圖片水印。Free Spire.Doc for Java下載鏈接:網頁鏈接
1.添加文本水印——代碼如下:
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
public class WordTextWatermark {
public static void main(String[] args) {
Document document = new Document();
document.loadFromFile("Sample.docx");
insertTextWatermark(document.getSections().get(0));
}
private static void insertTextWatermark(Section section) {
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.setText("內部使用");
txtWatermark.setFontSize(40);
txtWatermark.setColor(Color.red);
txtWatermark.setLayout(WatermarkLayout.Diagonal);
section.getDocument().setWatermark(txtWatermark);
}
}
2.添加圖片水印——代碼如下:
import com.spire.doc.*;
public class WordImageWatermark {
public static void main(String[] args) throws Exception{
Document document = new Document();
document.loadFromFile("Sample.docx");
PictureWatermark picture = new PictureWatermark();
picture.setPicture("logo.png");
picture.setScaling(5);
picture.isWashout(false);
document.setWatermark(picture);
document.saveToFile("out/result2.docx",FileFormat.Docx )
}
}