我需要将日历图像添加到我在Java应用程序中创建的PDF中。使用iText创建PDF,这不是问题。我只是不知道如何动态创建日历图像?日历必须准确显示整个月。我附上了2015年12月给定的样例
java - 如何在内存中创建日历图像以添加到PDF?-LMLPHP

我在想,如果无法解决此问题,请在接下来的36个月中手动创建这些图像,然后连续进行操作。显然那是不得已的方法

最佳答案

多么奇怪,您发布了一个要求,要求提供在官方文档中完全开发的示例。

在“ iText in Action-Second Edition”的chapter 4中,我写关于tables的内容,在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中,我描述了如何使用事件来更改表格的呈现方式(圆角,特殊颜色等)。此示例也称为PdfCalendar

顺便说一句,当您在官方网站上搜索关键字Calendar时,这两章是第一批。您怎么没找到这些例子?

另外:为什么要索取图片?您想在PDF中使用这些日历表,那么为什么要创建光栅图像?放大时,栅格图像看起来真的很差。按照我的书中的描述创建表格,将得到高质量的PDF。

09-04 14:27
查看更多