Skip to main content
Skip table of contents

How to introduce superscript?

How do I format dates so that the "th" in 14th or "rd" in 3rd is in 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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.