如何在itextsharp中实现smallcaps [英] how to implement smallcaps in itextsharp

查看:95
本文介绍了如何在itextsharp中实现smallcaps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个PDf中我得到了小型大写字母,字体大小为0.0。我提取了字体

In one PDf i got small caps text the font size is 0.0. i have extracted font

using:------  

iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(curBaseline[Vector.I1], curBaseline[Vector.I2], topRight[Vector.I1], topRight[Vector.I2]);
Single curFontSize = rect.Height;

int此代码curfontsize我得到0.0。但我能够提取字体系列。但是无法提取fontsize。所以文字没有显示。任何人都为此提供解决方案。还有其他方法。?

int this code curfontsize i got 0.0. but i was able to extract font family.but not able to extract fontsize. so the text was not displaying.can anyone provide solution for this . Is there anyother method.?

thankyou

推荐答案

我刚试过重现你的 rec.Height == 0.0 问题

iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(curBaseline[Vector.I1],
    curBaseline[Vector.I2], topRight[Vector.I1], topRight[Vector.I2]);
Single curFontSize = rect.Height

这样做我假设你选择了 curBaseline 表示基线的起点, topRight 表示上升线的终点。我的结果(输出 Rectangle rect 的宽度和高度以及我<分别收到的每个 TextRenderInfo 对象的文本code> RenderListener ):

Doing so I assumed you chose curBaseline to mean the start point of the base line and topRight to mean the end point of the ascent line. My result (outputting both width and height of that Rectangle rect and the text respectively for each TextRenderInfo object received by my RenderListener):

[140,29,   6,66] "nagement en beleid. Via omscholing ("
[ 11,87,   5,00] "PDB"
[  4,95,   6,66] ", "
[  9,36,   5,00] "MB"
[  4,37,   5,00] "A"
[ 26,76,   6,66] ", deels "
[ 11,28,   5,00] "SPD"
[  5,82,   6,66] ") "
[ 88,66,   6,66] "ben ik in het financiële v"

所以我得到0.0的高度,而不是5.00代表PDB,MB, A,和SPD。

So I did not get a height of 0.0 but instead of 5.00 for "PDB", "MB", "A", and "SPD".

此结果是使用相当新的iText版本生成的。因此,您可能希望更新库作为第一个措施。如果这没有帮助,您可能需要在此处透露您的源代码。

This result is produced using a fairly current iText version. Thus, you might want to update your library as a first measure. If that does not help, you may want to reveal your source code to review here.

编辑

正如这里的评论所讨论的那样,我扩展了测试类以提取实际的字体大小( Tf 操作数的大小和应用当前转换和文本矩阵后的大小) 。结果:

As discussed in the comments here I extended the test class to extract the actual font sizes (both the size of the Tf operand and the size after applying the current transformation and text matrices). The result:

[140,29,   6,66,   8,50] "nagement en beleid. Via omscholing (" ([, , , AAHACD+SlimbachStd-Book] at   1,00)
[ 11,87,   5,00,   6,38] "PDB" ([, , , AAGNLJ+SlimbachStd-Book-SC750] at   1,00)
[  4,95,   6,66,   8,50] ", " ([, , , AAGNLJ+SlimbachStd-Book-SC750] at   1,00)
[  9,36,   5,00,   6,38] "MB" ([, , , AAGNLJ+SlimbachStd-Book-SC750] at   1,00)
[  4,37,   5,00,   6,38] "A" ([, , , AAGNLJ+SlimbachStd-Book-SC750] at   1,00)
[ 26,76,   6,66,   8,50] ", deels " ([, , , AAHACD+SlimbachStd-Book] at   1,00)
[ 11,28,   5,00,   6,38] "SPD" ([, , , AAGNLJ+SlimbachStd-Book-SC750] at   1,00)
[  5,82,   6,66,   8,50] ") " ([, , , AAHACD+SlimbachStd-Book] at   1,00)
[ 88,66,   6,66,   8,50] "ben ik in het financiële v" ([, , , AAHACD+SlimbachStd-Book] at   1,00)

当你可以看到, Tf 字体大小始终为1.0,小ca的有效字体大小(缩放后) ps是6.38。

As you can see, the Tf font size is always 1.0 and the effective font size (after scaling) of your small caps is 6.38.

由于我主要使用iText(不是iTextSharp),我也在Java中完成了必要的反思和内省。这是我使用的 RenderText 实现的代码:

As I am mostly working with iText (not iTextSharp), I've done the necessary reflection and introspection stuff in Java, too. This is the code of the RenderText implementation I used:

public void renderText(TextRenderInfo renderInfo)
{
    LineSegment curBaseline = renderInfo.getBaseline();
    LineSegment curAscentline = renderInfo.getAscentLine();
    Rectangle rect = new Rectangle(curBaseline.getStartPoint().get(Vector.I1),
            curBaseline.getStartPoint().get(Vector.I2),
            curAscentline.getEndPoint().get(Vector.I1),
            curAscentline.getEndPoint().get(Vector.I2));

    try {
        System.out.printf("  [%6.2f, %6.2f, %6.2f] \"%s\" (%s at %6.2f)\n",
                rect.getWidth(), rect.getHeight(),
                getEffectiveFontSize(renderInfo),
                renderInfo.getText(),
                Arrays.asList(renderInfo.getFont().getFullFontName()[0]),
                getFontSize(renderInfo));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

float getEffectiveFontSize(TextRenderInfo renderInfo)
        throws IllegalArgumentException, SecurityException,
            IllegalAccessException, InvocationTargetException,
            NoSuchFieldException, NoSuchMethodException
{
    Method convertHeight = TextRenderInfo.class.getDeclaredMethod("convertHeightFromTextSpaceToUserSpace", Float.TYPE);
    convertHeight.setAccessible(true);
    return (Float)convertHeight.invoke(renderInfo, getFontSize(renderInfo));
}

float getFontSize(TextRenderInfo renderInfo)
        throws SecurityException, NoSuchFieldException,
            IllegalArgumentException, IllegalAccessException
{
    Field gsField = TextRenderInfo.class.getDeclaredField("gs");
    gsField.setAccessible(true);
    GraphicsState gs = (GraphicsState) gsField.get(renderInfo);
    return gs.getFontSize();
}

.Net内省和反思机制应该可以实现类似的技巧。

The analogous trick should be possible with .Net introspection and reflection mechanisms.

这篇关于如何在itextsharp中实现smallcaps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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