gpt4 book ai didi

c# - iTextSharp - 无法在表格单元背景颜色上绘制矩形

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:44 25 4
gpt4 key购买 nike

我正在使用 iTextSharp 创建一个 PDF,以 HTML 页面为例。我试图在文本周围放置边框以突出显示某些项目。使用这些帖子(Draw Rect at curr positionAdd Text over image)中的方法,我使用 OnGenericTag 事件为符合特定条件的每个 block 创建矩形。事件正确触发,矩形绘制在正确的元素上。但是,当我为 tablecell 添加背景时,问题出现了,它出现在 Chunk 矩形的顶部。有没有办法使从表格单元格背景上方绘制的 OnGenericTag 事件绘制的矩形?


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 页面。它显示了表格单元格的背景颜色和需要在它们周围有一个矩形以突出显示它们的值。 Heat map from HTML

这是渲染 PDF 时的结果。突出显示的 R# 匹配,但是,当我将背景颜色应用于 PdfPCell 时,它会绘制在 Chunk 的矩形上 heat map from pdf

最佳答案

使用 writer.DirectContentUnder 层绘制表格以确保矩形保持在顶部 (writer.DirectContent)。

关于c# - iTextSharp - 无法在表格单元背景颜色上绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905978/

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