iText(夏普) - 使用单元格事件直接绘制时文本不同 [英] iText(Sharp) - Text is different when drawn with cell event vs. directly

查看:113
本文介绍了iText(夏普) - 使用单元格事件直接绘制时文本不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PdfPTable,其中我正在绘制简单文本:

I've got a PdfPTable in which I am drawing simple text:

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font f2 = new Font(bf, 10.0f);
PdfPTable tab = new PdfPTable(2);
table.AddCell(new PdfPCell(new Phrase("Dude", f2)));

这一切都很好,花花公子。但是,为了做一些更高级的东西,我试图得到单元事件工作:

And that's all fine and dandy. But then in order to do some fancier stuff, I'm trying to get cell events to work:

Font f2 = new Font(bf, 10.0f);   // Pretend this is a global
class CellEvent : IPdfPCellEvent
{
    void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle r, PdfContentByte[] canvases)
    {
        ColumnText ct = new ColumnText(canvases[0]);
        ct.SetSimpleColumn(r);
        ct.AddElement(new PdfPCell(new Phrase("Dude", f2)));
        ct.Go();
    }
}
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfPTable tab = new PdfPTable(2);
PdfPCell ce = new PdfPCell();
ce.CellEvent = new CellEvent();
table.AddCell(ce);

当我这样做时,细胞的绘制方式完全不同。我的实际文本是多行长,当直接绘制时(将短语直接添加到单元格),文本行在垂直方向上非常接近;当通过单元格事件绘制时,文本行之间有很多空格,它不适合PdfPTable为我提供的框。

When I do this though, the cell is drawn quite differently. My actual text is multiple lines long, and when drawn directly (adding the phrase directly to the cell) the text lines are quite close together vertically; when drawn via the cell event, there is a lot of space between the text lines and it doesn't fit in the box the PdfPTable provides for me.

是否有在IPdfPCellEvent.CellLayout()中绘制文本的另一种更优选的方法是什么?或者我只需要将一些格式参数从PdfPCell复制到ColumnText中?

Is there a different, more preferred method for drawing text inside IPdfPCellEvent.CellLayout()? Or are there just some formatting parameters I need to copy from PdfPCell into ColumnText?

推荐答案

这个片段有一些问题:

There is something very wrong with this snippet:

ColumnText ct = new ColumnText(canvases[0]);
ct.SetSimpleColumn(r);
ct.AddElement(new PdfPCell(new Phrase("Dude", f2)));
ct.Go();

更具体地说:

ct.AddElement(new PdfPCell(new Phrase("Dude", f2)));

你不能在外面使用 PdfPCell 对象 PdfPTable 的上下文。

You can not use the PdfPCell object outside of the context of a PdfPTable.

至于 PdfPCell <中的内容之间的差异/ code>和 ColumnText ,您可能想要阅读这些问题的答案:

As for the differences between content in PdfPCell and ColumnText, you may want to read the answers to these questions:

  • itext ColumnText ignores alignment
  • Right aligning text in PdfPCell

PdfPCell 的内容实际上存储在 ColumnText 对象中,但是 PdfPCell 可能有填充。

The content of a PdfPCell is actually stored in a ColumnText object, but a PdfPCell may have a padding.

还有文字模式复合模式。你在谈论两条线之间的距离。在文本模式中,此距离在 PdfPCell / ColumnText 级别定义 setLeading()方法。在复合模式中,将忽略此参数。相反,使用添加到 PdfPCell / ColumnText 的元素的前导。

There's also text mode versus composite mode. You are talking about the distance between two lines. In text mode, this distance is defined at the level of the PdfPCell/ColumnText using the setLeading() method. In composite mode, this parameter is ignored. Instead, the leading of the elements added to the PdfPCell/ColumnText is used.

例如:if p1 p2 p3 是三个段落对象,每个对象都有不同的前导,然后是 ct 如果你这样做,将有三个不同引导的文本:

For instance: if p1, p2 and p3 are three Paragraph objects, each having a different leading, then ct will have text with three different leadings if you do this:

ct.addElement(p1);
ct.addElement(p2);
ct.addElement(p3);

这篇关于iText(夏普) - 使用单元格事件直接绘制时文本不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆