for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) {
String label = (String) iterator.next();
DocumentContainer docContainer = documentsMap.get(label);
byte[] content = docContainer.getContent();
if (content != null) {
Image img = null;
try {
img = Image.getInstance(content);
table.addCell(img);
} catch (Exception e) {
logger.debug("Trying to evaluate response as a PDF file... ");
table.addCell("");
// try {
// PdfReader reader = new PdfReader(content);
// PdfImportedPage page = writer.getImportedPage(reader, 1);
// writer.addPage(page);
// table.addCell("");
// } catch (Exception x) {
// logger.error("Error in inserting image for document " + label, e);
// logger.error("Error in inserting pdf file for document " + label, x);
// table.addCell("");
// }
}
}
cellsCounter++;
}
// if cell counter is not pair make it pair
if(cellsCounter%2!=0){
table.addCell("");
}
document.add(table);
}
else
{ // ************* NO DEFAULT STYLE *****************
logger.debug("No default style");
// I want to calculate total height of scaled heights!!
//int totalScaledHeight=calculateTotaleScaledHeights(documentsMap, defaultStyle);
// run on all documents
for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) {
String label = (String) iterator.next();
logger.debug("document with label "+label);
DocumentContainer docContainer= documentsMap.get(label);
MetadataStyle style=docContainer.getStyle();
// one table for each image, set at absolute position
PdfPTable table=new PdfPTable(1);
// width and height specified for the container by style attribute
int widthStyle=style.getWidth();
int heightStyle=style.getHeight();
logger.debug("style for document width: "+widthStyle+ " height: "+heightStyle);
// width and height for the table scaled to the document size
int tableWidth=calculatePxSize(docWidth, widthStyle, videoWidth);
int tableHeight=calculatePxSize(docHeight, heightStyle, videoHeight);
logger.debug("table for document width: "+tableWidth+ " height: "+tableHeight);
// x and y position as specified for the container by the style attribute
int yStyle=style.getY();
int xStyle=style.getX();
// width and height scaled to the document size
int xPos=(calculatePxPos(docWidth, xStyle, videoWidth));
int yPos=(int)docHeight-(calculatePxPos(docHeight, yStyle, videoHeight));
logger.debug("Table position at x: "+xPos+ " y: "+yPos);
// get the image
byte[] content=docContainer.getContent();
if(content != null){
Image img = null;
try {
img = Image.getInstance(content);
}
catch (Exception e) {
logger.debug("Trying to evaluate response as a PDF file... ");
try {
PdfReader reader = new PdfReader(content);
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
float[] tm = getTransformationMatrix(page, xPos, yPos, tableWidth, tableHeight);
cb.addTemplate(page, tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]);
} catch (Exception x) {
logger.error("Error in inserting image for document " + label, e);
logger.error("Error in inserting pdf file for document " + label, x);
}
continue;
}
//if it is a REPORT and has more than one page, too large, you have to resize the image, but how to understand it?
// if image size is more than double of the container size cut the first part,otherwise scale it
if(docContainer.getDocumentType().equals("REPORT")){
boolean cutImageWIdth=isToCutWidth(img, tableWidth);
boolean cutImageHeight=isToCutHeight(img, tableWidth);
if(cutImageWIdth==true || cutImageHeight==true){
logger.debug("Report will be cut to width "+tableWidth+" and height "+tableHeight);
try{
img=cutImage(content, cutImageHeight, cutImageWIdth, tableHeight, tableWidth, (int)img.getWidth(), (int)img.getHeight());
}catch (Exception e) {
logger.error("Error in image cut, cutt will be ignored and image will be drawn anyway ",e);
}
}
}
// this is percentage to resize
// The image must be size within the cell
int percToResize=percentageToResize((int)img.getWidth(), (int)img.getHeight(), tableWidth, tableHeight);
logger.debug("image will be scaled of percentage "+percToResize);
img.scalePercent(percToResize);
PdfPCell cell= new PdfPCell(img);
cell.setNoWrap(true);
cell.setFixedHeight(tableHeight);
cell.setBorderWidth(0);