如何在 iTextSharp 中引入上标?

Dha*_*n T 2 html c# pdf itextsharp

我想要像 2015 年 5 月 14 日这样的输出格式日期。14 天它应该带有 sup 标签,但 sup 标签没有在此处访问。我在 ph102 变量中得到的输出。Getsuffix(csq.EventDate.Value.Day) 在这个只有我得到 th st 和 rd 的日期后缀

我的代码:

PdfPTable table9 = new PdfPTable(4);
table9.WidthPercentage = 99;
table9.DefaultCell.Border = 0;
table9.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
Phrase ph101 = new Phrase("Event Date & Time", textFont2);
PdfPCell cellt1 = new PdfPCell(ph101);
cellt1.Border = PdfPCell.BOTTOM_BORDER + PdfPCell.LEFT_BORDER + PdfPCell.RIGHT_BORDER;
cellt1.PaddingTop = 0F;
cellt1.VerticalAlignment = Element.ALIGN_MIDDLE;
cellt1.HorizontalAlignment = Element.ALIGN_LEFT;
DateTime eventTime = DateTime.Today;
if (!string.IsNullOrEmpty(Convert.ToString(csq.EventTime)))
    eventTime = DateTime.Today.Add(csq.EventTime.Value);
Phrase ph102;
if (!string.IsNullOrEmpty(csq.EventDate.ToString()))
{
    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);
}
else
{
    ph102 = new Phrase();
}
PdfPCell cellt2 = new PdfPCell(ph102);
cellt2.Border = PdfPCell.BOTTOM_BORDER + PdfPCell.LEFT_BORDER + PdfPCell.RIGHT_BORDER;
cellt2.PaddingTop = 0F;
cellt2.VerticalAlignment = Element.ALIGN_MIDDLE;
cellt2.HorizontalAlignment = Element.ALIGN_LEFT;
Run Code Online (Sandbox Code Playgroud)

Bru*_*gie 5

当我阅读你的问题时,我假设你想要这样的东西:

在此处输入图片说明

但是,正如有人给您提示使用 HTML 到 PDF 的评论中所证明的那样,您使人们感到困惑。您的回答是“不,先生,它无法工作”,这是一个奇怪的答案,因为它可以工作。当您谈论 sup 标签时,这不是您的意思。至少,这就是我在查看您的代码时所假设的,我没有看到任何 HTML。

在你的代码中,你创建一个Phrase这样的:

 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);
Run Code Online (Sandbox Code Playgroud)

这完全Phrase是在表示textFont7不能在你的情况下工作,因为你想用较小的字体为"st""nd""rd""th"

你需要做这样的事情(请参阅OrdinalNumbers以获取完整示例):

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();
}
Run Code Online (Sandbox Code Playgroud)

这是在屏幕截图中创建 PDF的Java代码。您必须修改您的代码,以便它在 C# 中工作。如您所见,您不能仅string使用+运算符连接您的s 。您需要组合PhraseParagraph使用不同的Chunk对象。你所说的“sup”是通过使用较小的字体和文本上升来完成的。