When I create a PDF with a default ICC color profile and add an image created
by calling com.itextpdf.text.Image.getInstance(PdfWriter writer, Image
awtImage, float quality), iText is generating an invalid PDF where each
ColorSpace dictionary (in the page and nested XObjects) has another ColorSpace
entry which causes the failure. The sample code is below. Does anyone know
how to prevent the duplicate ColorSpace entry from being added? I don't have
this problem if I add images created by calling
com.itextpdf.text.Image.getInstance(String filename).
Thanks,
Peter
import java.awt.color.ColorSpace;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import javax.imageio.ImageIO;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ICC_Profile;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfICCBased;
import com.itextpdf.text.pdf.PdfIndirectReference;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfWriter;
public class TestPdf {
public static final int PDF_DEFAULT_DPI = 72;
public static float DEFAULT_JPEG_QUALITY = 0.9f;
Document document;
PdfWriter writer;
PdfContentByte _directContent;
PdfIndirectReference csRef;
ICC_Profile defaultPDFICCProfile;
float jpegQuality = DEFAULT_JPEG_QUALITY;
public TestPdf() {
java.awt.color.ICC_Profile defaultICCProfile =
java.awt.color.ICC_Profile.getInstance(ColorSpace.CS_sRGB);
defaultPDFICCProfile =
com.itextpdf.text.pdf.ICC_Profile.getInstance(defaultICCProfile.getData());
}
public void startPDF(String filename) throws DocumentException,
IOException {
startPDF(filename, new Rectangle(11.0f * PDF_DEFAULT_DPI, 8.5f
* PDF_DEFAULT_DPI));
}
public void startPDF(String filename, Rectangle mediabox) throws
DocumentException, IOException {
startPDF(new FileOutputStream(filename), mediabox, mediabox,
mediabox, mediabox);
}
public void startPDF(OutputStream outStream, Rectangle mediabox,
Rectangle cropbox, Rectangle bleedbox, Rectangle
trimbox) throws DocumentException, IOException {
document = new Document(mediabox);
writer = PdfWriter.getInstance(document, outStream);
writer.setCropBoxSize(cropbox);
writer.setBoxSize("bleed", bleedbox);
writer.setBoxSize("trim", trimbox);
writer.setPDFXConformance(PdfWriter.PDFX32002);
document.open();
if (defaultPDFICCProfile != null) {
// set up the ICCBased color space data stream
PdfICCBased iccstream = new
PdfICCBased(defaultPDFICCProfile);
iccstream.put(PdfName.N, new
PdfNumber(defaultPDFICCProfile.getNumComponents()));
iccstream.put(PdfName.ALTERNATE, PdfName.DEVICERGB);
// Compress the stream data
iccstream.flateCompress();
// ICCBased color space is specified as an array
PdfArray iccarray = new PdfArray();
iccarray.add(PdfName.ICCBASED);
PdfIndirectReference iccRef;
// add ICC profile data stream to body of pdf doc and
get its indirect reference
iccRef =
writer.addToBody(iccstream).getIndirectReference();
// add reference to ICC profile data to ICCBased
colorspace array
iccarray.add(iccRef);
// add ICCBased colorspace array to body of pdf doc and
get its indirect reference
csRef =
writer.addToBody(iccarray).getIndirectReference();
// Set the writer's default colorspace
writer.setDefaultColorspace(PdfName.DEFAULTRGB, csRef);
writer.setDefaultColorspace(PdfName.COLORSPACE,
PdfName.DEFAULTRGB);
_directContent = writer.getDirectContentUnder();
// set default colorspace to DefaultRGB with a
reference to the ICCBased colorspace array
_directContent.setDefaultColorspace(PdfName.DEFAULTRGB,
csRef);
_directContent.setDefaultColorspace(PdfName.COLORSPACE,
PdfName.DEFAULTRGB);
}
else
_directContent = writer.getDirectContentUnder();
}
public void addImageFromFile(String imgFile) throws
MalformedURLException, IOException, DocumentException {
java.awt.Image awtImage = ImageIO.read(new File(imgFile));
com.itextpdf.text.Image img =
com.itextpdf.text.Image.getInstance(writer, awtImage, jpegQuality);
//com.itextpdf.text.Image img =
com.itextpdf.text.Image.getInstance(imgFile);
addImage(img, 0, 0);
}
public void addImage(com.itextpdf.text.Image img, float x, float y)
throws DocumentException {
img.setAbsolutePosition(x, y);
document.newPage();
document.add(img);
}
public void finishPDF() {
document.close();
}
/**
* @param args
*/
public static void main(String[] args) {
String pdfFile = "test.pdf";
String jpgImageFile = "testImage.jpg";
TestPdf myPdf = new TestPdf();
try {
myPdf.startPDF(pdfFile);
myPdf.addImageFromFile(jpgImageFile);
myPdf.finishPDF();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
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/