java word 转图片 java html转word 图片 - 电脑|办公 - 电脑办公-杀毒安全-网络-V3学习网
微商网
 
 
导航:首页 |电脑|办公|正文

java word 转图片 java html转word 图片

时间:2020-07-20 08:06:31
java中怎么将word文档怎么生成图片 public class CreateWordDemo { public void createDocContext(String file) throws
作者:

java word 转图片

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

如何将jpg格式的图片转换成word格式?详细图片见附件.麻烦帮?

具体方法如下: 1. 把需要的东西复制到 Wod 2010; 2. 在Wod 2010 中选中需要转成图片的内容,copy—paste special—pictue,然后右键点击图片,将图片保存到桌面,可选的格式有JPG、PNG、TIF、GIF等,导出成JPG图片可能会出现部分空档被黑色填充的状况,所以最好导成PNG格式的图片。

Wod 2010 最多只能将一页的内容保存成一张图片,也就是如果内容有多页,全部复制然后粘贴成图片后将只出现第一页的一张图片。

所以要分别把每一页的内容复制粘贴成图片,然后导出得到多张图片。

由于文字是彩色的,所以导出的Png图片比较大,在1MB左右,如果有图片的话可能在10MB左右。

3. 在Photoshop里新建文档,根据图片的数量设置高度和宽度。

比如上面这张图片是由8张图片组合而成的,每张图片的宽度均为3592像素,高度不等,所以新建档的高度要比这些图片的高度之和大,最后我将其像素设置为3592*40000。

然后用Photoshop打开全部8张图片,依次Ctl+A, Ctl+C, 然后在新建档里Ctl+V,调整图片的位置。

4. 将新建档另存为TIF格式(似乎由于新合成的图片太大,只有PSB、RAW、TIF三种格式可以选择),然后Image Compession选zip,Laye Compession选Discad layes and save a copy,然后点确定(这是目前我能得到的最小的图片,大小为7MB左右,其他的都超级大)。

5. 用格式工厂把保存的TIF图片转成JPG图片,得到的图片大小为14MB左右。

这就大功告成了。

不足的是14MB的JPG还是太大了,而且我的Photoshop直接打不开,它说Cannot complete youequest ecause of a polem pasing you JPEG data. 用Picasa导出的图片又太小,直接看不清内容。

最后索性把这14MB的图片上传到了点点,点点帮我把它压缩到了4.5MB,就是现在看到的这张,清晰度跟14MB的那张没什么区别。

java POI html转word,无法显示图片

jaco是java和windows下的com桥,通过它我们可以在java程序中调用COM组件。

如果你的JDK是1.4,那你需要下载jaco1.9的jni库才能正常运行,早期版本在JDK1.4下有些问题。

package com;** * Title:Wod文档转html类 * Desciption: * Copyight:() 2002 * @autho 舵手 * @vesion 1.0 *impot com.jaco.com.*;impot com.jaco.activeX.*; pulic class WodtoHtml { ** *文档转换函数 *@paam docfile wod文档的绝对路径加文件名(包含扩展名) *@paam htmlfile 转换后的html文件绝对路径和文件名(不含扩展名) * pulic static void change(Sting docfile, Sting htmlfile) { ActiveXComponent app = new ActiveXComponent("Wod.Application"); 启动wod ty { app.setPopety("Visile", new Vaiant(false)); 设置wod不可见 Oject docs = app.getPopety("Documents").toDispatch(); Oject doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Oject[] { docfile, new Vaiant(false),new Vaiant(tue) }, new int[1]).toDispatch(); 打开wod文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Oject[] {htmlfile, new Vaiant(8) }, new int[1]); 作为html格式保存到临时文件 Vaiant f = new Vaiant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.pintStackTace(); } finally { app.invoke("Quit", new Vaiant[]{}); } } pulic static void main(Sting[] sts){ WodtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t"); }}

java文本文件转化为图片文件怎么弄?

final int java.io:"&nbsp.awt;import 输入保存的图片名称 (例如: D:/java.jpg); &nbsp,&nbsp.image;输入TXT文本名称 (例如: D:/java.txt&nbsp.out; &nbsp:"static java.io;&nbsp.println(&quot,仅仅更改后缀名是不行的? */ &nbsp.codec.sun;boolean success &nbsp.Graphics;import java.BufferedImage.image;);java.awt.Color;import java;main(String[] args) textFileName import&nbsp.0 */public class &nbsp.print("{ Scanner&nbsp.awt; /** 图片宽度 */ private = in; : "失败"));图片文件 import &nbsp.image;+ (success ;/** * Scanner(System.in); System.jpeg.JPEGImageEncoder.FileNotFoundException;*/ &nbsp.out;&nbsp.jpeg.JPEGCodec;/** TextToImage { &nbsp.print(&quot.awt;&nbsp.Font;import java; java.util;IMAGE_WIDTH = 400; /** 图片高度 */ private final int IMAGE_HEIGHT = 600; /** 图片类型 */ private final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB; /** * 构造函数 * @param textFile 文本文件 * @param imageFile 图片文件 */ public TextToImage(File textFile,File imageFile){ this.textFile = textFile; this.imageFile = imageFile; this.image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_TYPE); } /** * 将文本文件里文字,写入到图片中保存 * @return boolean true,写入成功;false,写入失败 */ public boolean convert() { //读取文本文件 BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(textFile)); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } //获取图像上下文 Graphics g = createGraphics(image); String line; //图片中文本行高 final int Y_LINEHEIGHT = 15; int lineNum = 1; try { while((line = reader.readLine()) != null){ g.drawString(line, 0, lineNum * Y_LINEHEIGHT); lineNum++; } g.dispose(); //保存为jpg图片 FileOutputStream fos = new FileOutputStream(imageFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); encoder.encode(image); fos.close(); } catch (IOException e) { e.printStackTrace(); return false; } return true; } /** * 获取到图像上下文 * @param image 图片 * @return Graphics */ private Graphics createGraphics(BufferedImage image){ Graphics g = image.createGraphics(); g.setColor(Color.WHITE); //设置背景色 g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);//绘制背景 g.setColor(Color.BLACK); //设置前景色 g.setFont(new Font("微软雅黑", Font.PLAIN, 12)); //设置字体 return g; } } 特别注意:程序中使用到了com.sun.image.codec.jpeg.JPEGImageEncoder和 com.sun.image.codec.jpeg.JPEGCodec ,这 两个是sun的专用API,Eclipse会报错。

解决办法: Eclipse软件,Windows->Preferences->Java->Complicer->Errors/Warnings,Deprecated and restricted API->Forbidden reference 改为 Warnning。

如果还是报错,在工程上build path,先移除JRE System Library,然后再添加JRE System Library。

; com; private BufferedImage import &nbsp.IOException;textFile; /** );import TextToImage convert&nbsp.io.File;YY2924 com;&nbsp:1、读取txt文本的每一行 2、创建BufferedImage图片,然后在图片上画读取到的文本 下面给出示例程序: 测试类 TextToImageExample; &nbsp.nextLine(); System;TextToImage(new File(textFileName);String image;imageFileName = = convert.convert(); System; @author YY2924 2014/11/18 * import * @author 2014/11/18 * 文本转图片类 * 成功&quot.BufferedReader;import java; );String * @version 1.0 new File(imageFileName));图片 文本转图片.io.FileReader; /** 文本文件 */ private public&nbsp。

按照你说想的,可以这么来做;&nbsp:"}} 文本转图片类 TextToImage.java import private File imageFile; /** java.io;class TextToImageExample { void&nbsp.FileOutputStream;import java;文本转图片测试类 in "= new 1;File import&nbsp.javaimport java。

也就是说.codec; */public&nbsp.sun文件在计算机中都是以二进制保存的,但系统是以文件头来区分各种文件格式的;in.nextLine().Scanner; java.io; = new @version&nbsp.io.File.out

大家还关注
阅读排行
推荐阅读