itextsharp add text over a circle image in a table cell

孤人 提交于 2019-12-01 11:25:51
Bruno Lowagie

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!