how to implement smallcaps in itextsharp

不想你离开。 提交于 2019-12-05 06:49:07

问题


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


回答1:


I just tried to reproduce your rec.Height == 0.0 issue in

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

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"

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

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.

EDIT

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)

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.

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

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



来源:https://stackoverflow.com/questions/15739221/how-to-implement-smallcaps-in-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!