在iText中水平添加带有多列文本的PdfPTable
我用iText生成了一个pdf报告,其中包含一个添加到MultiColumnText的PdfPTable,有时会变得非常大,以至于它会被拆分到多个页面上 目前,MultiColumnText被两列分开,MultiColumnText垂直填充PdfPTable,如下所示: 第1页在iText中水平添加带有多列文本的PdfPTable,itext,Itext,我用iText生成了一个pdf报告,其中包含一个添加到MultiColumnText的PdfPTable,有时会变得非常大,以至于它会被拆分到多个页面上 目前,MultiColumnText被两列分开,MultiColumnText垂直填充PdfPTable,如下所示: 第1页 +--+--+ |T1 | T5 | +--+--+ |T2 | T6 | +--+--+ |T3 | T7 | +--+--+ |T4 | T8 | +--+--+ 第2页 +---+---+ |T9 | T13 |
+--+--+
|T1 | T5 |
+--+--+
|T2 | T6 |
+--+--+
|T3 | T7 |
+--+--+
|T4 | T8 |
+--+--+
第2页
+---+---+
|T9 | T13 |
+---+---+
|T10 | T14 |
+---+---+
|T11 | T15 |
+---+---+
|T12 | T16 |
+---+---+
……我想说: 第1页
+--+--+
|T1 | T2 |
+--+--+
|T3 | T4 |
+--+--+
|T5 | T6 |
+--+--+
|T7 | T8 |
+--+--+
第2页
+---+---+
|T9 | T10 |
+---+---+
|T11 | T12 |
+---+---+
|T13 | T14 |
+---+---+
|T15 | T16 |
+---+---+
代码是:
/**
* Initializes the fonts and collections.
* Creates a PDF document.
*
* @param from as a Date
* @param to as a Date
* @param weeklyComplianceMap as a Map for print weekly compliance
* @param monthlyComplianceMap as a Map for print monthly compliance
* @param calLogList as a List for calculate the add event
* @param locale Locale in case you want to create a Calendar in another language
* @throws DocumentException, IOException, ParseException
* @return ByteArrayOutputStream of PDF output
*/
public ByteArrayOutputStream createPdf(Date from, Date to, Map<String, Integer> weeklyComplianceMap,
Map<String, Double> monthlyComplianceMap, List<InjectionLogInfo> calLogList, Locale locale)
throws DocumentException, ParseException, IOException {
calendar = new GregorianCalendar();
calendar.setTime(from);
BaseFont bf_normal = BaseFont.createFont(
"C:/Windows/Fonts/arial.ttf", BaseFont.WINANSI,
BaseFont.EMBEDDED);
small = new Font(bf_normal, 8);
normal = new Font(bf_normal, 11);
BaseFont bf_bold = BaseFont.createFont(
"C:/Windows/Fonts/arialbd.ttf", BaseFont.WINANSI,
BaseFont.EMBEDDED);
smallBold = new Font(bf_bold, 10);
normalBold = new Font(bf_bold, 12);
bigBold = new Font(bf_bold, 14);
document = new Document(PageSize.A4, 20, 20, 40, 30);
baos = new ByteArrayOutputStream();
writer = PdfWriter.getInstance(document, baos);
ResourceBundle rb = ResourceBundle.getBundle("com.resources.messages", locale);
Paragraph hText = new Paragraph(rb.getString("lbl.calendar.view"), bigBold);
hText.setAlignment(Element.ALIGN_CENTER);
Chunk c1 = new Chunk(rb.getString("lbl.document.generated") + " ", normal);
Chunk c2 = new Chunk(fdf.format(new Date()), normal);
Chunk c3 = new Chunk(" " + rb.getString("lbl.at") + " ", normal);
Chunk c4 = new Chunk(tdf.format(new Date()), normal);
Chunk c5 = new Chunk(new VerticalPositionMark(), 500f, false);
Chunk c6 = new Chunk(rb.getString("lbl.page") + " ", normal);
Phrase fText = new Phrase();
fText.add(c1);fText.add(c2);fText.add(c3);
fText.add(c4);fText.add(c5);fText.add(c6);
HeaderFooter header = new HeaderFooter(hText, false);
HeaderFooter footer = new HeaderFooter(fText, true);
document.setHeader(header);
document.setFooter(footer);
document.open();
document.leftMargin();
mct = new MultiColumnText();
mct.addRegularColumns(document.left(), document.right(), 20, 2);
mct.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
for (int month = 0; month < monthsBetween(from, to, Calendar.MONTH); month++) {
// create a table with 8 columns
float[] colsWidth = {35f, 35f, 35f, 35f, 35f, 35f, 35f, 50f};
table = new PdfPTable(colsWidth);
table.setWidthPercentage(100);
// add the name of the month
table.getDefaultCell().setBackgroundColor(Color.WHITE);
table.addCell(getMonthCell(calendar, locale));
Double monAdh = monthlyComplianceMap.get(mdf.format(calendar.getTime()));
table.addCell(getMonthlyAdherence(monAdherence));
// add the name of the days
String[] days = getDayNames();
for (String day : days) {
table.addCell(getDayNamesCell(day, locale));
}
int day = 1;
int position = 2;
int dayofWeek = calendar.get(Calendar.DAY_OF_WEEK);
int daysinMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// add empty cells
int rc = 0;
while (position != dayofWeek) {
rc++;
position = (position % 7) + 1;
table.addCell("");
}
// add cells for each day
while (day <= daysinMonth) {
calendar = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), day++);
table.addCell(getDayCell(calLogList, calendar, locale));
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
rc++;
String week = (calendar.get(Calendar.WEEK_OF_YEAR)-1) + ", " + calendar.get(Calendar.YEAR);
if (null != weeklyComplianceMap) {
wa = weeklyComplianceMap.get(week);
table.addCell(getDayAdherenceCell(weekAdherence));
} else {
String weekAdherence = "0%";
table.addCell(getDayAdherenceCell(weekAdherence));
}
}
}
if (9 < rc)
table.setSpacingAfter(20);
else
table.setSpacingAfter(40);
// complete the table
table.completeRow();
// add the table to MultiColumnText object
mct.addElement(table);
// increment the day by 1
calendar.add(Calendar.DATE, 1);
}
document.add(mct);
document.newPage();
document.close();
return baos;
}
/**
* Creates a PdfPCell with the name of the month
*
* @param calendar a date
* @param locale a locale
* @return a PdfPCell with rowspan 7, containing the name of the month
*/
public PdfPCell getMonthCell(Calendar calendar, Locale locale) {
PdfPCell cell = new PdfPCell();
cell.setColspan(7);
cell.setMinimumHeight(30);
cell.setBackgroundColor(Color.GRAY);
Paragraph p = new Paragraph(String.format(locale, "%1$tB %1$tY", calendar), normalBold);
p.setAlignment(Element.ALIGN_LEFT);
cell.addElement(p);
return cell;
}
/**
* Creates a PdfPCell for a month
*
* @param string adherence of a month
* @return a PdfPCell
*/
private PdfPCell getMonthlyAdherence(String adherence) {
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(35);
//cell.setBorderColorLeft(Color.GRAY);
cell.setBackgroundColor(Color.GRAY);
Paragraph p = new Paragraph(adherence, smallBold);
p.setAlignment(Element.ALIGN_RIGHT);
cell.addElement(p);
return cell;
}
/**
* Creates a PdfPCell with the name of the day
*
* @param day name of a day
* @param locale a locale
* @return a PdfPCell, containing the name of the day
*/
public PdfPCell getDayNamesCell(String day, Locale locale) {
PdfPCell cell = new PdfPCell();
cell.setPadding(3);
cell.setBackgroundColor(Color.LIGHT_GRAY);
Paragraph p = new Paragraph(day, smallBold);
p.setAlignment(Element.ALIGN_CENTER);
cell.addElement(p);
return cell;
}
/**
* Creates a PdfPCell for a specific day
*
* @param calendar a date
* @param locale a locale
* @return a PdfPCell
*/
public PdfPCell getDayCell(List<InjectionLogInfo> calLogList, Calendar calendar, Locale locale) {
PdfPCell cell = new PdfPCell();
cell.setPadding(3);
// set the content in the language of the locale
Chunk chunk = new Chunk(String.format(locale, "%1$te", calendar), small);
// a paragraph with the day
Paragraph p = new Paragraph(chunk);
p.setAlignment(Element.ALIGN_CENTER);
cell.addElement(p);
return cell;
}
/**
* Creates a PdfPCell for a week
*
* @param string adherence of a week
* @return a PdfPCell
*/
public PdfPCell getDayAdherenceCell(String adherence) {
PdfPCell cell = new PdfPCell();
cell.setPadding(3);
// set the adherence for each week
Chunk chunk = new Chunk(adherence, small);
// a paragraph with the adherence
Paragraph p = new Paragraph(chunk);
p.setAlignment(Element.ALIGN_CENTER);
cell.addElement(p);
return cell;
}
/**
* Retrieves a Day Names for a single week
*
* @return a String array of day names
*/
public String[] getDayNames() {
DateFormatSymbols symbols = new DateFormatSymbols();
String[] dayNames = symbols.getShortWeekdays();
List<String> stringList = new ArrayList<String>();
for (String string : dayNames) {
if (string != null && string.length() > 0) {
stringList.add(string);
}
}
if (stringList.size() > 0) {
String one = stringList.get(0);
stringList.remove(0);
stringList.add(one);
stringList.add("%");
}
dayNames = stringList.toArray(new String[stringList.size()]);
return dayNames;
}
/**
*初始化字体和集合。
*创建PDF文档。
*
*@param from作为日期
*@param to作为日期
*@param weeklyComplianceMap作为每周打印合规性地图
*@param monthlyComplianceMap作为每月打印合规性地图
*@param calLogList作为计算添加事件的列表
*@param locale,以防您想用另一种语言创建日历
*@DocumentException、IOException、ParseException
*@return ByteArrayOutputStream的PDF输出
*/
public ByteArrayOutputStream createPdf(日期从,日期到,地图周一致性地图,
映射月符合性映射、列表调用列表、区域设置)
抛出DocumentException、ParseException、IOException{
日历=新的公历日历();
日历。设置时间(从);
BaseFont bf_normal=BaseFont.createFont(
“C:/Windows/Fonts/arial.ttf”,BaseFont.WINANSI,
BaseFont.EMBEDDED);
小字体=新字体(bf_正常,8);
正常=新字体(bf_正常,11);
BaseFont bf_bold=BaseFont.createFont(
“C:/Windows/Fonts/arialbd.ttf”,BaseFont.WINANSI,
BaseFont.EMBEDDED);
smallBold=新字体(bf_粗体,10);
normalBold=新字体(bf_bold,12);
大黑体=新字体(bf_粗体,14);
文件=新文件(PageSize.A4、20、20、40、30);
bas=新的ByteArrayOutputStream();
writer=PdfWriter.getInstance(文档,BAS);
ResourceBundle rb=ResourceBundle.getBundle(“com.resources.messages”,locale);
段落hText=新段落(rb.getString(“lbl.calendar.view”)、粗体字;
hText.setAlignment(Element.ALIGN_CENTER);
Chunk c1=新块(rb.getString(“lbl.document.generated”)+“”,正常);
Chunk c2=新块(fdf.format(new Date()),正常);
Chunk c3=新块(“+rb.getString(“lbl.at”)+”,正常);
Chunk c4=新块(tdf.format(new Date()),正常);
Chunk c5=新块(新垂直位置标记(),500f,false);
Chunk c6=新块(rb.getString(“lbl.page”)+“”,正常);
短语fText=新短语();
fText.add(c1);fText.add(c2);fText.add(c3);
fText.add(c4);fText.add(c5);fText.add(c6);
HeaderFooter header=新的HeaderFooter(hText,false);
HeaderFooter=新的HeaderFooter(fText,true);
document.setHeader(header);
document.setFooter(页脚);
document.open();
document.leftMargin();
mct=新的多列文本();
mct.addRegularColumns(document.left(),document.right(),20,2);
mct.setRunDirection(PdfWriter.RUN\u DIRECTION\u LTR);
for(int-month=0;月<月之间(从,到,日历.月);月++){
//创建一个包含8列的表
float[]colsWidth={35f,35f,35f,35f,35f,35f,35f,50f};
表=新的PdfPTable(colsWidth);
表1.设定宽度百分比(100);
//添加月份的名称
table.getDefaultCell().setBackgroundColor(Color.WHITE);
表.addCell(getMonthCell(日历、语言环境));
Double monAdh=monthlyComplianceMap.get(mdf.format(calendar.getTime());
表.addCell(getMonthlyAdherence(monAdherence));
//添加日期的名称
字符串[]天=getDayNames();
用于(字符串日期:天){
表.addCell(getDayNamesCell(天,区域设置));
}
整日=1;
int位置=2;
int dayofWeek=calendar.get(calendar.DAY/u周);
int daysinMonth=calendar.getActualMaximum(calendar.DAY\u OF\u MONTH);
//添加空单元格
int rc=0;
while(位置!=dayofWeek){
rc++;
位置=(位置%7)+1;
表1.addCell(“”);
}
//每天添加单元格
而(第0天){
stringList.add(字符串);
}
}
如果(stringList.size()>0){
stringone=stringList.get(0);
stringList.remove(0);
stringList。添加(一个);
stringList。添加(“%”);
}
dayNames=stringList.toArray(新字符串[stringList.size()]);
返回日名;
}
我坚持这样做,因此非常感谢您的帮助。谢谢。我想您正在为多栏文本使用
新的PdfPtable(1)
。将其更改为newpdfptable(2)
以每1个多列文本获得2列表格