Results 1 to 5 of 5
  1. #1
    Karlovsky120 is offline Member
    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Default JTextArea, iText and spacing isuess

    So I have this code that looks for the value of iText spacing that gives a row of text that is the closest width as the original in jTextArea:

    Java Code:
    float textWidth;
    BigDecimal characterSpacing = new BigDecimal(0.3);
    
    		try
    		{
    			com.itextpdf.text.Document tester = new com.itextpdf.text.Document();
    
    			PdfWriter writer = PdfWriter.getInstance(tester, null);
    
    			tester.open();
    
    			PdfContentByte cb = writer.getDirectContent();
    			cb.setCharacterSpacing(characterSpacing.floatValue());
    			cb.setFontAndSize(bf, fontSize);
    			
    			textWidth = cb.getEffectiveStringWidth(string, true);
    			
    			while (textWidth >= width*0.75)
    			{			
    				characterSpacing = characterSpacing.subtract(new BigDecimal(0.005));
    				
    				cb.setCharacterSpacing(characterSpacing.floatValue());
    				textWidth = cb.getEffectiveStringWidth(string, true);
    				
    			}
    return characterSpacing.floatValue();
    However, I'm getting inconsistent results. This code starts from 0.3f spacing and reduces it until the text in iText becomes narrower than the JTextArea one.
    But this is the result:

    JTextArea, iText and spacing isuess-untitled2.png

    The first row is narrower, but the second one is wider than it's original bellow.

    Only thing that differentiates the two rows are the characterSpacing values, which are defined by the code above.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,843
    Rep Power
    19

    Default Re: JTextArea, iText and spacing isuess

    Stick some debugging in there just so you can see what values are being return for characterSpacing.
    That's the first thing I would check.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Karlovsky120 is offline Member
    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Default Re: JTextArea, iText and spacing isuess

    Values from character spacing are varying from -0.05 to 0.2...

    I first tried using the same character spacing for the entire paragraph to find a sweet spot, but it seems that spacing was different for various letters, so some rows were wider than the originals, while at the same time, some rows were narrower. In order to correct that, I tried using a different spacing for each row, so the rows in the and would be as similar in width as possible.

    But I think that problem here is that while setCharacterSpacing(float f) method takes in a float, it rounds it in a way that my step of 0.005f means nothing...

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,739
    Rep Power
    5

    Default Re: JTextArea, iText and spacing isuess

    Quote Originally Posted by Karlovsky120 View Post
    But I think that problem here is that while setCharacterSpacing(float f) method takes in a float, it rounds it in a way that my step of 0.005f means nothing...
    Well lets check the code:

    Java Code:
    public void  setCharacterSpacing(float charSpace) {
    
        state.charSpace = charSpace;
        content.append(charSpace).append(" Tc").append_i(separator);
    }
    Doesn't look like it, its just a plain setter. Although the rounding may happen at some other part of the code of course, but I rather doubt it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Karlovsky120 is offline Member
    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Default Re: JTextArea, iText and spacing isuess

    Well, there's the limitation of display device... Everything in PDF is measured by units, which are 1/72th of an inch. And since my display is 96dpi, that means that one pixel is represented by 0.75 units (which the float characterSpacing represents in the end). So it might be just that, but the API said that the values are usualy from -0.1f to 0.3f, which would mean, if step is 0.75f, A or B, in best case.

    So while it may be returning all kinds of values, the machine or PDF reader may just be rounding it up to something.

Similar Threads

  1. Spacing....
    By micolord in forum New To Java
    Replies: 8
    Last Post: 10-10-2011, 02:53 PM
  2. [Q] Spacing Help
    By iriscience in forum New To Java
    Replies: 7
    Last Post: 02-08-2011, 01:44 AM
  3. digit spacing
    By puk284 in forum New To Java
    Replies: 3
    Last Post: 06-09-2009, 02:49 PM
  4. Spacing buttons on a panel
    By 2o2 in forum New To Java
    Replies: 8
    Last Post: 10-20-2008, 10:44 AM
  5. Help with spacing in java
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •