当2个学生的时候,每个学生固定是2页,循环生产完是4页,现在只能出来第一个学生的第一页第二个学生的两页,如果是10个学生就是前面9个学生都没第二页,最后一个有第二页,不知道怎么回事,求大神解决下,歇歇啦!感激不尽
package atom.lianxi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.filechooser.FileSystemView;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFReport {
public static void main(String[] args) throws Exception, DocumentException {
// 设置表格的形式
Rectangle rectPageSize = new Rectangle(PageSize.A4);
// 创建文档
Document document = new Document(rectPageSize, 0, 0, 0, 0);
document.setMargins(0, 0, 20, 0);
// 获取当前桌面路径
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
String desktopPath = desktopDir.getAbsolutePath() + "\\";
String xxmc = "某某某学院";
String xm = "某某某";
String xzbmc = "某某某班级";
String zymc = "某某某专业";
// 写入
PdfWriter.getInstance(document, new FileOutputStream(new File(desktopPath + "毕业生成绩表" + ".pdf")));
int ys=0;//页数
for (int i = 0; i < 2; i++) {//人数
try {
// 打开
document.open();
for (int y = 0; y < 2; y++) {
++ys;
Font title = new Font(getBaseFont(), 18, Font.BOLD);
Paragraph p = new Paragraph("毕业生成绩表", title);
p.setSpacingBefore(50);
p.setAlignment(1);
document.add(p);
// 成绩表头
Font title2 = new Font(getBaseFont(), 12, Font.BOLD);
// 学生表头
Font title3 = new Font(getBaseFont(), 11, Font.BOLD);
PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(80);
table.setSpacingBefore(13f);
table.setSpacingAfter(2f);
PdfPCell _cell;
for (int h = 0; h < 3; h++) {
for (int l = 0; l < 2; l++) {
_cell = new PdfPCell();
_cell.setBorder(0);
if (h == 0) {
if (l == 0) {
_cell.setColspan(3);
_cell.addElement(new Paragraph("学校名称: " + xxmc, title3));
}
if (l == 1) {
_cell.addElement(new Paragraph("姓名: " + xm, title3));
}
}
if (h == 1) {
if (l == 0) {
_cell.setColspan(3);
_cell.addElement(new Paragraph("专业名词: " + zymc, title3));
}
if (l == 1) {
_cell.addElement(new Paragraph("班级: " + xzbmc, title3));
}
}
table.addCell(_cell);
}
}
// 文档中加入该段落
document.add(table);
// 成绩
String[][] xscj = { { "1", "语文", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }, { "2", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "3", "英语", "", "", "", "", "5", "6", "", "", "", "" }, { "4", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "5", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "6", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "7", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "8", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "9", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "10", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "11", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "12", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "13", "数学", "1", "2", "", "", "", "", "", "", "", "" },
{ "14", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "15", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "16", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "17", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "18", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "19", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "20", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "21", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "22", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "23", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "24", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "25", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "26", "数学", "1", "2", "", "", "", "", "", "", "", "" },
{ "27", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "28", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "29", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "30", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "31", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "32", "数学", "1", "2", "", "", "", "", "", "", "", "" }, { "33", "数学", "1", "2", "", "", "", "", "", "", "", "" } };
int NumColumns = 13;
PdfPTable datatable = new PdfPTable(NumColumns);
float headerwidths[] = { 0.04f, 0.13f, 0.04f, 0.04f, 0.04f, 0.04f, 0.04f, 0.04f, 0.04f, 0.04f, 0.04f, 0.04f, 0.04f };
datatable.setWidths(headerwidths);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
// 画表格
if (y == 0) {//第一页
for (int h = 0; h < 29; h++) {
for (int l = 0; l < NumColumns; l++) {
_cell = new PdfPCell();
if (h >= 1 && h <= xscj.length && l < 12) {
_cell.setMinimumHeight(22);
_cell.addElement(new Paragraph((xscj[h - 1])[l], getFontChinese()));
}
if (h == 0) {
if (l == 0) {
_cell = new PdfPCell(new Paragraph("序号", title2));
_cell.setMinimumHeight(40);
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 1) {
_cell = new PdfPCell(new Paragraph("课程名称", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 2) {
_cell = new PdfPCell(new Paragraph("第 一 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 3) {
_cell = new PdfPCell(new Paragraph("第 二 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 4) {
_cell = new PdfPCell(new Paragraph("第 三 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 5) {
_cell = new PdfPCell(new Paragraph("第 四 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 6) {
_cell = new PdfPCell(new Paragraph("第 五 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 7) {
_cell = new PdfPCell(new Paragraph("第 六 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 8) {
_cell = new PdfPCell(new Paragraph("第 七学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 9) {
_cell = new PdfPCell(new Paragraph("第 八 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 10) {
_cell = new PdfPCell(new Paragraph("第 九 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 11) {
_cell = new PdfPCell(new Paragraph("第 十 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 12) {
_cell = new PdfPCell(new Paragraph("备注", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
_cell.setMinimumHeight(22);
datatable.addCell(_cell);
} else {
_cell.setMinimumHeight(22);
datatable.addCell(_cell);
}
}
}
document.add(datatable);
System.out.println(ys);
} else {//第二页
for (int h = 0; h < 29; h++) {
for (int l = 0; l < NumColumns; l++) {
_cell = new PdfPCell();
if (h >= 1 && h +28<= xscj.length && l < 12) {
_cell.setMinimumHeight(22);
_cell.addElement(new Paragraph((xscj[h+27])[l], getFontChinese()));
}
if (h == 0) {
if (l == 0) {
_cell = new PdfPCell(new Paragraph("序号", title2));
_cell.setMinimumHeight(40);
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 1) {
_cell = new PdfPCell(new Paragraph("课程名称", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 2) {
_cell = new PdfPCell(new Paragraph("第 一 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 3) {
_cell = new PdfPCell(new Paragraph("第 二 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 4) {
_cell = new PdfPCell(new Paragraph("第 三 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 5) {
_cell = new PdfPCell(new Paragraph("第 四 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 6) {
_cell = new PdfPCell(new Paragraph("第 五 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 7) {
_cell = new PdfPCell(new Paragraph("第 六 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 8) {
_cell = new PdfPCell(new Paragraph("第 七学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 9) {
_cell = new PdfPCell(new Paragraph("第 八 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 10) {
_cell = new PdfPCell(new Paragraph("第 九 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 11) {
_cell = new PdfPCell(new Paragraph("第 十 学 期", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (l == 12) {
_cell = new PdfPCell(new Paragraph("备注", title2));
_cell.setHorizontalAlignment(Element.ALIGN_CENTER);
_cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
_cell.setMinimumHeight(22);
datatable.addCell(_cell);
} else {
_cell.setMinimumHeight(22);
datatable.addCell(_cell);
}
}
}
document.add(datatable);
Font title1 = new Font(getBaseFont(), 9, Font.BOLD);
PdfPCell cell;
// 创建需要填入文档的元素
PdfPTable table0 = new PdfPTable(4);
table0.setSpacingBefore(5f);
table0.setWidthPercentage(85);
cell = new PdfPCell(new Paragraph("此表装入学生档案", title1));
cell.setBorder(0);
cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table0.addCell(cell);
cell = new PdfPCell(new Paragraph("北京市教育委员会监制", title1));
cell.setBorder(0);
cell.setColspan(1);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table0.addCell(cell);
document.add(table0);
System.out.println(ys);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
document.close();
System.err.println(ys);
}
// 添加文中字体
private static BaseFont getBaseFont() throws DocumentException, IOException {
return BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
}
// 设置字体大小
private static Font getFontChinese() throws DocumentException, IOException {
Font fontChinese = new Font(getBaseFont(), 10, Font.NORMAL);
return fontChinese;
}
}
第一个成绩单你打印的第二页的时候是有空行的。刚好第二个成绩单的第一页可以放得下,就会覆盖掉。你可以在第二个循环的后面加
document.add(new Paragraph("\n")); 试试,多跳几行空行。
cell = new PdfPCell(new Paragraph("此表装入学生档案", title1));
cell.setBorder(0);
cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table0.addCell(cell);
cell = new PdfPCell(new Paragraph("北京市教育委员会监制", title1));
cell.setBorder(0);
cell.setColspan(1);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table0.addCell(cell);
document.add(table0);
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
System.out.println(ys);
就像这样
解决了,谢谢各位帮助解答,把open放到循环外面就可以了