Mark Hall wrote:
On Monday 14 May 2007, Thomas Bickel wrote:
        if((itImg.width() > docWidth) || (itImg.height() > docHeight)) {
I missed it the first time I read through your code, but when I ran it now, I caught the error. In this line you are mixing pixels and points. itImg.width() returns the width of the image in pixels, while docWidth is calculated in points and 1px = 1.33pt. So comparing 1px = 1pt means that the

isn't it the other way round? 1px = .75pt?

code thinks the image is to large to early and then calls the scaling code. The scaling code works correctly, because you feed it docWidth and docHeight (both in points) and the scaleToFit method expects user units (for RTF points).

So I was correct and the rtf image resolution is 33% higher than the document 
resolution. :)
This is a bit annoying though because it differs from the way the pdf writer 
works (where the above code sample works as intended) and because none of the 
methods actually specify the units that are used.

The scaling is still broken however.
Check out the following example: Scaling to 99.99% in iText causes the image to 
be scaled to 130% in Word while scaling to 75% causes it to be scaled to 100%.
This would work if the twips value (RtfElement.TWIPS_FACTOR) used to convert 
image pixels to twips (\picwgoal,\pichgoal) was set to 15.


        final com.lowagie.text.Document doc = new com.lowagie.text.Document();
        RtfWriter2.getInstance(doc, new FileOutputStream("scaletest2.rtf"));
        doc.open();
                
        final String format = "png";
        final File imgFile = new File("test."+format).getAbsoluteFile();        
      
                
        {       BufferedImage bim = new BufferedImage(100, 100, 
BufferedImage.TYPE_INT_RGB);
                Graphics2D g2 = bim.createGraphics();                           
                g2.setColor(Color.GREEN);
                g2.fillRect(0,0, 100,100);
                g2.dispose();
                ImageIO.write(bim, format, imgFile);
        }
                        
        doc.add(new Chunk("no scaling:\n"));
        com.lowagie.text.Image itImg = 
com.lowagie.text.Image.getInstance(imgFile.toURL());
        doc.add(itImg); 

        doc.add(new Chunk("99.99% scaling:\n"));
        itImg = com.lowagie.text.Image.getInstance(imgFile.toURL());
        itImg.scalePercent(99.99f);
        doc.add(itImg);
                
        doc.add(new Chunk("75% scaling:\n"));
        itImg = com.lowagie.text.Image.getInstance(imgFile.toURL());
        itImg.scalePercent(75f);
        doc.add(itImg);
                
        doc.close();



Regards,
Thomas

Attachment: scaletest2.rtf
Description: MS-Word document

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to