How to introduce superscript?

I want to format a date like this: 14th may 2015. However, the th in 14 should be in superscript as is what happens when you use the "sup" tag.

This is my code:

 

ph102 = new Phrase(csq.EventDate.Value.Day
        +  Getsuffix(csq.EventDate.Value.Day)
        + csq.EventDate.Value.ToString("MMM")
        + " " + csq.EventDate.Value.Year
        + " at " + eventTime.ToString("hh:mm tt"), textFont7);
PdfPCell cellt2 = new PdfPCell(ph102);

Posted on StackOverflow on May 19, 2015 by Dhasarathan T

When I read your question, I assume that you want something like this:

Superscript

Superscript

In your code, you create a Phrase like this:

 ph102 = new Phrase(csq.EventDate.Value.Day
         +  Getsuffix(csq.EventDate.Value.Day)
         + csq.EventDate.Value.ToString("MMM")
         + " " + csq.EventDate.Value.Year
         + " at " + eventTime.ToString("hh:mm tt"), textFont7);

This complete Phrase is expressed in textFont7 which can't work in your case, because you want to use a smaller font for the "st", "nd", "rd" or "th".

You need to do something like this (see OrdinalNumbers for the full example):

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    Font small = new Font(FontFamily.HELVETICA, 6);
    Chunk st = new Chunk("st", small);
    st.setTextRise(7);
    Chunk nd = new Chunk("nd", small);
    nd.setTextRise(7);
    Chunk rd = new Chunk("rd", small);
    rd.setTextRise(7);
    Chunk th = new Chunk("th", small);
    th.setTextRise(7);
    Paragraph first = new Paragraph();
    first.add("The 1");
    first.add(st);
    first.add(" of May");
    document.add(first);
    Paragraph second = new Paragraph();
    second.add("The 2");
    second.add(nd);
    second.add(" and the 3");
    second.add(rd);
    second.add(" of June");
    document.add(second);
    Paragraph fourth = new Paragraph();
    fourth.add("The 4");
    fourth.add(rd);
    fourth.add(" of July");
    document.add(fourth);
    document.close();
}

This is the Java code to create the PDF in the screen shot. You'll have to adapt your code so that it works in C#. As you can see, you can not just concatenate your strings using the + operator. You need to compose your Phrase or Paragraph using different Chunk objects. What you call "sup" is done by using a smaller font and a text rise.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now