restoreStream.flush();
//Wrap the existing page's content in a save/restore pair (q/Q) to have a controlled
//environment to add additional content.
COSDictionary pageDictionary = page.getCOSObject();
COSBase contents = pageDictionary.getDictionaryObject(COSName.CONTENTS);
if (contents instanceof COSStream)
{
COSStream contentsStream = (COSStream)contents;
COSArray array = new COSArray();
array.add(saveGraphicsStateStream);
array.add(contentsStream);
array.add(restoreGraphicsStateStream);
pageDictionary.setItem(COSName.CONTENTS, array);
}
else if( contents instanceof COSArray )
{
COSArray contentsArray = (COSArray)contents;
contentsArray.add(0, saveGraphicsStateStream);
contentsArray.add(restoreGraphicsStateStream);
}
else
{
throw new IOException("Contents are unknown type: " + contents.getClass().getName());
}
}