2015-01-21 159 views
2

我需要一个有多个列的表格,其中我在不同单元格中具有不同的彩色圆圈,而圆圈中间有一个数字。 类似于下面的模型,但是一切都集中和平等。itextsharp在表格单元格中的圆形图像上添加文字

enter image description here

我曾尝试以下:

 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); 

但我结束了这样的事情。 (我还没有尝试设置颜色)。我无法让图像和文字坐在同一个地方。

enter image description here

我自己也尝试下面这个帖子:

iTextSharp - Text overlapping image

这也解释了如何把一个事件在小区设置单元格的背景图像,但我的圈子里出现一半在页面下方。

有没有人去这个工作的例子?

回答

0

有很多不同的方法来实现你想要的。

其实,我只是投票决定关闭问题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"); 
+0

谢谢,这真是太棒了!我现在在我的圈子里有我的号码,但是我怎样才能将整个事情整合到我的PPCPCell的中心? – Bex 2015-01-21 13:59:40

+0

在文本模式下工作时:将单元格的对齐方式更改为Element.ALIGN_CENTER;在复合模式下工作时:将“Paragraph”的对齐方式改为“Element.ALIGN_CENTER”。如果对文本或复合模式有疑问,请查阅[免费电子书](https://leanpub.com/itext_so) – 2015-01-21 15:57:20

相关问题