A. 用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);
}
}
B. 怎么用java导出word文档
java导出word大致有6种解决方案:
1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。DLL动态链接库的生成需要windows平台的支持。该方案只能在windows平台实现,是其局限性。
2:Apache POI包括一系列的API,它们可以操作基于MicroSoft OLE 2 Compound Document Format的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操作,不能设置样式。
3:Java2word是一个在java程序中调用 MS Office Word 文档的组件(类库)。该组件提供了一组简单的接口,以便java程序调用他的服务操作Word 文档。 这些服务包括: 打开文档、新建文档、查找文字、替换文字,插入文字、插入图片、插入表格,在书签处插入文字、插入图片、插入表格等。填充数据到表格中读取表格数据 ,1.1版增强的功能: 指定文本样式,指定表格样式。如此,则可动态排版word文档。是一种不错的解决方案。
4:iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。功能强大。
5:JSP输出样式,该方案实现简单,但是处理样式有点缺陷,简单的导出可以使用。
6:用XML做就很简单了。Word从2003开始支持XML格式,大致的思路是先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。
C. java 把office word,ppt转化为图片
从一个大神那里学来的,已测试无误
package com;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class ExportPPT {
public static void main(String[] args) {
// 读入PPT文件
File file = new File("D:\\UPH.ppt");
doPPTtoImage(file);
}
public static boolean doPPTtoImage(File file) {
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
return false;
}
try {
FileInputStream is = new FileInputStream(file);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
TextRun[] truns = slide[i].getTextRuns();
for (int k = 0; k < truns.length; k++) {
RichTextRun[] rtruns = truns[k].getRichTextRuns();
for (int l = 0; l < rtruns.length; l++) {
rtruns[l].setFontIndex(1);
rtruns[l].setFontName("宋体");
}
}
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.BLUE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
// 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
File path = new File("D:/images");
if (!path.exists()) {
path.mkdir();
}
FileOutputStream out = new FileOutputStream(path + "/" + (i + 1)
+ ".jpg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
System.out.println("success!!");
return true;
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
}
return false;
}
// function 检查文件是否为PPT
public static boolean checkFile(File file) {
boolean isppt = false;
String filename = file.getName();
String suffixname = null;
if (filename != null && filename.indexOf(".") != -1) {
suffixname = filename.substring(filename.lastIndexOf("."));
if (suffixname.equals(".ppt")) {
isppt = true;
}
return isppt;
} else {
return isppt;
}
}
}
D. 求JAVA WORD 转图片 方法 有代码更好!
给你说个思路吧!
用java将word转换为图片,道理就是用java读取word然后写到图片上,就像生成验证码似的
其中会遇到的瓶颈是:1、word里面插入的图片,这个你要分开来判断。2、对于word多少页生成一张图片。还是整个word生成一个图片。
希望可以帮到你。
E. java如何将flex界面中的饼图直接导出到word文档中
FLEX只是一个前台展现,它也是访问过后台数据库取得的数据才展现出来的。在FLEX页面上写一个按钮,调用后台JAVA去访问数据库取得相对的数据,然后用JAVA的方式导出成WORD文件。具体的需求还要看您了,反正核心是取得要的数据做JAVA导出,FLEX上就提供触发导出的操作而已
F. 有什么方法可以用java 将word或者Excel文件转换成图片文件
可以用openoffice将word转化为pdf,再使用swftools把pdf转换为swf
G. java中如何将有图片的页面导出成Word文档
http://blog.163.com/ask_rent@yeah/blog/static/139893663201102471721745/
可以参考一下
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中提取出图片
因为太长了, 我放我空间了
思路是2003以后, word就可存为xml, 二进制数据按base64编码
然后可以按解析xml文档方式获取图片数据
然后对它进行解码--