在用iText写word文档时遇到了几个问题搞不定,各位XDJM知道怎么解决的,还请指点一下问题1、生成的word文档在文档结构图的大纲中不能正常显示。
问题2、怎们设置才能让新的一章不从新的一页开始,而是接着上一章节开始。
问题3、表格如何缩进(目前只能设置表格左对齐、右对齐和居中对齐,可是不知道怎么设置缩进)。代码段如下:
try {
Document document = new Document(PageSize.A4);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new FileOutputStream(fileName));
document.open(); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bfChinese, 28, Font.NORMAL, new Color(255, 0, 0));
Font chapterFont = new Font(bfChinese, 24, Font.NORMAL, new Color(255, 0, 0));
Font sectionFont = new Font(bfChinese, 20, Font.NORMAL, new Color(0, 0, 255));
Font contentFont = new Font(bfChinese, 18, Font.BOLD, new Color(0, 64, 64));
// 添加文档标题
Paragraph title = new Paragraph("测试报告", titleFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 添加第一章
Paragraph cTitle1 = new Paragraph("第一章:chapter1", chapterFont);
Chapter chapter1 = new Chapter(cTitle1, 1);
chapter1.setIndentation(20.0f);
chapter1.setTriggerNewPage(false);
chapter1.setBookOpen(true); // 已经设置了在大纲视图中显示,可是还是显示不出来(问题1)
chapter1.setBookTitle("第一章:chapter1");
Paragraph subTitle1 = new Paragraph("第一节: section 1", sectionFont);
Section section11 = chapter1.addSection(20.0f, subTitle1, 2);
Paragraph content11 = new Paragraph("第一章第一节的内容。", contentFont);
section11.add(content11);
document.add(chapter1);
// 添加第二章
Paragraph cTitle2 = new Paragraph("第二章:chapter2", chapterFont);
Chapter chapter2 = new Chapter(cTitle2, 2);
chapter2.setIndentation(20.0f);
chapter2.setTriggerNewPage(false); // 已经设置了不从新的一页开始,可最后生成的还是从新的一页开始(问题2)
Paragraph subTitle2 = new Paragraph("第一节: section 1", sectionFont);
Section section21 = chapter1.addSection(20.0f, subTitle2, 2);
Paragraph content21 = new Paragraph("第二章第一节中插入表格:", contentFont);
section21.add(content21);
Table table = new Table(3, 2);
table.setAutoFillEmptyCells(true);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
table.addCell(new Phrase("cell" + i + j, contentFont));
}
}
table.setAlignment(Element.ALIGN_LEFT);
table.setLeft(40.0f); // 设置了缩进,可表格还是没缩进
section21.add(table);
chapter2.add(section21);
document.add(chapter2);
// 添加第三章
Paragraph cTitle3 = new Paragraph("第三章:chapter3", chapterFont);
Chapter chapter3 = new Chapter(cTitle3, 3);
document.add(chapter3);
document.close();
} catch(Exception de) {
de.printStackTrace();
}
问题2、怎们设置才能让新的一章不从新的一页开始,而是接着上一章节开始。
问题3、表格如何缩进(目前只能设置表格左对齐、右对齐和居中对齐,可是不知道怎么设置缩进)。代码段如下:
try {
Document document = new Document(PageSize.A4);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new FileOutputStream(fileName));
document.open(); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bfChinese, 28, Font.NORMAL, new Color(255, 0, 0));
Font chapterFont = new Font(bfChinese, 24, Font.NORMAL, new Color(255, 0, 0));
Font sectionFont = new Font(bfChinese, 20, Font.NORMAL, new Color(0, 0, 255));
Font contentFont = new Font(bfChinese, 18, Font.BOLD, new Color(0, 64, 64));
// 添加文档标题
Paragraph title = new Paragraph("测试报告", titleFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 添加第一章
Paragraph cTitle1 = new Paragraph("第一章:chapter1", chapterFont);
Chapter chapter1 = new Chapter(cTitle1, 1);
chapter1.setIndentation(20.0f);
chapter1.setTriggerNewPage(false);
chapter1.setBookOpen(true); // 已经设置了在大纲视图中显示,可是还是显示不出来(问题1)
chapter1.setBookTitle("第一章:chapter1");
Paragraph subTitle1 = new Paragraph("第一节: section 1", sectionFont);
Section section11 = chapter1.addSection(20.0f, subTitle1, 2);
Paragraph content11 = new Paragraph("第一章第一节的内容。", contentFont);
section11.add(content11);
document.add(chapter1);
// 添加第二章
Paragraph cTitle2 = new Paragraph("第二章:chapter2", chapterFont);
Chapter chapter2 = new Chapter(cTitle2, 2);
chapter2.setIndentation(20.0f);
chapter2.setTriggerNewPage(false); // 已经设置了不从新的一页开始,可最后生成的还是从新的一页开始(问题2)
Paragraph subTitle2 = new Paragraph("第一节: section 1", sectionFont);
Section section21 = chapter1.addSection(20.0f, subTitle2, 2);
Paragraph content21 = new Paragraph("第二章第一节中插入表格:", contentFont);
section21.add(content21);
Table table = new Table(3, 2);
table.setAutoFillEmptyCells(true);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
table.addCell(new Phrase("cell" + i + j, contentFont));
}
}
table.setAlignment(Element.ALIGN_LEFT);
table.setLeft(40.0f); // 设置了缩进,可表格还是没缩进
section21.add(table);
chapter2.add(section21);
document.add(chapter2);
// 添加第三章
Paragraph cTitle3 = new Paragraph("第三章:chapter3", chapterFont);
Chapter chapter3 = new Chapter(cTitle3, 3);
document.add(chapter3);
document.close();
} catch(Exception de) {
de.printStackTrace();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货