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 )
}
}