如何在内存中创建日历图像以添加到 PDF?
How to create a calendar image in memory to add to PDF?
我需要将日历图像添加到我在 java 应用程序中创建的 PDF。 PDF 是使用 iText 创建的,这不是问题。我只是不知道如何动态创建日历图像?日历必须准确显示完整的月份。我附上了给定 2015 年 12 月的样例
我在想,如果我不能解决这个问题,我会在接下来的 36 个月内手动创建这些图像,然后继续这样做。显然这是最后的手段
真奇怪,你post要求一个完全在官方文档中开发的例子。
在 chapter 4 of "iText in Action - Second Edition", I write about tables and in the PdfCalendar 示例中,我使用 PdfPTable
创建日历:
// create a table with 7 columns
table = new PdfPTable(7);
table.setTotalWidth(504);
// add the name of the month
table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
table.addCell(getMonthCell(calendar, locale));
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int day = 1;
int position = 2;
// add empty cells
while (position != calendar.get(Calendar.DAY_OF_WEEK)) {
position = (position % 7) + 1;
table.addCell("");
}
// add cells for each day
while (day <= daysInMonth) {
calendar = new GregorianCalendar(year, month, day++);
table.addCell(getDayCell(calendar, locale));
}
// complete the table
table.completeRow();
这些是使用的辅助方法:
/**
* 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.setBackgroundColor(BaseColor.WHITE);
cell.setUseDescender(true);
Paragraph p = new Paragraph(String.format(locale, "%1$tB %1$tY", calendar), bold);
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(Calendar calendar, Locale locale) {
PdfPCell cell = new PdfPCell();
cell.setPadding(3);
// set the background color, based on the type of day
if (isSunday(calendar))
cell.setBackgroundColor(BaseColor.GRAY);
else if (isSpecialDay(calendar))
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
else
cell.setBackgroundColor(BaseColor.WHITE);
// set the content in the language of the locale
Chunk chunk = new Chunk(String.format(locale, "%1$ta", calendar), small);
chunk.setTextRise(8);
// a paragraph with the day
Paragraph p = new Paragraph(chunk);
// a separator
p.add(new Chunk(new VerticalPositionMark()));
// and the number of the day
p.add(new Chunk(String.format(locale, "%1$te", calendar), normal));
cell.addElement(p);
return cell;
}
/**
* Returns true for Sundays.
* @param calendar a date
* @return true for Sundays
*/
public boolean isSunday(Calendar calendar) {
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
return true;
}
return false;
}
/**
* Returns true if the date was found in a list with special days (holidays).
* @param calendar a date
* @return true for holidays
*/
public boolean isSpecialDay(Calendar calendar) {
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
return true;
if (specialDays.containsKey(String.format("%1$tm%1$td", calendar)))
return true;
return false;
}
请注意,isSpecialDay()
方法需要一个 specialDays
对象,其中包含特殊日子的列表,例如耶稣升天节、圣诞节等...
在chapter 5 of the same book, I describe how to use events to change the way the table is rendered (rounded corners, special colors,...). This example is also named PdfCalendar.
顺带一提,这两章是官网搜索关键词Calendar最先出现的。你怎么没找到这些例子?
另外:你为什么要一张图片?您想在 PDF 中使用这些日历 table,那么为什么要创建光栅图像?放大时光栅图像看起来真的很糟糕。按照我的书中所述创建 table,将生成高质量的 PDF。
我需要将日历图像添加到我在 java 应用程序中创建的 PDF。 PDF 是使用 iText 创建的,这不是问题。我只是不知道如何动态创建日历图像?日历必须准确显示完整的月份。我附上了给定 2015 年 12 月的样例
我在想,如果我不能解决这个问题,我会在接下来的 36 个月内手动创建这些图像,然后继续这样做。显然这是最后的手段
真奇怪,你post要求一个完全在官方文档中开发的例子。
在 chapter 4 of "iText in Action - Second Edition", I write about tables and in the PdfCalendar 示例中,我使用 PdfPTable
创建日历:
// create a table with 7 columns
table = new PdfPTable(7);
table.setTotalWidth(504);
// add the name of the month
table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
table.addCell(getMonthCell(calendar, locale));
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int day = 1;
int position = 2;
// add empty cells
while (position != calendar.get(Calendar.DAY_OF_WEEK)) {
position = (position % 7) + 1;
table.addCell("");
}
// add cells for each day
while (day <= daysInMonth) {
calendar = new GregorianCalendar(year, month, day++);
table.addCell(getDayCell(calendar, locale));
}
// complete the table
table.completeRow();
这些是使用的辅助方法:
/**
* 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.setBackgroundColor(BaseColor.WHITE);
cell.setUseDescender(true);
Paragraph p = new Paragraph(String.format(locale, "%1$tB %1$tY", calendar), bold);
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(Calendar calendar, Locale locale) {
PdfPCell cell = new PdfPCell();
cell.setPadding(3);
// set the background color, based on the type of day
if (isSunday(calendar))
cell.setBackgroundColor(BaseColor.GRAY);
else if (isSpecialDay(calendar))
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
else
cell.setBackgroundColor(BaseColor.WHITE);
// set the content in the language of the locale
Chunk chunk = new Chunk(String.format(locale, "%1$ta", calendar), small);
chunk.setTextRise(8);
// a paragraph with the day
Paragraph p = new Paragraph(chunk);
// a separator
p.add(new Chunk(new VerticalPositionMark()));
// and the number of the day
p.add(new Chunk(String.format(locale, "%1$te", calendar), normal));
cell.addElement(p);
return cell;
}
/**
* Returns true for Sundays.
* @param calendar a date
* @return true for Sundays
*/
public boolean isSunday(Calendar calendar) {
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
return true;
}
return false;
}
/**
* Returns true if the date was found in a list with special days (holidays).
* @param calendar a date
* @return true for holidays
*/
public boolean isSpecialDay(Calendar calendar) {
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
return true;
if (specialDays.containsKey(String.format("%1$tm%1$td", calendar)))
return true;
return false;
}
请注意,isSpecialDay()
方法需要一个 specialDays
对象,其中包含特殊日子的列表,例如耶稣升天节、圣诞节等...
在chapter 5 of the same book, I describe how to use events to change the way the table is rendered (rounded corners, special colors,...). This example is also named PdfCalendar.
顺带一提,这两章是官网搜索关键词Calendar最先出现的。你怎么没找到这些例子?
另外:你为什么要一张图片?您想在 PDF 中使用这些日历 table,那么为什么要创建光栅图像?放大时光栅图像看起来真的很糟糕。按照我的书中所述创建 table,将生成高质量的 PDF。