[简单]使用itext2.1.7制作一个简单的word(2003)

        使用了itext2.1.7制作一个简单的word,代码如下

       

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
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.Table;
import com.lowagie.text.pdf.BaseFont;
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.graphic.RtfShape;
import com.lowagie.text.rtf.graphic.RtfShapePosition;
import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;

public class 答题卡制作_S4_Test {

	public List<String> getContentByHorizontal(int startIndex, int endIndex,
			int colNum, String beforeSpan, String span) {
		List<String> result = new ArrayList<String>();
		int totalRow = (endIndex - startIndex - 1) / colNum + 1;// 总行数
		for (int i = 1; i <= totalRow; i++) {
			StringBuffer sb = new StringBuffer();
			for (int j = 1; j <= colNum; j++) {
				int line = (i - 1) * colNum + j - 1 + startIndex;
				if (line > endIndex) {
					break;
				}
				sb.append(line).append(beforeSpan).append(span);
			}
			result.add(sb.toString());
		}
		/*for (String str : result) {
			System.out.println(str);
		}*/
		return result;
	}

	public List<String> getContentByVertical(int startIndex, int endIndex,
			int colNum, String beforeSpan, String span) {
		List<String> result = new ArrayList<String>();
		int totalRow = (endIndex - startIndex - 1) / colNum + 1;// 总行数
		for (int i = 1; i <= totalRow; i++) {
			StringBuffer sb = new StringBuffer();
			for (int j = 1; j <= colNum; j++) {
				int line = (j - 1) * totalRow + i - 1 + startIndex;
				if (line > endIndex) {
					break;
				}
				sb.append(line).append(beforeSpan).append(span);
			}
			result.add(sb.toString());
		}
		/*for (String str : result) {
			System.out.println(str);
		}*/
		return result;
	}

	public void createDocContext(String file) throws DocumentException,
			IOException {
		// 设置纸张大小
		Document document = new Document(PageSize.A4);
		// 建立一个书写器,与document对象关联
		RtfWriter2.getInstance(document, new FileOutputStream(file));
		document.open();
		// 设置中文字体
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font font2 = new Font(bfChinese, 12, Font.UNDERLINE);// 下划线

		// 页眉
		Image headerImage = Image.getInstance("F:/saveFile/temp/test.jpg");
		headerImage.scaleAbsolute(430, 30);
		Paragraph headerImgPara = new Paragraph();
		RtfHeaderFooter headerImg = new RtfHeaderFooter(headerImage);
		headerImgPara.add(headerImg);

		Phrase headerPara2 = new Phrase();
		headerPara2.add(headerImgPara);
		headerPara2.setFont(font2);

		HeaderFooter header = new HeaderFooter(headerPara2, false);
		header.setAlignment(Paragraph.ALIGN_CENTER);
		document.setHeader(header);

		RtfShapePosition position = new RtfShapePosition(100, 900, 9600, 100);
		position.setXRelativePos(RtfShapePosition.POSITION_X_RELATIVE_MARGIN);
		position.setYRelativePos(RtfShapePosition.POSITION_Y_RELATIVE_PARAGRAPH);
		RtfShape shape = new RtfShape(RtfShape.SHAPE_LINE, position);
		Paragraph par = new Paragraph();
		par.add(shape);
		document.add(par);
		// 标题字体风格
		Font titleFont = new Font(bfChinese, 20, Font.BOLD);
		// 正文字体风格
		Font contextNomalFont = new Font(bfChinese, 11, Font.BOLD);
		Font contextTextFont = new Font(bfChinese, 11, Font.NORMAL);
		Paragraph paragraph = new Paragraph();
		paragraph.setIndentationLeft(50);
		document.add(paragraph);

		paragraph = new Paragraph(
				"七年级上册Unit2 This is just a test. sectionA测试卷答题卡");
		// 设置标题格式对齐方式
		paragraph.setAlignment(Element.ALIGN_CENTER);
		paragraph.setFont(titleFont);
		paragraph.setIndentationLeft(50);
		paragraph.setIndentationRight(50);
		document.add(paragraph);

		paragraph = new Paragraph("班级:________    姓名:________");
		paragraph.setAlignment(Element.ALIGN_CENTER);
		paragraph.setFont(contextTextFont);
		document.add(paragraph);

		paragraph = new Paragraph("一、单选题");
		paragraph.setAlignment(Element.ALIGN_LEFT);
		paragraph.setFont(contextNomalFont);
		paragraph.setFirstLineIndent(60);
		document.add(paragraph);

		List<String> choiceList = getContentByHorizontal(1, 18, 4, " ",
				"[A][B][C][D]  ");
		choiceList = getContentByVertical(1, 18, 4, " ", "[A][B][C][D]  ");
		for (String str : choiceList) {
			// paragraph = new
			// Paragraph("1 [A][B][C][D]  2 [A][B][C][D]  3 [A][B][C][D]  4 [A][B][C][D]");
			paragraph = new Paragraph(str);
			paragraph.setSpacingBefore(5);
			paragraph.setAlignment(Element.ALIGN_LEFT);
			paragraph.setFont(contextTextFont);
			paragraph.setFirstLineIndent(70);
			document.add(paragraph);
		}

		paragraph = new Paragraph("二、填空题");
		paragraph.setSpacingBefore(20);
		paragraph.setAlignment(Element.ALIGN_LEFT);
		paragraph.setFont(contextNomalFont);
		paragraph.setFirstLineIndent(60);
		document.add(paragraph);

		List<String> packList = getContentByHorizontal(4, 12, 3, ".",
				"________________  ");
		for (String str : packList) {
			// paragraph = new
			// Paragraph("4.________________  5.________________  6.________________");
			paragraph = new Paragraph(str);
			paragraph.setAlignment(Element.ALIGN_LEFT);
			paragraph.setFont(contextTextFont);
			paragraph.setSpacingBefore(3);
			paragraph.setFirstLineIndent(70);
			document.add(paragraph);
		}

		// 页眉页脚字体风格
		Font headerFooterFont = new Font(bfChinese, 10, Font.BOLD);
		Paragraph paraFooter = new Paragraph();
		paraFooter.add(new Phrase("第", headerFooterFont));
		paraFooter.add(new RtfPageNumber());
		paraFooter.add(new Phrase("页 共", headerFooterFont));
		paraFooter.add(new RtfTotalPageNumber());
		paraFooter.add(new Phrase("页", headerFooterFont));
		paraFooter.setAlignment(Paragraph.ALIGN_CENTER);
		paraFooter.setFont(headerFooterFont);
		document.setFooter(new RtfHeaderFooter(paraFooter));
		document.close();

	}

	public static void main(String[] args) {
		答题卡制作_S4_Test word = new 答题卡制作_S4_Test();
		String file = "f:/saveFile/temp/sys_16_test.doc";
		try {
			word.createDocContext(file);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

     结果如下:

    
[简单]使用itext2.1.7制作一个简单的word(2003)_第1张图片
      全文完

 

你可能感兴趣的:(itext)