`
步青龙
  • 浏览: 295641 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
博客专栏
72ba33fb-eefe-3de1-bd65-82a6e579265d
Java面试
浏览量:0
社区版块
存档分类
最新评论

itext对word处理合并单元格,处理图片,文档结构图

 
阅读更多

效果:

 

代码:

package com.aaa.bbb;

import java.awt.Color;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.ServletOutputStream;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.field.RtfPageNumber;
import com.lowagie.text.rtf.field.RtfTotalPageNumber;
import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;
import com.lowagie.text.rtf.style.RtfFont;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import com.opensymphony.xwork2.ActionSupport;
import com.aaa.bbb.BaseAction;
 
/**
 *  itext 测试
 */
public class WordAction extends  BaseAction {
 
	RtfFont tableFont = new RtfFont("宋体_GB2312", 14, Font.COURIER, Color.BLACK);
	public void test() {
		try{
		RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1; 
		rtfGsBt1.setAlignment(Element.ALIGN_CENTER); 
		rtfGsBt1.setStyle(Font.BOLD); 
		rtfGsBt1.setSize(14); 
		/* 设置标题2格式 */ 
		RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2; 
		rtfGsBt2.setAlignment(Element.ALIGN_LEFT); 
		rtfGsBt2.setStyle(Font.NORMAL); 
		rtfGsBt2.setSize(12); 
		
		Document document = new Document(PageSize.A4.rotate());
		ByteArrayOutputStream   baos = new ByteArrayOutputStream();
		RtfWriter2.getInstance(document, baos);
		document.open();

		Paragraph paragraph1 = new Paragraph("段落1");
		paragraph1.setAlignment(Element.ALIGN_CENTER);
		paragraph1.setFont(rtfGsBt1); 
		RtfFont titleFont = new RtfFont("宋体_GB2312", 16, Font.BOLD, Color.BLACK);
		// paragraph1.setFont(titleFont);
		document.add(paragraph1);

		
		paragraph1 = new Paragraph("1:图片");
		paragraph1.setFont(rtfGsBt2);// 2级目录 
		
		document.add(paragraph1);
		 
		Paragraph parafooter = new Paragraph();
		Font footerFont = new RtfFont("宋体_GB2312", 12, Font.BOLD, Color.BLACK);
		parafooter.setFont(new Font(footerFont));
		parafooter.add(new Phrase("第"));
		parafooter.add(new RtfPageNumber());
		parafooter.add(new Phrase("页  共"));
		parafooter.add(new RtfTotalPageNumber());
		parafooter.add(new Phrase("页"));

		HeaderFooter footer = new RtfHeaderFooter(parafooter);
		footer.setAlignment(Element.ALIGN_CENTER);
		footer.setBorder(Rectangle.NO_BORDER);
		document.setFooter(footer);

		int rows = 6;
		int columns = 5;// 列的个数
		Table table = new Table(columns, rows);

		int[] widths = new int[columns];
		for (int i = 0; i < widths.length; i++) {
			widths[i] = columns;
		}
		table.setWidths(widths);// 设置每列所占比例

		// table.setWidth(100); // 占页面宽度100%
		table.setAlignment(Element.ALIGN_CENTER);// 居中显示
		table.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
		table.setAutoFillEmptyCells(true); // 自动填满
		table.setPadding(2);// 单元格内间距,即文字与单元格边的距离
		table.setSpacing(0);// 单元格之间的间距
		// 表格操作开始
		addCell(table, "1,1");
		addCell(table, "1,2");
		addCell(table, "1,3");

		Cell cell = new Cell(new Phrase("1,4", tableFont));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setRowspan(1);
		cell.setColspan(2);// 2 列
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setBackgroundColor(Color.blue);
		table.addCell(cell);

		cell = new Cell(new Phrase("1,5", tableFont));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setRowspan(1);
		cell.setColspan(2);// 2 列
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setBackgroundColor(Color.pink);
		table.addCell(cell);

		cell = new Cell(new Phrase("1,6", tableFont));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setRowspan(1);
		cell.setColspan(3);// 3 列
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setBackgroundColor(Color.yellow);
		table.addCell(cell);

		cell = new Cell(new Phrase("1,7占4行", tableFont));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setRowspan(4);// 4行
		cell.setColspan(1);// 1 列
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setBackgroundColor(Color.red);
		table.addCell(cell);

		cell = new Cell(new Phrase("1,8占3列3行", tableFont));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setRowspan(3);// 4行
		cell.setColspan(3);// 1 列
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setBackgroundColor(Color.GREEN);
		table.addCell(cell);
		
		cell = new Cell();
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setRowspan(4);// 4行
		cell.setColspan(1);// 1 列
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setBackgroundColor(Color.magenta);
		Image img = Image.getInstance("D:\\me.jpg");
		img.scaleAbsolute(80, 68);
		cell.add(img);
		table.addCell(cell);
		 
		document.add(table);//  表格
		
		
		
		paragraph1 = new Paragraph("2:描述");
		paragraph1.setFont(rtfGsBt2);// 2级目录  
		paragraph1.add("图片显示表格的横向合并与纵向合并,并在合并的单元格插入图片。");
		document.add(paragraph1);
		
		paragraph1 = new Paragraph("段落2");
		paragraph1.setFont(rtfGsBt1);// 2级目录 
		document.add(paragraph1);
		
		paragraph1 = new Paragraph("1:Coding");
		paragraph1.setFont(rtfGsBt2);// 2级目录 
		paragraph1.add("Coder 开始写代码了!");
		document.add(paragraph1);
		
		paragraph1 = new Paragraph("2:吃饭");
		paragraph1.setFont(rtfGsBt2);// 2级目录  
		paragraph1.add("Coder 吃饭了。");
		document.add(paragraph1);
		
		paragraph1 = new Paragraph("3:睡觉");
		paragraph1.setFont(rtfGsBt2);// 2级目录  
		paragraph1.add("Coder 睡觉了。");
		document.add(paragraph1);
		document.add(new Paragraph("\n"));
		 
		document.close();
		response.reset(); // 清空buffer
		// application/msword --->2003 
		response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
		String filePath = "itext 测试.doc";
		filePath = java.net.URLEncoder.encode(filePath, "UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename="+ new String(filePath.getBytes("UTF-8"), "GBK"));// 定义文件名
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", " 0");
		response.setContentLength(baos.size());
		ServletOutputStream fOut = response.getOutputStream();
		baos.writeTo(fOut);
		fOut.flush();
		fOut.close();
		}catch (Exception e) {
			System.out.println(e);
		}
	}
	public void addCell(Table table, String content) throws BadElementException {
		Cell cell = new Cell(new Phrase(content, tableFont));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		cell.setRowspan(1);
		cell.setColspan(1);
		table.addCell(cell);
	}
}

 

 

 

 

 

 

 

 

 

  • 大小: 78.3 KB
分享到:
评论

相关推荐

    IText导出Word

    IText提供了Table类来创建表格,我们可以设置列数、行数,调整单元格的宽度,合并单元格,甚至添加边框和背景色。 6. **图片插入**:将图片插入Word文档通常需要使用Image类。IText支持多种图像格式,如JPEG、PNG等...

    Itext实现自动生成pdf word文件

    4. **表格和列表**:iText提供高级的表格处理功能,支持多列、合并单元格、自动调整列宽等。同时,它也可以轻松地创建和格式化列表。 5. **字体支持**:iText支持多种字体,包括Unicode和CJK(Chinese, Japanese, ...

    利用Itext画模板导出word,纯java实现

    4. **流式写入**:Itext支持流式写入,这意味着我们可以一边构建文档结构,一边写入到输出流中,最后生成Word文档。这种方式对内存使用友好,适合处理大文档。 5. **字体和样式**:在Word文档中,字体和样式是非常...

    JAVA利用poi完成word转pdf,内容包括两个现成工具类和使用到的所有jar包

    Apache POI提供对Word文档的读取,Docx4j负责转换为PDF,而iText或PDFBox则用于格式优化。这种转换方法虽然涉及的组件较多,但能确保转换的准确性和灵活性,满足各种项目需求。在实际使用中,一定要注意依赖库的版本...

    iText相关jar包

    2. 表格处理:iText提供了`PdfPTable`类来创建和填充表格,支持多列、合并单元格、自适应宽度等功能。开发者可以通过`Cell`对象设置单元格的内容和样式。 3. 图像集成:使用`Image`类可以将图片插入PDF文档,支持...

    poi3.12-beta1

    - XWPF(XML Word Processor Format):用于处理新版Word文档(.docx),基于OOXML规范。 3. **PowerPoint处理**: - HSLF(Horrible Slide Library Format):处理旧版PowerPoint(.ppt)。 - XSLF(XML Slide ...

    InsertDoc:向word插入图片,包含了excel\pdf\word的操作

    【InsertDoc】项目是一个专注于在Word文档中插入各种类型图像的Java实现,它不仅支持常见的图像格式,还处理了PDF、Excel以及Word自身的文件。这个项目的核心功能是将这些不同类型的文件转换为图像,然后方便地插入...

    docx,pdf,xlsx文件操作.zip

    iText提供了`Document`类来创建PDF,`Paragraph`和`Chunk`用于构造文本,`Image`类用于插入图片。读取PDF则需要`PdfReader`和`PdfStamper`等工具。 4. **Java核心技术** 这些操作都基于Java核心技术,如IO流、...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例182 简单的文件合并工具 231 8.2 泛型使用的简介 233 实例183 自定义非泛型栈结构 233 实例184 使用泛型实现栈结构 234 实例185 自定义泛型化数组类 235 实例186 泛型方法与数据查询 236 实例187 泛型化方法与...

Global site tag (gtag.js) - Google Analytics