Hi, > I have discovered a state problem that I did > not understand, until I tried to put images > in my document with the text. > > It appears that you have to create all of > the PDJpeg objects after the PDDocument > is created and BEFORE you try to > create any text using the PDPageContentStream > object. > > When I tried to create the PDJpeg object > in the same part of my code where I was > creating text, the text PDF stream was > getting mixed in with the "stream" > where the XOjbect is defined. This > situation makes some of the text disappear, > and the PDF reader throws errors > trying to process the image. > > This issue begs the question of how > you create mixed image/text documents > "on the fly", where you don't know > whether you will embed an image, or > where you don't know the name of > the image file until the instant > you have to create the text stream. PDFs consist of different objects and streams. Some of them must not be mixed, e.g. streams containing text and those containing xobjects. The trick is to close the content stream, add the xobject and finally (re)open the content stream again:
PDDocument document = ... PDPage page = ... // create new content stream PDPageContentStream stream = new PDPageContentStream(document, page); // add some content stream.addSomeContent... // close the content stream stream.close(); // add new xobject PDJpeg jpeg = new PDJpeg(document, bufferedImage); // open new content stream stream = new PDPageContentStream(document, page); // or reopen content stream stream = new PDPageContentStream(document, page,true, true); BR Andreas Lehmkühler