I am using iText to render a document that has already been laid out by another 
application, and I need to position the text as close as possible to that 
produced by the primary application. For example, I know the width of each line 
of text so I need iText to place a given text string with a given font at an 
absolute position and fill an absolute width exactly. I couldn't find any way 
to do this using PdfContext, so I thought I might try with setWordSpacing. Here 
is my example formatted text in XML:

<TEXT x="1.2245" y="9.93" width="2.76" height="0.0759" fontFamily="Arial" 
fontWeight="normal" fontStyle="normal" fontSize="6" fontColor="0" text="Lorem 
ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse metus urna, "/>
    <TEXT x="1.2245" y="9.83" width="2.76" height="0.0759" fontFamily="Arial" 
fontWeight="normal" fontStyle="normal" fontSize="6" fontColor="0" 
text="pharetra quis auctor eu, aliquet nec leo. Vivamus et eros nibh. Sed 
imperdiet diam "/>
    <TEXT x="1.2245" y="9.73" width="2.76" height="0.0759" fontFamily="Arial" 
fontWeight="normal" fontStyle="normal" fontSize="6" fontColor="0" text="nibh. 
Donec id quam enim. Nulla venenatis pellentesque magna, ac mollis est aliquet 
"/>
    <TEXT x="1.2245" y="9.63" width="2.76" height="0.0759" fontFamily="Arial" 
fontWeight="normal" fontStyle="normal" fontSize="6" fontColor="0" text="ac. In 
hac habitasse platea dictumst. Integer iaculis dolor nec enim auctor in "/>
    <TEXT x="1.2245" y="9.530" width="2.76" height="0.0759" fontFamily="Arial" 
fontWeight="normal" fontStyle="normal" fontSize="6" fontColor="0" 
text="consectetur odio feugiat. Vivamus sem elit, interdum ultricies blandit 
auctor, ornare "/>

And here is the code I am using to try and set the word spacing:

pdftxt.beginText();
pdftxt.setFontAndSize(font.getBaseFont(), new 
Float(el2.getAttribute("fontSize")));
String txt = el2.getAttribute("text");
if ( Math.abs(Utilities.inchesToPoints(w) - 
pdftxt.getEffectiveStringWidth(txt,true)) > 0 ) {
      int spaces=0;
      int chars=0;
      for (int c=0; c < txt.length(); c++ ) {
            if ( Character.isSpaceChar(txt.charAt(c))) spaces++;
            else chars++;
      }
      float diff = 
Utilities.inchesToPoints(w)-pdftxt.getEffectiveStringWidth(txt,true);
      if ( spaces > 0 ) {
            float ws = pdftxt.getWordSpacing() + (diff / spaces);
            pdftxt.setWordSpacing(ws);
      } else {
            otherwise have to space out the chars
            cs = pdftxt.getCharacterSpacing() + (diff/chars);
            pdftxt.setCharacterSpacing(cs);
      }
}
pdftxt.showTextAlignedKerned(com.itextpdf.text.Element.ALIGN_LEFT, txt, 
Utilities.inchesToPoints(x), Utilities.inchesToPoints(y), 0);
pdftxt.endText();

The results from this aren't bad, but it doesn't give the really sharp right 
alignment I am looking for. Any suggestions?

Thanks,
Darryl.
------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to