导航:首页 > 编程语言 > java图片word文档

java图片word文档

发布时间:2022-11-02 03:03:18

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为依赖

  1. import com.spire.doc.Document;

  2. import com.spire.doc.FileFormat;

  3. import com.spire.doc.Section;

  4. import com.spire.doc.documents.Paragraph;

  5. import com.spire.doc.fields.DocPicture;


  6. import java.io.File;

  7. import java.io.FileInputStream;

  8. import java.io.FileNotFoundException;

  9. import java.io.InputStream;


  10. public class AppendImage {


  11. public static void main(String[] args) throws FileNotFoundException {


  12. //创建Document对象

  13. Document doc = new Document();

  14. //加载Word文档

  15. doc.loadFromFile("G:\360MoveData\Users\Administrator\Desktop\Test.docx");

  16. //获取最后一节

  17. Section lastSection = doc.getLastSection();

  18. //添加段落

  19. Paragraph paragraph = lastSection.addParagraph();

  20. //添加图片到段落

  21. InputStream inputStream = new FileInputStream(new File("G:\360MoveData\Users\Administrator\Desktop\image.jpg"));

  22. DocPicture picture = paragraph.appendPicture(inputStream);

  23. //设置图片高宽

  24. picture.setWidth(120f);

  25. picture.setHeight(120f);

  26. //保存文档

  27. doc.saveToFile("AppendImage.docx", FileFormat.Docx_2013);

  28. }

  29. }

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 )

}

}

阅读全文

与java图片word文档相关的资料

热点内容
找漫画看应该下载什么app 浏览:182
如何在vps上搭建自己的代理服务器 浏览:744
nginxphp端口 浏览:403
内脏pdf 浏览:152
怎么看云服务器架构 浏览:85
我的世界国际服为什么登不进服务器 浏览:996
微盟程序员老婆 浏览:930
intellij创建java 浏览:110
java连接odbc 浏览:38
启动修复无法修复电脑命令提示符 浏览:359
手机编程是什么 浏览:98
山东移动程序员 浏览:163
苏州java程序员培训学校 浏览:479
单片机液晶驱动 浏览:856
魔拆app里能拆到什么 浏览:132
新预算法的立法理念 浏览:144
wdcpphp的路径 浏览:136
单片机p0口电阻 浏览:926
浏览器中调短信文件夹 浏览:595
五菱宏光空调压缩机 浏览:70