Hi iText community,

I'm completely new to all of this, so sorry if I break any unwritten rules or anything, but I have gotten myself completely stuck and I have no idea how to solve my problem even after days of trying.

What I am trying to do is draw images to the Graphics object obtained from a PdfContentByte. I need to do this because I am creating a report which is output to different formats, but I want to create the reports in exactly the same way, so I obtain a Graphics object from each and draw to it with methods such as drawString() and drawLine().

My problem is when I use the drawImage() method, for some strange reason the second image I draw (and subsequent images) to the first page of the pdf (but not any following pages, even if I output exactly the same content), the image gets stretched way out to the left. The strange thing is that it doesnt always happen, I can run my program multiple times without changing anything and sometimes it will be ok, but most of the time its stretched way off the left side of the page.

I have reduced my code down to the barest essentials to demonstrate my problem:

package pdftest;

import java.awt.*;
import java.awt.print.*;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class PdfTest {

 public static void main(String[] args) throws Exception
 {
   FileOutputStream stream = new FileOutputStream("c:/test.pdf");

Document document = new Document(PageSize.A4);

   DefaultFontMapper mapper = new DefaultFontMapper();
   mapper.insertDirectory("c:/windows/fonts");

PdfWriter writer = PdfWriter.getInstance(document, stream);

document.open();

PdfContentByte cb = writer.getDirectContent();

   document.newPage();
   cb.saveState();

Graphics2D graphicsContext = cb.createGraphics(500, 800, mapper);

String file = "pdftest/images/image1.jpg";
java.awt.Image image = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(file));
graphicsContext.drawImage(image, 300, 20, 200, 30, null);


file = "pdftest/images/image2.jpg";
java.awt.Image image2 = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(file));
graphicsContext.drawImage(image2, 300, 60, 200, 30, null);


   graphicsContext.dispose();
   cb.restoreState();

   document.close();
 }
}

This is obviously heavily simplified from what I am actually doing, but it produces exactly the same results. The second image added gets skewed.

It doesnt seem to matter what image is added, or what file type (I have tested various gifs, jpgs and pngs) or where on the page or what size. It is almost always the second image on the first produced page (I say almost because occaisionally it is the first image!!!)

I have tried using a MediaTracker to wait until the image has loaded before leaving the method, but this doesnt seem to help either. I have tried all sorts of different little hacks but nothing seems to permanently fix the problem.

I am aware that there is a PdfContentByte.addImage(com.lowagie.text.Image) method, but I am reluctant to use it as this would force me to create different implementations of the report for each format and the way it is set up, this would be very messy.

I am banging my head against a brick wall here - I have been stuck on this for days. If anyone has any ideas at all, I would greatly appreciate hearing what you have to say because I'm all ideaed out :)

Thank in advance,
Peter Agar

_________________________________________________________________
Gaming galore at  http://xtramsn.co.nz/gaming !



-------------------------------------------------------
This SF.net email is sponsored by:  Etnus, makers of TotalView, The best
thread debugger on the planet. Designed with thread debugging features
you've never dreamed of, try TotalView 6 free at www.etnus.com.
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to