Skip to main content
 首页 » 编程设计

c#之如何在 iTextSharp 中引入上标

2024年02月06日17lyhabc

我想要输出格式日期,如 2015 年 5 月 14 日。14 中的第 th 它应该带有 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; 

请您参考如下方法:

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

但是,正如有人给您提示使用 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); 

这个完整的 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(); 
} 

这是在屏幕截图中创建 PDF 的 Java 代码。您必须调整您的代码才能在 C# 中运行。如您所见,您不能仅使用 + 运算符连接您的 string。您需要使用不同的 Chunk 对象来组成您的 Phrase 或 Paragraph。您所说的“sup”是通过使用较小的字体和上升的文本来完成的。