A. 如何用java代碼向word文檔里固定位置導入圖片
以前做過其他office的沒做過微軟的,不過大同小異
這個你就需要在java中獲取到word的某種對象,方法有很多,但是要看你用什麼方式調用的office了,如果是某個牌子的中間件,那就用那個中間件的二次開發文檔裡面應該就有提到這些方法,如果是第三方的jar包,應該也不麻煩,通過jar包中的方法,可能要有一個word的實例創建的過程吧,然後獲取到range或者textrange等這些對象(應該是這倆對象……)通過這對象然後找到插入圖片的方法,然後傳入位置參數(可能是兩個數用逗號隔開)
B. 用java如何添加圖片到word中,是直接添加不是解析,最好能有代碼
首先你得打開這個word文檔,然後你再把這個圖片做成文件流的格式,然後把它寫進去。
但是這個過程如果你用java自帶的文件流格式打開的話,寫進去會是亂碼。我做過的方法是用開源框架包,poi.jar可以用來做有關office的打開、寫入、讀出等操作,具體代碼沒有了,但是demo裡面的代碼都已經足夠了,你自己稍微看看就能知道怎麼做了。這個包還是比較穩定的。
C. java如何生成含圖片的word文檔
pageoffice很不錯,你有時間網上搜pageoffice的示例代碼參考下吧。
D. 用java怎麼將word文檔轉成圖片格式
可以使用Spire.Doc for Java在Java中利用代碼進行轉換。需要在 Java 程序中添加Free Spire.Doc.jar文件作為依賴項。可以從這個鏈接下載 JAR 文件;如果使用Maven,則可以通過在 pom.xml 文件中添加以下代碼導入 JAR 文件。
repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository></repositories><dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.3</version>
</dependency></dependencies>
Java代碼如下:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertWordToOtherFormats {
public static void main(String[] args) throws IOException {
//創建Document對象
Document doc = new Document();
//載入Word文檔
doc.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");
//將指定頁保存為BufferedImage
BufferedImage image= doc.saveToImages(0, ImageType.Bitmap);
//將圖片數據保存為PNG格式文檔
File file= new File("output/ToPNG.png");
ImageIO.write(image, "PNG", file);
//將Word保存為SVG格式
doc.saveToFile("output/ToSVG.svg",FileFormat.SVG);
//將Word保存為RTF格式
doc.saveToFile("output/ToRTF.rtf",FileFormat.Rtf);
//將Word保存為XPS格式
doc.saveToFile("output/ToXPS.xps",FileFormat.XPS);
//將Word保存為XML格式
doc.saveToFile("output/ToXML.xml",FileFormat.Xml);
//將Word保存為TXT格式
doc.saveToFile("output/ToTXT.txt",FileFormat.Txt);
}
}
E. 用java怎麼把給的一個圖片鏈接,把圖片放在word文檔裡面
兄弟你摳門的夠可以的了,這么復雜你就給20分 你就是給倆白分都不願意有人給你做啊 醒醒吧
F. java想要對已有word文檔追加圖片,應該怎麼做
試試下面的代碼,需要添加spire.doc.jar為依賴
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class AppendImage {
public static void main(String[] args) throws FileNotFoundException {
//創建Document對象
Document doc = new Document();
//載入Word文檔
doc.loadFromFile("G:\360MoveData\Users\Administrator\Desktop\Test.docx");
//獲取最後一節
Section lastSection = doc.getLastSection();
//添加段落
Paragraph paragraph = lastSection.addParagraph();
//添加圖片到段落
InputStream inputStream = new FileInputStream(new File("G:\360MoveData\Users\Administrator\Desktop\image.jpg"));
DocPicture picture = paragraph.appendPicture(inputStream);
//設置圖片高寬
picture.setWidth(120f);
picture.setHeight(120f);
//保存文檔
doc.saveToFile("AppendImage.docx", FileFormat.Docx_2013);
}
}
G. 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:
H. java中怎麼將word文檔怎麼生成圖片
public class CreateWordDemo
{
public void createDocContext(String file)
throws DocumentException,IOException {
//
設置紙張大小
Document document = new
Document(PageSize.A4);
//
建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中
RtfWriter2.getInstance(document, new
FileOutputStream(file));
document.open();
//
設置中文字體
BaseFont bfChinese =
BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//
標題字體風格
Font titleFont = new Font(bfChinese, 12,
Font.BOLD);
//
正文字體風格
Font contextFont = new Font(bfChinese, 10,
Font.NORMAL);
Paragraph title = new
Paragraph("標題");
//
設置標題格式對齊方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
String contextString =
"iText是一個能夠快速產生PDF文件的java類庫。"
+ " \n"//
換行
+
"iText的java類對於那些要產生包含文本,"
+ "表格,圖形的只讀文檔是很有用的。它的類庫尤其與java
Servlet有很好的給合。"
+
"使用iText與PDF能夠使你正確的控制Servlet的輸出。";
Paragraph context = new
Paragraph(contextString);
//
正文格式左對齊
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
//
離上一段落(標題)空的行數
context.setSpacingBefore(5);
//
設置第一行空的列數
context.setFirstLineIndent(20);
document.add(context);
//
利用類FontFactory結合Font和Color可以設置各種各樣字體樣式Paragraph underline = new Paragraph("下劃線的實現",
FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18,
Font.UNDERLINE, new Color(0, 0,
255)));
document.add(underline);
// 設置 Table
表格
Table aTable = new
Table(3);
int width[] = { 25, 25, 50
};
aTable.setWidths(width);//
設置每列所佔比例
aTable.setWidth(90); // 占頁面寬度
90%
aTable.setAlignment(Element.ALIGN_CENTER);//
居中顯示
aTable.setAlignment(Element.ALIGN_MIDDLE);//
縱向居中顯示
aTable.setAutoFillEmptyCells(true); //
自動填滿
aTable.setBorderWidth(1); //
邊框寬度
aTable.setBorderColor(new Color(0, 125, 255)); //
邊框顏色
aTable.setPadding(2);//
襯距,看效果就知道什麼意思了
aTable.setSpacing(3);//
即單元格之間的間距
aTable.setBorder(2);//
邊框
//
設置表頭Cell haderCell = new
Cell("表格表頭");
haderCell.setHeader(true);
haderCell.setColspan(3);
aTable.addCell(haderCell);
aTable.endHeaders();
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,
Color.GREEN);
Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據",
fontChinese));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(255, 0,
0));
cell.setRowspan(2);
aTable.addCell(cell);
aTable.addCell(new
Cell("#1"));
aTable.addCell(new
Cell("#2"));
aTable.addCell(new
Cell("#3"));
aTable.addCell(new
Cell("#4"));
Cell cell3 = new Cell(new Phrase("一行三列數據",
fontChinese));
cell3.setColspan(3);
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
document.add(aTable);
document.add(new
Paragraph("\n"));
//
添加圖片 Image.getInstance即可以放路徑又可以放二進制位元組流
Image img =
Image.getInstance("d:\\img01800.jpg");
img.setAbsolutePosition(0,
0);
img.setAlignment(Image.RIGHT);//
設置圖片顯示位置
img.scaleAbsolute(60, 60);//
直接設定顯示尺寸
//
img.scalePercent(50);//表示顯示的大小為原尺寸的50%
// img.scalePercent(25,
12);//圖像高寬的顯示比例
//
img.setRotation(30);//圖像旋轉一定角度
document.add(img);
document.close();
}public static void main(String[] args)
{
CreateWordDemo word = new
CreateWordDemo();
String file =
"d:/demo1.doc";
try
{
word.createDocContext(file);
} catch (DocumentException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
I. 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 )
}
}