tags:

views:

20

answers:

0

hi Everyone i have created a pdf documet with itext (itext version 5,0.4) iam trying to add a some text with setSimpleColumn att absolute position using PdfContentByte i specified the coordinates(lower left x,y,and upper right x, and y ) and it did ran just fine , but when i changed upper right y coordinate just a little , by 1 millimeter,.( I wrote a little helper methods to deal with cm to points conversion and vice/versa it stopped working, just hangs up forever, showing near 100% cpu usage by javaw here is my code

package com.mypackage;

import java.io.*;

import com.itextpdf.text.; import com.itextpdf.text.pdf.;

public class DiplomaPrintout { public static final String RESULT= "test.pdf";

public static void main(String[] args)throws DocumentException, IOException
{
    float  myLeft=PrintUtils.cm2pt(0.8f);
    float  myRight=PrintUtils.cm2pt(1.0f);
    float  myTop=PrintUtils.cm2pt(1.1f);
    float  myBottom=PrintUtils.cm2pt(0.6f);
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    writer.setViewerPreferences(PdfWriter.PrintScalingNone);
    //Image       image = Image.getInstance("diplom.jpg");
    document.setMargins(PrintUtils.cm2pt(0.5f),PrintUtils.cm2pt(0.5f),PrintUtils.cm2pt(0.5f),PrintUtils.cm2pt(0.5f));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfContentByte contentunder = writer.getDirectContentUnder();
    try
    {
        PdfGState gstate=new PdfGState();
        gstate.setFillOpacity(0.6f);;
        contentunder.setGState(gstate);
        //  contentunder.addImage(image,document.getPageSize().getWidth(), 0, 0,document.getPageSize().getHeight(), 0, 0);
        //image.setAlignment(Image.MIDDLE|Image.UNDERLYING);
        FontFactory.registerDirectories();
        String s=
            "негосударственное образовательное учреждение\n \"Институт экономики, управления и права (г.Казань)\"";
        Phrase leftTitle = new Phrase(s, FontFactory.getFont("times new roman", "Cp1251",14,Font.BOLDITALIC));
        ColumnText ct = new ColumnText(cb);
        ct.setSimpleColumn(leftTitle, myLeft+PrintUtils.cm2pt(15.9f), 
                document.getPageSize().getHeight()- myTop-PrintUtils.cm2pt(5.7f),      
                document.getPageSize().getWidth()- myRight - PrintUtils.cm2pt(0.9f),
                document.getPageSize().getHeight() - myTop - PrintUtils.cm2pt(4.2f),
                15f,Element.ALIGN_CENTER);
        // so above  line works with PrintUtils.cm2pt(4.1f), 
        // doesn't with PrintUtils.cm2pt(4.2f)      
        System.out.println(PrintUtils.pt2cm(document.getPageSize().getHeight()));
        int status = ColumnText.START_COLUMN;    
        while (ColumnText.hasMoreText(status)) { 
            status = ct.go();   
        }
        System.out.println(document.getPageSize().getHeight() - myTop - PrintUtils.cm2pt(4.2f));
        document.close();
    }
    catch(Exception d )
    {
        d.printStackTrace();
    }

}

} here is my helper class

package com.mypackage;

public class PrintUtils { public static final float POINTS_IN_CM = 28.346456692913386f;

public static final float CM_IN_POINTS =    0.035277777777778f;

public static final float INCHES_IN_POINT = 0.013888888888889f;

     public static final float POINTS_IN_INCH =  72.0f;

public static float cm2pt(float centimetres)
{   
    float points = POINTS_IN_CM * centimetres;
    return points;
}

public static float pt2cm(float points) 
{   
    float centimetres = CM_IN_POINTS * points;
    return centimetres;
}

public static float in2pt(float inches)
{
    float points = POINTS_IN_INCH * inches;
    return points;
}
public static float pt2inch(float points)
{
    float inches = INCHES_IN_POINT * points;
    return inches;
}

}

as you can see in my code, I'm also adding the big image (7,1MB filesize ) to serve as i canvas I thought that this might be a problem so commented it out to see if it helps only result was that a test line I put after setSimpleColumn showed up but the program itself still hanged up am I missing something here? by the way my java version is 1.5