Hi.  I've discovered what I suspect is a bug with tab chunks and
justified column text.  Depending upon the size of the tab chunk
sometimes the text juts out to the right of the column.  The same code
works just fine when the tab is set to different sizes; that is the
line justifies without jutting out.

There aren't any patterns I've seen; you can adjust the tab size up or
down and sometimes it will jut out and other times it will justify.
I've also tried with several different fonts; my sample uses Courier
but I've tried other fonts and experience the same behavior.  I've
tried several different incantations of the same code but the behavior
appears in all of them.

I've searched throughout the web, have the book, but don't see this
addressed.  I've figured out crude work-arounds but, after some
thought, decided this is worth posting about.  I'm attaching the PDF
this generates, though it can be run to create the same one.

Michael.

import java.io.FileOutputStream;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;

public class Test
{
    public static final String RESULT = "_tab-problem.pdf";

        private Document document;
        private PdfWriter writer;
        private PdfContentByte cb;

        static private Phrase getText()
        {
                Font font =
                        FontFactory.getFont("courier", 
FontFactory.defaultEncoding, true,
10, Font.NORMAL);
                
                Chunk tabChunk = new Chunk(new VerticalPositionMark(), 40);

                String text1 = "Here is some text without an indent.  You can 
see
that it wraps and works and does all fine.\n";
                String text2 = "This line is supposed to have a tab at the 
beginning
of it, and it does, but sometimes the text pops out to the right.\n";
                
                Chunk firstParagraph = new Chunk(text1, font);
                Chunk secondParagraph = new Chunk(text2, font);

                Phrase p = new Phrase();
                p.add(firstParagraph);
                p.add(tabChunk);
                p.add(secondParagraph);
                
                return p;
        }
        
        public void addText(Phrase phrase,
                                                float leading,
                                                float x, float y,
                                                float width, float height,
                                                int alignment,
                                                float paragraphSpacing,
                                                float tabIndent, float 
hangingIndent)
        {
                try
                {
                        ColumnText ct = new ColumnText(cb);

                        // Adjust to origin in top-left, like screens
                        float pageHeight = 
this.document.getPageSize().getHeight();
                        float lowerLeftX = x;
                        float lowerLeftY = pageHeight - y - height;
                        float upperRightX = x + width;
                        float upperRightY = pageHeight - y;
                        
                        ct.setSimpleColumn(lowerLeftX, lowerLeftY, upperRightX, 
upperRightY);
                        ct.setIndent(tabIndent);
                        ct.setAlignment(alignment);
                        ct.setExtraParagraphSpace(paragraphSpacing);
                        ct.setUseAscender(true);
                        ct.setFollowingIndent(hangingIndent);
                        if(leading>0){ ct.setLeading(leading); }

                        ct.addText(phrase);
                        
                        ct.go();
                }
                catch (Exception ex)
                {
                        ex.printStackTrace();
                }
        }

    public void run()
    {
        this.document = new Document();
        try
        {
            this.writer = PdfWriter.getInstance(document, new
FileOutputStream(RESULT));
            this.document.open();
            this.cb = writer.getDirectContent();

            Phrase phrase = Test.getText();

            float leading=0;
            float width=200;
            float height=250;
            float paragraphSpacing=0;
            float tabIndent=0;
            float hangingIndent=0;

            addText(phrase,
                                        leading,
                                        5, 5,
                                        width, height,
                                        Element.ALIGN_JUSTIFIED,
                                        paragraphSpacing, tabIndent, 
hangingIndent);

            addText(phrase,
                                        leading,
                                        5, 275,
                                        width, height,
                                        Element.ALIGN_LEFT,
                                        paragraphSpacing, tabIndent, 
hangingIndent);


                this.document.close();
        }
        catch(Exception ex)
        {
                ex.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        Test test = new Test();
        test.run();
        
        System.out.println("** finished **");
    }

}

Attachment: _tab-problem.pdf
Description: Adobe PDF document

------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to