我需要一个包含多列的表格,其中不同单元格中有不同颜色的圆圈,圆圈中间有一个数字。 类似于下面的模型,但一切都集中且平等。
我尝试了以下方法:
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);
但我最终得到了这样的结果。 (我还没有尝试设置颜色)。我无法将图像和文本放在同一个位置。
我也试过关注这篇文章:
iTextSharp - Text overlapping image
这解释了如何在单元格上放置一个事件以设置单元格的背景图像,但我的圆圈出现在页面的中间位置。
有人举过这个工作的例子吗?
最佳答案
有许多不同的方法可以实现您想要的。
其实我只是投票结束了一个问题https://stackoverflow.com/questions/28066639/how-to-get-text-on-image-in-itext-using-java因为它是 How to add text to an image? 的副本
在您的情况下,您还可以从文档中受益。 The Best iText Questions on StackOverflow 中有一些示例这解释了如何使用单元格事件,但您的要求与书中的几个示例中所做的非常相似 iText in Action - Second Edition .
您可以使用单元格事件来绘制单元格的背景,如日历示例所示:calendar.pdf , 但您的要求也可以使用通用标记事件来满足: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");
关于c# - itextsharp 在表格单元格中的圆形图像上添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066895/