itextsharp add text over a circle image in a table cell

前端 未结 1 1876
故里飘歌
故里飘歌 2021-01-15 01:44

I need to have a table with multiple columns where I have different coloured circles in different cells with a number in the middle of the circle. Similar to the mockup belo

相关标签:
1条回答
  • 2021-01-15 02:05

    There are many different ways to achieve what you want.

    Actually, I just voted to close a question https://stackoverflow.com/questions/28066639/how-to-get-text-on-image-in-itext-using-java because it was a duplicate of How to add text to an image?

    In your case, you may also benefit from the documentation. There are some examples in The Best iText Questions on StackOverflow 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.

    You could use cell events to draw the background of a cell, as shown in the Calendar examples: calendar.pdf, but your requirement can also be met using a generic tag event: movie_years.pdf

    Do you see how the word IMDB nicely fits inside an ellipse? You could fit a number inside a circle in the exact same way.

    With this code, one draws an ellipse (changing it into a circle is fairly easy):

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

    With this snippet, you introduce this event:

    GenericTags gevent = new GenericTags();
    writer.PageEvent = gevent;
    

    With this snippet, you mark a Chunk with the generic tag:

    chunk.SetGenericTag("circle");
    
    0 讨论(0)
提交回复
热议问题