gpt4 book ai didi

java - 如何在内存中创建日历图像以添加到 PDF?

转载 作者:行者123 更新时间:2023-12-01 10:21:53 25 4
gpt4 key购买 nike

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

我在想,如果我无法解决这个问题,请在接下来的 36 个月内手动创建这些图像,然后持续这样做。显然这是最后的手段

最佳答案

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

chapter 4在《iText in Action - 第二版》中,我写了 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。

关于java - 如何在内存中创建日历图像以添加到 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com