itextsharp 在 table 单元格中的圆形图像上添加文本
itextsharp add text over a circle image in a table cell
我需要一个包含多个列的 table,其中不同单元格中有不同颜色的圆圈,圆圈中间有一个数字。
类似于下面的模型,但一切都集中且平等。
我试过以下方法:
PdfContentByte canvas = writer.DirectContent;
PdfTemplate template = canvas.CreateTemplate(40, 40);
template.SetLineWidth(1f);
template.Circle(15f, 15f, 15);
template.Stroke();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(template);
img.Alignment = iTextSharp.text.Image.UNDERLYING | iTextSharp.text.Image.ALIGN_CENTER;
Phrase imgPhrase = new Paragraph(new Chunk(img, 1f, 1f));
PdfPCell meAnswerCell = new PdfPCell();
meAnswerCell.Colspan = 1;
meAnswerCell.BorderWidthBottom = 0;
meAnswerCell.HorizontalAlignment = Element.ALIGN_CENTER;
string meAnswerText = "1;
Phrase phrase = new Phrase(meAnswerText, questionFont);
Paragraph para = new Paragraph();
para.Add(imgPhrase);
para.Add(phrase);
para.Alignment = Element.ALIGN_CENTER;
meAnswerCell.AddElement(para);
answersTable.AddCell(meAnswerCell);
但我最终得到了这样的结果。 (我还没有尝试设置颜色)。我无法让图片和文字放在同一个地方。
我也试过按照这个 post:
iTextSharp - Text overlapping image
这解释了如何在单元格上放置一个事件来设置单元格的背景图像,但我的圆圈出现在页面的中间位置。
有人举过这个工作的例子吗?
有许多不同的方法可以实现您想要的。
实际上,我刚刚投票结束了一个问题 because it was a duplicate of How to add text to an image?
对于您的情况,您还可以从文档中受益。 The Best iText Questions on Whosebug that explain how to use cell events, but your requirement is very similar to what was done in a couple of examples from the book iText in Action - Second Edition.
中有一些示例
您可以使用单元格事件来绘制单元格的背景,如日历示例所示:calendar.pdf, but your requirement can also be met using a generic tag event: movie_years.pdf
您看到 IMDB 这个词如何很好地适合椭圆形了吗?您可以用完全相同的方式在圆圈内放置一个数字。
用这段代码画一个椭圆(改成圆很容易):
class GenericTags : PdfPageEventHelper {
public override void OnGenericTag(
PdfWriter writer, Document pdfDocument, Rectangle rect, String text) {
PdfContentByte content = writer.DirectContentUnder, rect);
content.SaveState();
content.SetRGBColorFill(0x00, 0x00, 0xFF);
content.Ellipse(
rect.Left - 3f, rect.Bottom - 5f,
rect.Right + 3f, rect.Top + 3f
);
content.Fill();
content.RestoreState();
}
}
通过这段代码,您介绍了这个事件:
GenericTags gevent = new GenericTags();
writer.PageEvent = gevent;
使用此代码段,您可以使用通用标签标记 Chunk
:
chunk.SetGenericTag("circle");
我需要一个包含多个列的 table,其中不同单元格中有不同颜色的圆圈,圆圈中间有一个数字。 类似于下面的模型,但一切都集中且平等。
我试过以下方法:
PdfContentByte canvas = writer.DirectContent;
PdfTemplate template = canvas.CreateTemplate(40, 40);
template.SetLineWidth(1f);
template.Circle(15f, 15f, 15);
template.Stroke();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(template);
img.Alignment = iTextSharp.text.Image.UNDERLYING | iTextSharp.text.Image.ALIGN_CENTER;
Phrase imgPhrase = new Paragraph(new Chunk(img, 1f, 1f));
PdfPCell meAnswerCell = new PdfPCell();
meAnswerCell.Colspan = 1;
meAnswerCell.BorderWidthBottom = 0;
meAnswerCell.HorizontalAlignment = Element.ALIGN_CENTER;
string meAnswerText = "1;
Phrase phrase = new Phrase(meAnswerText, questionFont);
Paragraph para = new Paragraph();
para.Add(imgPhrase);
para.Add(phrase);
para.Alignment = Element.ALIGN_CENTER;
meAnswerCell.AddElement(para);
answersTable.AddCell(meAnswerCell);
但我最终得到了这样的结果。 (我还没有尝试设置颜色)。我无法让图片和文字放在同一个地方。
我也试过按照这个 post:
iTextSharp - Text overlapping image
这解释了如何在单元格上放置一个事件来设置单元格的背景图像,但我的圆圈出现在页面的中间位置。
有人举过这个工作的例子吗?
有许多不同的方法可以实现您想要的。
实际上,我刚刚投票结束了一个问题 because it was a duplicate of How to add text to an image?
对于您的情况,您还可以从文档中受益。 The Best iText Questions on Whosebug that explain how to use cell events, but your requirement is very similar to what was done in a couple of examples from the book iText in Action - Second Edition.
中有一些示例您可以使用单元格事件来绘制单元格的背景,如日历示例所示:calendar.pdf, but your requirement can also be met using a generic tag event: movie_years.pdf
您看到 IMDB 这个词如何很好地适合椭圆形了吗?您可以用完全相同的方式在圆圈内放置一个数字。
用这段代码画一个椭圆(改成圆很容易):
class GenericTags : PdfPageEventHelper {
public override void OnGenericTag(
PdfWriter writer, Document pdfDocument, Rectangle rect, String text) {
PdfContentByte content = writer.DirectContentUnder, rect);
content.SaveState();
content.SetRGBColorFill(0x00, 0x00, 0xFF);
content.Ellipse(
rect.Left - 3f, rect.Bottom - 5f,
rect.Right + 3f, rect.Top + 3f
);
content.Fill();
content.RestoreState();
}
}
通过这段代码,您介绍了这个事件:
GenericTags gevent = new GenericTags();
writer.PageEvent = gevent;
使用此代码段,您可以使用通用标签标记 Chunk
:
chunk.SetGenericTag("circle");