iTextSharp - 无法在表格单元背景颜色上绘制矩形
iTextSharp - Unable to draw rectangle over tablecell background color
我以 HTML 页面为例,使用 iTextSharp 创建 PDF。我试图在文本周围放置边框以突出显示某些项目。使用这些帖子 (Draw Rect at curr position, and ) 中的方法,我使用 OnGenericTag 事件为每个符合特定条件的块创建矩形。事件正确触发,矩形绘制在正确的元素上。但是,当我为 table 单元格添加背景时,问题出现了,它出现在块矩形的顶部。有没有办法让 OnGenericTag
事件绘制的矩形绘制在 table 单元格背景上方?
OnGenericTags 方法:
public class GenericTags : PdfPageEventHelper
{
public override void OnGenericTag(PdfWriter writer, Document pdfDocument, iTextSharp.text.Rectangle rect, String text)
{
if ("ellipse".Equals(text)) //I know it's not needed.
ellipse(writer.DirectContent, rect);
}
public void ellipse(PdfContentByte content, iTextSharp.text.Rectangle rect)
{
content.SaveState();
content.SetRGBColorStroke(0x00, 0x00, 0xFF);
content.SetLineWidth(2);
content.Rectangle(rect.Left - 2f, rect.Bottom - 3f, rect.Width, rect.Height + 3f);
content.Stroke();
content.RestoreState();
}
}
每个PdfPCell
定义为:
PdfPCell med = new PdfPCell(ph[4])
{
HorizontalAlignment = Element.ALIGN_CENTER,
VerticalAlignment = Element.ALIGN_CENTER,
BackgroundColor = hm_yellow //this draws above rect from onGenericTag()
};
Chunks
是通过 foreach
循环创建和分配的:
ph[j] = new Phrase();
foreach (var x in p.Risks)
{
c[i] = new Chunk("R" + x.APP_RISK_ID.ToString() + ", ");
if (x.RISK_STATUS_CD == "57")
{
//c[i].SetBackground(hm_top_tier); // still shows behind table cell background
c[i].SetGenericTag("ellipse");
}
ph[j].Add(c[i]);
}
这是我要转换为 PDF 的 HTML 页面。它显示 table 单元格背景颜色和需要在它们周围有一个矩形以突出显示它们的值。
这是渲染 PDF 时的结果。突出显示的 R# 匹配,但是,当我将背景颜色应用于 PdfPCell
时,它会绘制在 Chunk
的矩形上
使用图层 writer.DirectContentUnder
绘制 table 以确保矩形保持在顶部 (writer.DirectContent
)。
我以 HTML 页面为例,使用 iTextSharp 创建 PDF。我试图在文本周围放置边框以突出显示某些项目。使用这些帖子 (Draw Rect at curr position, and OnGenericTag
事件绘制的矩形绘制在 table 单元格背景上方?
OnGenericTags 方法:
public class GenericTags : PdfPageEventHelper
{
public override void OnGenericTag(PdfWriter writer, Document pdfDocument, iTextSharp.text.Rectangle rect, String text)
{
if ("ellipse".Equals(text)) //I know it's not needed.
ellipse(writer.DirectContent, rect);
}
public void ellipse(PdfContentByte content, iTextSharp.text.Rectangle rect)
{
content.SaveState();
content.SetRGBColorStroke(0x00, 0x00, 0xFF);
content.SetLineWidth(2);
content.Rectangle(rect.Left - 2f, rect.Bottom - 3f, rect.Width, rect.Height + 3f);
content.Stroke();
content.RestoreState();
}
}
每个PdfPCell
定义为:
PdfPCell med = new PdfPCell(ph[4])
{
HorizontalAlignment = Element.ALIGN_CENTER,
VerticalAlignment = Element.ALIGN_CENTER,
BackgroundColor = hm_yellow //this draws above rect from onGenericTag()
};
Chunks
是通过 foreach
循环创建和分配的:
ph[j] = new Phrase();
foreach (var x in p.Risks)
{
c[i] = new Chunk("R" + x.APP_RISK_ID.ToString() + ", ");
if (x.RISK_STATUS_CD == "57")
{
//c[i].SetBackground(hm_top_tier); // still shows behind table cell background
c[i].SetGenericTag("ellipse");
}
ph[j].Add(c[i]);
}
这是我要转换为 PDF 的 HTML 页面。它显示 table 单元格背景颜色和需要在它们周围有一个矩形以突出显示它们的值。
这是渲染 PDF 时的结果。突出显示的 R# 匹配,但是,当我将背景颜色应用于 PdfPCell
时,它会绘制在 Chunk
的矩形上
使用图层 writer.DirectContentUnder
绘制 table 以确保矩形保持在顶部 (writer.DirectContent
)。