【问题标题】:iText PDF Stretch a characteriText PDF 拉伸一个字符
【发布时间】:2013-06-05 06:44:10
【问题描述】:

我正在使用 iText PDF 2.1.7 创建 PDF。任何人都知道iText PDF是否可以拉伸单个字符的高度(右花括号,ASCII 125)?

拉伸花括号的高度取决于前一个单元格的高度。 (请原谅我的示例图片,上大括号应该比下大括号高。

如果无法拉伸单个字符的高度,您可以建议任何其他仍使用 iText PDF 库的解决方法吗?

非常感谢。


我现在使用的是 iText 5.4.0 版。

我在缩放图像高度时遇到问题。此外,虽然我复制了单元格的位置,但图像的位置错误。大括号底部不显示。

您知道我缩放图像的方式和定位方式有什么问题吗?谢谢。

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

【问题讨论】:

    标签: pdf itext


    【解决方案1】:

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

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

    我会将PdfTemplate 包装在Image 对象中。别担心:这不会将您的“矢量”模板变成“光栅”图像。 Image 对象只是让操作模板变得更容易。

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

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

    【讨论】:

    猜你喜欢
    • 2018-12-11
    • 2018-05-27
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    相关资源
    最近更新 更多