使用了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(); } } }
结果如下:
全文完。
相关推荐
总的来说,IText 2.1.7是一个强大而全面的PDF处理工具,它简化了PDF在Java应用中的使用,无论是生成新的PDF文档,还是处理已有的PDF文件,都提供了极大的便利。不过,需要注意的是,随着版本的更新,新版本的IText...
在2.1.7版本中,它包含了一个全面的核心库,支持各种PDF操作,并且在这个版本中还整合了对RTF(Rich Text Format)的支持,使得用户可以方便地进行Word文档的操作。RTF是一种通用的文本格式,能够保留文本的格式信息...
iText是一个强大的库,能够帮助开发者轻松创建和编辑PDF及Word文档。本文将详细介绍如何使用iText 2.1.7版本来实现这一功能,并探讨其主要特性。 iText是一个开源的Java库,主要用于生成和处理PDF文档。此外,通过...
总的来说,iText v2.1.7是一个功能丰富的PDF处理库,适用于多种场景,如报表生成、电子书制作、合同签署等。它的API设计直观且易于使用,使得开发者无需深入理解PDF规范的复杂细节,就能高效地创建高质量的PDF文档。...
以下是一个简单的步骤概述: 1. **导入库**:在你的Java文件中,你需要导入必要的iText类。例如: ```java import com.lowagie.text.Document; import com.lowagie.text.Paragraph; import ...
在Java中实现Word文档导出,IText是一个强大的工具。通过使用这个库,开发者可以自定义Word文档的样式,包括字体、颜色、大小、对齐方式、边框、背景色等。此外,还可以插入图片、创建复杂的表格、添加页眉页脚、...
这里我们要讨论的是一个强大的Java库——iText-Lowagie Jar,它为开发人员提供了强大的功能,使得在程序中创建和操作PDF以及Word文档变得轻而易举。 iText是一个开源的Java库,由Bruno Lowagie和他的团队开发,主要...
- **JPEG转PDF**: 首先,使用iText创建一个新的PDF文档,然后将JPEG图片插入到PDF中,完成图片到PDF的转换。 - **PDF文档操作**: iText允许用户添加、删除、编辑PDF页面,插入文本、图像,甚至可以添加表单字段和...