我有一个要求,其中在日历中我需要根据业务逻辑设置不同的图像。日历是使用pdfpTable创建的,请通过链接http://kuujinbo.info/cs/itext_calendar.aspx来创建我的自定义日历。
有一个带有日期和状态的数据集,需要对照日历中的每个日期进行检查。例如,如果日期为1,状态为良好,则添加image1 如果日期为1,状态为NONCOMM,则添加image2。
我为所有图像创建了不同的单元事件,但是为所有单元分配了相同的图像。在调试时,显示的是不同的单元格事件,但是最终的pdf输出显示表中所有具有相同事件的单元格。
下面是我正在使用的完整代码,同时还将ImageExpected(注释的代码输出)和ImageError结果附加到下面的代码中,如果我在错误的地方得到了任何帮助,请感谢。
这是我的期望:ImageExpected
这就是我得到的:Image Error
DataTable listStatus的Date&Status列为
日期状态 1/7/2018良好 于2018年2月7日完成 2018年3月7日NONCOMM ..... 2018/7/30良好
private void CreateCalendar(DataTable listStatus)
{
CalculateDates(currentYear, currentMonth);
IntializeHeadings();
float height = 40f;
PdfPCell day = new PdfPCell();
day.PaddingTop = 0;
GoodEvent GoodEvent = new GoodEvent();
PMCreatedEvent PMCreatedEvent = new PMCreatedEvent();
UMRemoteEvent UMRemoteEvent = new UMRemoteEvent();
UMFieldEvent UMFieldEvent = new UMFieldEvent();
PMPerformedEvent PMPerformedEvent = new PMPerformedEvent();
NonCommEvent NonCommEvent = new NonCommEvent();
int count = 0;
int dayCounter = 0;
try
{
for (int i = 0; i < calRows; ++i)
{
// set fixed row height
day.FixedHeight = height;
int y = listStatus.Rows.Count;
for (int j = 0; j < 7; ++j)
{
string daynum = count >= firstOffsOfmonth && count < (firstOffsOfmonth + daysinMonth) ? (++dayCounter).ToString() : "";
// This commented code is working and setting properly the cell event
//if (daynum != "" && dayCounter <= DateTime.Now.Day)
//{
// day.CellEvent = GoodEvent;
//}
//else if (daynum != "" && dayCounter==20)
//{
// day.CellEvent = PMCreatedEvent;
//}
//else
//{
// day.CellEvent = null;
//}
// Below code not working and setting all event as GoodEvent
if (listStatus.Rows.Count != 0)
{
DateTime tranDate = Convert.ToDateTime(listStatus.Rows[0]["Date"].ToString());
string tranStatus = listStatus.Rows[0]["Status"].ToString();
int tranDay = Convert.ToInt32(tranDate.Day.ToString());
if (daynum != "" && dayCounter == tranDay && tranStatus == "GOOD")
{
day.CellEvent=GoodEvent;
}
else if (daynum != "" && dayCounter == tranDay && tranStatus == "PMCREATED")
{
day.CellEvent = PMCreatedEvent;
}
else if (daynum != "" && dayCounter == tranDay && tranStatus == "UMREMOTE")
{
day.CellEvent = UMRemoteEvent;
}
else if (daynum != "" && dayCounter == tranDay && tranStatus == "UMFIELD")
{
day.CellEvent = UMFieldEvent;
}
else if (daynum != "" && dayCounter == tranDay && tranStatus == "PMPERFORMED")
{
day.CellEvent = PMPerformedEvent;
}
else if (daynum != "" && dayCounter == tranDay && tranStatus == "NONCOMM")
{
day.CellEvent = NonCommEvent;
}
else
{
day.CellEvent = null;
}
listStatus.Rows[0].Delete(); // Deleting the table so that after 7 days its goes to next row of table
listStatus.AcceptChanges();
}
day.Phrase = new Phrase(daynum, fontday);
pTableCal.AddCell(day);
++count;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error encountered " + ex);
}
}
private class GoodEvent: IPdfPCellEvent
{
Image imgGoodIcon = Image.GetInstance(imgGood);
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(cb);
// set exact coordinates for ColumnText
ct.SetSimpleColumn(
position.Left + 2, // lower-left x; add some padding
position.Bottom, // lower-left y
position.Right, // upper-right x
position.Top // upper-right x; adjust for existing content
- cellLeading - 3
);
this.imgGoodIcon.ScaleToFit(25f, 25f);
this.imgGoodIcon.Alignment = Element.ALIGN_MIDDLE;
ct.AddElement(this.imgGoodIcon);
ct.Go();
}
}
private class PMCreatedEvent : IPdfPCellEvent
{
Image imgPMCreatedIcon = Image.GetInstance(imgPMCreated);
public void CellLayout(
PdfPCell cell, Rectangle position, PdfContentByte[] canvases
)
{
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(cb);
// set exact coordinates for ColumnText
ct.SetSimpleColumn(
position.Left + 2, // lower-left x; add some padding
position.Bottom, // lower-left y
position.Right, // upper-right x
position.Top // upper-right x; adjust for existing content
- cellLeading - 3
);
imgPMCreatedIcon.ScaleToFit(25f, 25f);
imgPMCreatedIcon.Alignment = Element.ALIGN_MIDDLE;
ct.AddElement(imgPMCreatedIcon);
ct.Go();
}
}
private class UMRemoteEvent : IPdfPCellEvent
{
Image imgUmRemoteIcon = Image.GetInstance(imgUmRemote);
public void CellLayout(
PdfPCell cell, Rectangle position, PdfContentByte[] canvases
)
{
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(cb);
// set exact coordinates for ColumnText
ct.SetSimpleColumn(
position.Left + 2, // lower-left x; add some padding
position.Bottom, // lower-left y
position.Right, // upper-right x
position.Top // upper-right x; adjust for existing content
- cellLeading - 3
);
imgUmRemoteIcon.ScaleToFit(25f, 25f);
imgUmRemoteIcon.Alignment = Element.ALIGN_MIDDLE;
ct.AddElement(imgUmRemoteIcon);
ct.Go();
}
}
private class UMFieldEvent : IPdfPCellEvent
{
Image imgUmFieldIcon = Image.GetInstance(imgUmField);
public void CellLayout(
PdfPCell cell, Rectangle position, PdfContentByte[] canvases
)
{
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(cb);
// set exact coordinates for ColumnText
ct.SetSimpleColumn(
position.Left + 2, // lower-left x; add some padding
position.Bottom, // lower-left y
position.Right, // upper-right x
position.Top // upper-right x; adjust for existing content
- cellLeading - 3
);
imgUmFieldIcon.ScaleToFit(25f, 25f);
imgUmFieldIcon.Alignment = Element.ALIGN_MIDDLE;
ct.AddElement(imgUmFieldIcon);
ct.Go();
}
}
private class PMPerformedEvent : IPdfPCellEvent
{
Image imgPmPerformedIcon = Image.GetInstance(imgPmPerformed);
public void CellLayout(
PdfPCell cell, Rectangle position, PdfContentByte[] canvases
)
{
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(cb);
// set exact coordinates for ColumnText
ct.SetSimpleColumn(
position.Left + 2, // lower-left x; add some padding
position.Bottom, // lower-left y
position.Right, // upper-right x
position.Top // upper-right x; adjust for existing content
- cellLeading - 3
);
imgPmPerformedIcon.ScaleToFit(25f, 25f);
imgPmPerformedIcon.Alignment = Element.ALIGN_MIDDLE;
ct.AddElement(imgPmPerformedIcon);
ct.Go();
}
}
private class NonCommEvent : IPdfPCellEvent
{
Image imgNonCommIcon = Image.GetInstance(imgNonComm);
public void CellLayout(
PdfPCell cell, Rectangle position, PdfContentByte[] canvases
)
{
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(cb);
// set exact coordinates for ColumnText
ct.SetSimpleColumn(
position.Left + 2, // lower-left x; add some padding
position.Bottom, // lower-left y
position.Right, // upper-right x
position.Top // upper-right x; adjust for existing content
- cellLeading - 3
);
imgNonCommIcon.ScaleToFit(25f, 25f);
imgNonCommIcon.Alignment = Element.ALIGN_MIDDLE;
ct.AddElement(imgNonCommIcon);
ct.Go();
}
}
答案 0 :(得分:0)
我已经通过将Cellvent添加到表中之后重置Cellvent来实现了目标,以下是我修改的代码段。并能够根据业务逻辑获得具有图像的所需日历。CorrectImage
希望这对希望达到类似要求的人有所帮助。
if (listStatus.Rows.Count != 0)
{
DateTime tranDate = Convert.ToDateTime(listStatus.Rows[0]["TransactionDate"].ToString());
string tranStatus = listStatus.Rows[0]["Status"].ToString();
int tranDay = Convert.ToInt32(tranDate.Day.ToString());
listStatus.Rows[0].Delete();
listStatus.AcceptChanges();
if (daynum != "" && dayCounter == tranDay && tranStatus == "GOOD")
{
day.CellEvent=GoodEvent;
}
if (daynum != "" && dayCounter == tranDay && tranStatus == "PMCREATED")
{
day.CellEvent = PMCreatedEvent;
}
if (daynum != "" && dayCounter == tranDay && tranStatus == "UMREMOTE")
{
day.CellEvent = UMRemoteEvent;
}
if (daynum != "" && dayCounter == tranDay && tranStatus == "UMFIELD")
{
day.CellEvent = UMFieldEvent;
}
if (daynum != "" && dayCounter == tranDay && tranStatus == "PMPERFORMED")
{
day.CellEvent = PMPerformedEvent;
}
if (daynum != "" && dayCounter == tranDay && tranStatus == "NONCOMM")
{
day.CellEvent = NonCommEvent;
}
}
day.Phrase = new Phrase(daynum, fontday);
pTableCal.AddCell(day);
day.CellEvent = null;
++count;
}