【问题标题】:How to introduce superscript in iTextSharp?如何在 iTextSharp 中引入上标?
【发布时间】:2015-07-31 02:33:19
【问题描述】:

我想要像 2015 年 5 月 14 日这样的输出格式日期。14 中的 th 应该带有 sup 标签,但 sup 标签无法在此处访问。我在 ph102 变量中得到的输出。 Getsuffix(csq.EventDate.Value.Day) 在这里我只得到日期的 th 和 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;

【问题讨论】:

  • 正如布鲁诺所说,您的问题非常令人困惑。您的实际问题是“如何制作上标文字”?另外,当您说“sup tag”和“HTML”时,您只是将其用于比较目的,而实际上您并没有以任何方式解析 HTML?

标签: c# html pdf itextsharp


【解决方案1】:

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

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

这个完整的PhrasetextFont7 表示,这在您的情况下不起作用,因为您想为"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# 中工作。如您所见,您不能只使用+ 运算符连接您的strings。您需要使用不同的Chunk 对象来编写PhraseParagraph。你所谓的“sup”是通过使用较小的字体和文字上升来完成的。

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2011-01-05
    • 2012-06-23
    • 2014-03-04
    • 2012-10-18
    相关资源
    最近更新 更多