1

I'm using iText PDF 2.1.7 to create a PDF. Anyone has an idea if stretching the height of a single character (a closing curly brace, ASCII 125), is possible with iText PDF?

Stretched closing curly brace

The height of the stretched curly brace will depend on the height of the preceding cell's height. (Pardon my sample image, the upper curly brace should be taller than the lower curly brace.

If stretching the height of a single character is not possible, any other workaround you could suggest that still utilizes iText PDF library?

Thanks much.


Curly brace

I'm now using the iText version 5.4.0.

I'm having problem with scaling the height of the Image. Also, the Image is on the wrong position, though I copied the Cell's position. The bottom part of the curly brace is not displayed.

Do you know what is wrong with the way I scale the Image and on how I position it? Thanks.

public void cellLayout(PdfPCell cell, Rectangle position,
    PdfContentByte[] canvases) {
    try {

        BaseFont bf = BaseFont.createFont(
            CreatePDFCurlyBrace.DEFAULT_ENGLISH_FONT_TTF, BaseFont.IDENTITY_H,
            BaseFont.EMBEDDED);
        String curlyBrace = "}";
        float width = bf.getWidthPoint(curlyBrace, 12);
        float height = bf.getAscentPoint(curlyBrace, 12)
            - bf.getDescentPoint(curlyBrace, 12);

        PdfTemplate template = this.cb.createTemplate(width, height);
        template.beginText();
        template.setFontAndSize(bf, 12);
                    template.setTextMatrix(0, bf.getDescentPoint(curlyBrace, 12));
        template.showText(curlyBrace);
        template.endText();

        // Scale the image so that its height corresponds 
        //   with the height of the cell
        Image img = Image.getInstance(template);
        img.scaleToFit(1000f, position.getHeight());
        img.setAbsolutePosition(position.getRight(), position.getBottom());
                    cb.addImage(img);

    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
4

1 回答 1

0

如果左边是一个单元格,右边是一个单元格,而花括号是单元格之间的“边界”,我将为左边或右边的单元格定义一个单元格事件。

我会创建一个简单PdfTemplate的,我会添加一个花括号。我将 PdfTemplate 的尺寸基于花括号的度量信息(上升 - 下降 = 实际高度)。我不在乎字体大小;我们只需要一个带有花括号字形的 PdfTemplate 对象。

我会包裹一个物体的PdfTemplate内部。Image别担心:这不会将您的“矢量”模板变成“光栅”图像。该Image对象只是使操作模板变得更容易。

我将cellLayout()在单元格事件中实现该方法,并缩放图像,使其高度与单元格的高度一致(不损失分辨率:您使用的是 PdfTemplate,您正在添加矢量数据,而不是光栅图像) ,并将图像添加到正确的位置(cellLayout()使用参数传递给方法position)。

不用说,我不会使用 iText 2.1.7(4 岁的版本!)来完成这项任务,因为几年前我已经修复了一个导致PdfPTable百万分之一丢失的错误次。为了避免这个错误,我不会欺骗我的客户使用被认为已过时的 iText 版本:http: //lowagie.com/itext2

于 2013-06-05T07:28:45.960 回答