if(!StringUtil.isEmpty(strY))y = Caster.toIntValue(strY);
}
PDFDocument doc = toPDFDocument(source, password, null);
doc.setPages(pages);
PdfReader reader = doc.getPdfReader();
reader.consolidateNamedDestinations();
boolean destIsSource = destination!=null && doc.getResource()!=null && destination.equals(doc.getResource());
java.util.List bookmarks = SimpleBookmark.getBookmark(reader);
ArrayList master = new ArrayList();
if(bookmarks!=null)master.addAll(bookmarks);
// output
OutputStream os=null;
if(!StringUtil.isEmpty(name) || destIsSource){
os=new ByteArrayOutputStream();
}
else if(destination!=null) {
os=destination.getOutputStream();
}
try {
int len = reader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(reader, os);
if(len >0){
if(x==UNDEFINED || y==UNDEFINED) {
PdfImportedPage first = stamp.getImportedPage(reader, 1);
if(y==UNDEFINED)y=(first.getHeight()-img.getHeight())/2;
if(x==UNDEFINED)x=(first.getWidth()-img.getWidth())/2;
}
img.setAbsolutePosition(x, y);
//img.setAlignment(Image.ALIGN_JUSTIFIED); ration geht nicht anhand mitte
}
// rotation
if(rotation!=0) {
img.setRotationDegrees(rotation);
}
Set _pages = doc.getPages();
for (int i=1;i<=len;i++) {
if(_pages!=null && !_pages.contains(Integer.valueOf(i))) continue;
PdfContentByte cb =foreground? stamp.getOverContent(i):stamp.getUnderContent(i);
PdfGState gs1 = new PdfGState();
//print.out("op:"+opacity);
gs1.setFillOpacity(opacity);
//gs1.setStrokeOpacity(opacity);
cb.setGState(gs1);
cb.addImage(img);
}
if(bookmarks!=null)stamp.setOutlines(master);
stamp.close();
}
finally {
IOUtil.closeEL(os);
if(os instanceof ByteArrayOutputStream) {
if(destination!=null)IOUtil.copy(new ByteArrayInputStream(((ByteArrayOutputStream)os).toByteArray()), destination,true);// MUST overwrite
if(!StringUtil.isEmpty(name)){
pageContext.setVariable(name,new PDFDocument(((ByteArrayOutputStream)os).toByteArray(),password));
}
}
}
}