I have a problem where a JPEG image is not showing in a PDF. I am using
iText 2.1.5, Java 6, and Adobe Acrobat 8.1. I have written a test
program to simplify showing the problem. The JPEG is loaded into the
Image class using getInstance and passing the URL of the file. The image
does show up in the PDF but it is blank, meaning that if I click in the
PDF I can find the image and do a copy. The copy however only produces a
blue background. I have tried other PDF creation software (such as
CutePDFWriter) and the image displays in the PDF just fine.  I have also
tried with the Jpeg class and get the same errors. The Jpeg2000 class
gives me an invalid JPEG error message as part of an IO Exception. Any
help is greatly appreciated. 

Here is the code:

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.image.BufferedImage;
import java.io.RandomAccessFile;
import java.net.URL;
/**
 *
*/
public class PDFPrototype {
    
    /** Creates a new instance of PDFPrototype */
    public PDFPrototype() {
    }
    public static void main(String args[]) {
        // Use the iText package to create various PDF documents and see
what memory usage is
        // step 1: creation of a document-object
        int numPages = 4;
        System.out.println("Start Time for " + numPages + ": " +
System.currentTimeMillis());
        try {
            Document document = new Document();
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            PdfWriter.getInstance(document,
                            new
FileOutputStream("c:\\projects\\PDFPrototype\\PDFPrototype2.pdf"));

            // step 3: we open the document
            document.open();
            document.setPageSize(PageSize.LETTER.rotate());
            // step 4: we add a paragraph to the document
            for(int i = 0; i < numPages; i++) {
                document.newPage();
                document.setPageSize(PageSize.LETTER.rotate());
                URL url = null;
                BufferedImage bufImg = null;
                RandomAccessFile raf = null;
                Image jpg = null;
                if(i % 2 != 0)  {
                    // This image does not show
                    document.add(new Paragraph("JPEGNotvisibleinPDF2"));
                    url = new URL("file", "localhost",
"c:\\projects\\PDFPrototype\\src\\images\\JPEGNotvisibleinPDF2.jpg");
                    jpg = Image.getInstance(url);
                    jpg.scalePercent(40.00f);
                    document.add(jpg);
                } else {
                    // This image does show
                    document.add(new Paragraph("JPEGNotvisibleinPDF"));
                    url = new URL("file", "localhost",
"c:\\projects\\PDFPrototype\\src\\images\\JPEGNotvisibleinPDF.jpg");
                    jpg = image.getInstance(url);
                    jpg.scalePercent(40.00f);
                    document.add(jpg);
                }
                document.newPage();
            }
            System.out.println("End Time for " + numPages + ": " +
System.currentTimeMillis());
            // step 5: we close the document
            document.close();
        } catch (DocumentException de) {
            System.err.println("Caught Document Exception: " +
de.getMessage());
            de.printStackTrace();
        } catch (IOException ioe) {
            System.err.println("Caught IO Exception: " +
ioe.getMessage());
            ioe.printStackTrace();
        }

    }
}

BJ Blinston
System Architect
[email protected]
480.724.4349 - Office
480.209.0101 - Cell


------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to