c# - 如何在iTextSharp中引入上标?

标签 c# html pdf itext

我想要像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;

最佳答案

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

enter image description here

但是,您让人们感到困惑,正如有人提示您使用 HTML 转 PDF 的评论所示。您的答案是“不,先生,它不能工作”,这是一个奇怪的答案,因为它可以工作。当你谈论sup tag时,这不是你的意思。至少,当我查看您的代码时,我是这么认为的,我没有看到任何 HTML。

在您的代码中,您创建一个如下所示的短语:

 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);

这个完整的短语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 对象来组成您的 PhraseParagraph。您所谓的“sup”是通过使用较小的字体和文本上升来完成的。

关于c# - 如何在iTextSharp中引入上标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30322665/

相关文章:

c# - 数组何时出错?

c# - 断言我的 C# 函数抛出与另一个函数相同的异常

c# - 使用 Lambda 作为 If 语句的参数

python - 使用 Python 进行独立于操作系统的打印

pdf - 无法在 Safari 上下载 pdf blob url

pdf - 如何使用 pdftk 和/MediaBox 裁剪 PDF 边距

c# - 比较 2 字节数组

jquery - 如何在悬停时更改列表中下拉菜单的颜色?

javascript - 为什么我不能用 jQuery 触发这个 keydown 事件?

jquery - 语义 UI 模式设置最大宽度?