List of usage examples for com.lowagie.text.pdf PdfPTable addCell
public void addCell(Phrase phrase)
From source file:ilarkesto.integration.itext.Paragraph.java
License:Open Source License
@Override public Element getITextElement() { com.lowagie.text.Paragraph p = new com.lowagie.text.Paragraph(); float maxSize = 0; for (AParagraphElement element : getElements()) { if (element instanceof TextChunk) { TextChunk textChunk = (TextChunk) element; FontStyle fontStyle = textChunk.getFontStyle(); FontSelector fontSelector = createFontSelector(fontStyle.getFont(), fontStyle); String text = textChunk.getText(); Phrase phrase = fontSelector.process(text); p.add(phrase);/*from w ww .j a va2 s . c o m*/ float size = (fontStyle.getSize() * 1.1f) + 1f; if (size > maxSize) maxSize = PdfBuilder.mmToPoints(size); } else if (element instanceof Image) { Image image = (Image) element; com.lowagie.text.Image itextImage; try { itextImage = image.getITextElement(); } catch (Exception ex) { log.warn("Including image failed:", image, ex); continue; } if (image.getAlign() != null) { itextImage.setAlignment(Image.convertAlign(image.getAlign()) | com.lowagie.text.Image.TEXTWRAP); p.add(itextImage); } else { Chunk chunk = new Chunk(itextImage, 0, 0); p.add(chunk); float size = image.getHeight() + 3; if (size > maxSize) maxSize = size; } } else { throw new RuntimeException("Unsupported paragraph element: " + element.getClass().getName()); } } p.setLeading(maxSize); p.setSpacingBefore(PdfBuilder.mmToPoints(spacingTop)); p.setSpacingAfter(PdfBuilder.mmToPoints(spacingBottom)); if (align != null) p.setAlignment(convertAlign(align)); if (height <= 0) return p; // wrap in table PdfPCell cell = new PdfPCell(); cell.setBorder(0); cell.setFixedHeight(PdfBuilder.mmToPoints(height)); cell.addElement(p); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); table.addCell(cell); return table; }
From source file:ilarkesto.integration.itext.Table.java
License:Open Source License
@Override public Element getITextElement() { float[] cellWidths = getCellWidths(); PdfPTable t = cellWidths == null ? new PdfPTable(getColumnCount()) : new PdfPTable(cellWidths); t.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); Float width = getWidth();//from w ww . j a va 2 s. c om if (width != null) t.setWidthPercentage(width); for (Cell cell : cells) { t.addCell((PdfPCell) cell.getITextElement()); } return t; }
From source file:is.idega.idegaweb.egov.cases.business.CaseWriter.java
License:Open Source License
protected MemoryFileBuffer writePDF(IWContext iwc) { Font titleFont = new Font(Font.HELVETICA, 14, Font.BOLD); Font labelFont = new Font(Font.HELVETICA, 11, Font.BOLD); Font textFont = new Font(Font.HELVETICA, 11, Font.NORMAL); try {//from www. ja v a2 s .c o m MemoryFileBuffer buffer = new MemoryFileBuffer(); MemoryOutputStream mos = new MemoryOutputStream(buffer); Document document = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter.getInstance(document, mos); document.addAuthor("Idegaweb eGov"); document.addSubject("Case"); document.open(); document.newPage(); String title = iwrb.getLocalizedString("case_overview", "Case overview"); Paragraph cTitle = new Paragraph(title, titleFont); cTitle.setSpacingAfter(24); document.setPageCount(1); document.add(cTitle); int[] widths = { 25, 75 }; PdfPTable table = new PdfPTable(2); table.setWidths(widths); table.getDefaultCell().setBorder(0); table.getDefaultCell().setPaddingBottom(8); CaseCategory category = theCase.getCaseCategory(); CaseCategory parentCategory = category.getParent(); CaseType type = theCase.getCaseType(); User user = theCase.getOwner(); Address address = user != null ? getUserBusiness(iwc).getUsersMainAddress(user) : null; PostalCode postal = null; if (address != null) { postal = address.getPostalCode(); } Phone phone = null; if (user != null) { try { phone = getUserBusiness(iwc).getUsersHomePhone(user); } catch (NoPhoneFoundException e) { //No phone found... } } Email email = null; if (user != null) { try { email = getUserBusiness(iwc).getUsersMainEmail(user); } catch (NoEmailFoundException e) { //No email found... } } IWTimestamp created = new IWTimestamp(theCase.getCreated()); if (user != null) { table.addCell(new Phrase(iwrb.getLocalizedString("name", "Name"), labelFont)); table.addCell(new Phrase( new Name(user.getFirstName(), user.getMiddleName(), user.getLastName()).getName(locale), textFont)); table.addCell(new Phrase(iwrb.getLocalizedString("personal_id", "Personal ID"), labelFont)); table.addCell(new Phrase(PersonalIDFormatter.format(user.getPersonalID(), locale), textFont)); table.addCell(new Phrase(iwrb.getLocalizedString("address", "Address"), labelFont)); table.addCell(new Phrase(address != null ? address.getStreetAddress() : "-", textFont)); table.addCell(new Phrase(iwrb.getLocalizedString("zip_code", "Postal code"), labelFont)); table.addCell(new Phrase(postal != null ? postal.getPostalAddress() : "-", textFont)); table.addCell(new Phrase(iwrb.getLocalizedString("home_phone", "Home phone"), labelFont)); table.addCell(new Phrase(phone != null ? phone.getNumber() : "-", textFont)); table.addCell(new Phrase(iwrb.getLocalizedString("email", "Email"), labelFont)); table.addCell(new Phrase(email != null ? email.getEmailAddress() : "-", textFont)); table.addCell(new Phrase("")); table.addCell(new Phrase("")); table.addCell(new Phrase("")); table.addCell(new Phrase("")); } table.addCell(new Phrase(iwrb.getLocalizedString("case_nr", "Case nr."), labelFont)); table.addCell(new Phrase(theCase.getPrimaryKey().toString(), textFont)); if (getCasesBusiness(iwc).useTypes()) { table.addCell(new Phrase(iwrb.getLocalizedString("case_type", "Case type"), labelFont)); table.addCell(new Phrase(type.getName(), textFont)); } if (parentCategory != null) { table.addCell(new Phrase(iwrb.getLocalizedString("case_category", "Case category"), labelFont)); table.addCell(new Phrase(parentCategory.getLocalizedCategoryName(locale), textFont)); table.addCell(new Phrase(iwrb.getLocalizedString("sub_case_category", "Case category"), labelFont)); table.addCell(new Phrase(category.getLocalizedCategoryName(locale), textFont)); } else { table.addCell(new Phrase(iwrb.getLocalizedString("case_category", "Case category"), labelFont)); table.addCell(new Phrase(category.getLocalizedCategoryName(locale), textFont)); } table.addCell(new Phrase(iwrb.getLocalizedString("created_date", "Created date"), labelFont)); table.addCell(new Phrase(created.getLocaleDateAndTime(locale, IWTimestamp.SHORT, IWTimestamp.SHORT), textFont)); if (theCase.getSubject() != null) { table.addCell(new Phrase(iwrb.getLocalizedString("subject", "Subject"), labelFont)); table.addCell(new Phrase(theCase.getSubject(), textFont)); } table.addCell(new Phrase(iwrb.getLocalizedString("message", "Message"), labelFont)); table.addCell(new Phrase(theCase.getMessage(), textFont)); if (theCase.getReference() != null) { table.addCell(new Phrase(iwrb.getLocalizedString("reference", "Reference"), labelFont)); table.addCell(new Phrase(theCase.getReference(), textFont)); } table.setWidthPercentage(100); document.add(table); document.close(); try { mos.close(); } catch (Exception ex) { ex.printStackTrace(); } buffer.setMimeType("application/pdf"); return buffer; } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:is.idega.idegaweb.egov.printing.business.DocumentBusinessBean.java
License:Open Source License
public void createCommuneFooter(PdfWriter writer) throws Exception { PdfContentByte cb = writer.getDirectContent(); Font nameFont = getDefaultParagraphFont(); nameFont.setSize(9);//from ww w. j a v a 2 s. c om Font textFont = getDefaultTextFont(); textFont.setSize(9); PdfPTable table = new PdfPTable(4); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setNoWrap(true); IWBundle iwb = getIWApplicationContext().getIWMainApplication() .getBundle(is.idega.idegaweb.egov.message.business.MessageConstants.IW_BUNDLE_IDENTIFIER); table.addCell(new Phrase(iwb.getProperty("commune.name_mailaddr", "Mailaddress"), nameFont)); table.addCell(new Phrase(iwb.getProperty("commune.name_visitaddr", "Visitaddress"), nameFont)); table.addCell(new Phrase(iwb.getProperty("commune.name_contact", "Contact"), nameFont)); table.addCell(new Phrase(iwb.getProperty("commune.name_org_nr", "Organizationsnr"), nameFont)); table.addCell(new Phrase(iwb.getProperty("commune.mail_name", "Mail name"), getTextFont())); table.addCell(new Phrase(iwb.getProperty("commune.visit_name", "Visit name"), textFont)); table.addCell(new Phrase(iwb.getProperty("commune.website", "www.some-place.com"), textFont)); table.addCell(new Phrase(iwb.getProperty("commune.org_number", "XXXXXX-XXXX"), textFont)); table.addCell(new Phrase(iwb.getProperty("commune.mail_zip", "Zip code"), textFont)); table.addCell(new Phrase(iwb.getProperty("commune.visit_streetaddr", "Street and number,"), textFont)); table.addCell(new Phrase(iwb.getProperty("commune.support_email", "email@someplace.com"), textFont)); table.addCell(new Phrase(" ", textFont)); table.addCell(new Phrase(" ", textFont)); table.addCell(new Phrase(iwb.getProperty("commune.visit_zip", "Visit zip"), textFont)); table.addCell(new Phrase(iwb.getProperty("commune.office_phone", "office phone"), textFont)); table.addCell(new Phrase(" ", textFont)); int distFromBottomMM = 30; int[] widths = { 20, 20, 30, 20 }; table.setWidths(widths); table.setTotalWidth(getPointsFromMM(210 - 25 - 20)); table.writeSelectedRows(0, -1, getPointsFromMM(25), getPointsFromMM(distFromBottomMM), cb); PdfContentByte linebyte = new PdfContentByte(writer); // we add some crosses to visualize the destinations linebyte.moveTo(getPointsFromMM(25), getPointsFromMM(distFromBottomMM + 2)); linebyte.lineTo(getPointsFromMM(210 - 25), getPointsFromMM(distFromBottomMM + 2)); linebyte.stroke(); // we add the template on different positions cb.add(linebyte); }
From source file:ispyb.client.biosaxs.pdf.DataAcquisitionPDFReport.java
License:Open Source License
/** * @param experiment/*from ww w . j a v a2 s. c o m*/ * @return */ private Element getMeasurementTable(Experiment3VO experiment, List<Buffer3VO> buffers) { PdfPTable table = new PdfPTable(MEASUREMENT_COLUMNS.length); table.setWidthPercentage(100f); table.getDefaultCell().setPadding(3); table.getDefaultCell().setColspan(MEASUREMENT_COLUMNS.length); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setColspan(1); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); for (int i = 0; i < MEASUREMENT_COLUMNS.length; i++) { table.addCell(this.getCell(MEASUREMENT_COLUMNS[i], BaseFont.HELVETICA_BOLD)); } table.getDefaultCell().setBackgroundColor(null); List<List<String>> data = this.getExperimentMesurementData(experiment, buffers); for (List<String> list : data) { for (String string : list) { table.addCell(getCell(string)); } } return table; }
From source file:ispyb.client.biosaxs.pdf.DataAcquisitionPDFReport.java
License:Open Source License
private Element getImageTable(Experiment3VO experiment, List<Buffer3VO> buffers) { PdfPTable table = new PdfPTable(ImageColumns.length); table.setWidthPercentage(100f);//from w w w . j a v a 2 s . c om table.getDefaultCell().setPadding(3); table.getDefaultCell().setColspan(ImageColumns.length); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setColspan(1); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); for (int i = 0; i < ImageColumns.length; i++) { table.addCell(this.getCell(ImageColumns[i], BaseFont.HELVETICA_BOLD)); } table.getDefaultCell().setBackgroundColor(null); List<Measurement3VO> measurements = experiment.getMeasurements(); Collections.sort(measurements, MeasurementComparator .compare(MeasurementComparator.getComparator(MeasurementComparator.PRIOTIRY_SORT_ASC))); for (int x = 0; x < measurements.size(); x++) { Measurement3VO measurement = measurements.get(x); Specimen3VO specimen = experiment.getSampleById(measurement.getSpecimenId()); if (specimen.getMacromolecule3VO() != null) { SaxsDataCollection3VO dataCollection = experiment .getDataCollectionByMeasurementId(measurement.getMeasurementId()); ArrayList<String> list = this.addAnalysisRow(measurement, experiment, buffers); if (list.size() > 0) { int sizeTable = list.get(0).length(); PdfPTable nested1 = new PdfPTable(sizeTable); nested1.getDefaultCell().setBorder(0); for (int i = 0; i < list.size(); i++) { if (i == 0) { nested1.getDefaultCell().setBackgroundColor(PdfRtfExporter.LIGHT_GREY_COLOR); nested1.getDefaultCell().setBorder(0); Paragraph cell = this.getCell(this.COLUMNS[i] + ": " + list.get(i)); nested1.addCell(cell); } else { nested1.getDefaultCell().setBackgroundColor(null); nested1.getDefaultCell().setBorder(0); PdfPCell cell = new PdfPCell(this.getCell(this.COLUMNS[i] + ": " + list.get(i))); cell.setBorder(0); nested1.addCell(cell); } } table.addCell(new PdfPCell(nested1)); if (dataCollection.getSubstraction3VOs() != null) { if (dataCollection.getSubstraction3VOs().size() > 0) { Subtraction3VO substraction = ((Subtraction3VO) (dataCollection.getSubstraction3VOs() .toArray()[0])); String scatteringImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getScatteringFilePath()); if (scatteringImage != null) { if (new File(scatteringImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(scatteringImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } String guinierImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getGuinierFilePath()); if (guinierImage != null) { if (new File(guinierImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(guinierImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } String kraktyImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getKratkyFilePath()); if (kraktyImage != null) { if (new File(kraktyImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(kraktyImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } String gnomImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getGnomFilePath()); if (gnomImage != null) { if (new File(gnomImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(gnomImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } } } } } return table; }
From source file:ispyb.client.biosaxs.pdf.DataAcquisitionPDFReport.java
License:Open Source License
private Element getAnalysis(Experiment3VO experiment, List<Buffer3VO> buffers) { PdfPTable table = new PdfPTable(COLUMNS.length); /** SUBTITLE **/ table.getDefaultCell().setColspan(5); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(this.getCell("Sample", BaseFont.HELVETICA_BOLD)); table.getDefaultCell().setColspan(5); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(this.getCell("Guinier", BaseFont.HELVETICA_BOLD)); table.getDefaultCell().setColspan(3); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(this.getCell("Gnom", BaseFont.HELVETICA_BOLD)); table.getDefaultCell().setColspan(2); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(this.getCell("Porod", BaseFont.HELVETICA_BOLD)); table.setWidthPercentage(100f);/*ww w. ja va 2s . c om*/ table.getDefaultCell().setPadding(3); table.getDefaultCell().setColspan(COLUMNS.length); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setColspan(1); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); for (int i = 0; i < COLUMNS.length; i++) { table.addCell(this.getCell(COLUMNS[i], BaseFont.HELVETICA_BOLD)); } table.getDefaultCell().setBackgroundColor(null); List<List<String>> data = this.getExperimentAnalysisData(experiment, buffers); for (List<String> list : data) { for (String string : list) { table.addCell(getCell(string)); } } return table; }
From source file:ispyb.common.util.export.PdfExporterSample.java
License:Open Source License
/** * Exports the file for viewSample for shipment * //from w ww . j av a 2s.co m * @return * @throws Exception */ public ByteArrayOutputStream exportAsPdf() throws Exception { // create simple doc and write to a ByteArrayOutputStream Document document = new Document(PageSize.A4.rotate(), 20, 20, 20, 20); document.addTitle("exportSamplesView"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); HeaderFooter header; // header + footer if (viewName != null) header = new HeaderFooter(new Phrase("Samples for Proposal: " + proposalDesc + " --- " + viewName), false); else header = new HeaderFooter(new Phrase("Samples for Proposal: " + proposalDesc), false); header.setAlignment(Element.ALIGN_CENTER); header.setBorderWidth(1); header.getBefore().getFont().setSize(8); HeaderFooter footer = new HeaderFooter(new Phrase("Page n."), true); footer.setAlignment(Element.ALIGN_RIGHT); footer.setBorderWidth(1); footer.getBefore().getFont().setSize(6); document.setHeader(header); document.setFooter(footer); document.open(); if (aList.isEmpty()) { document.add(new Paragraph("There is no samples in this report")); document.close(); return baos; } // Create first table for samples int NumColumns = 19; PdfPTable table = new PdfPTable(NumColumns); int headerwidths[] = { 6, 6, 6, 6, 6, 4, 6, 4, 4, 4, 4, 4, 4, 8, 5, 5, 5, 10, 6 }; // percentage table.setWidths(headerwidths); table.setWidthPercentage(100); // percentage table.getDefaultCell().setPadding(3); table.getDefaultCell().setBorderWidth(1); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); // header PdfPCell cell = new PdfPCell(); table.addCell(new Paragraph("Protein", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Sample name", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Smp code", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Dewar", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Container", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Loc. in cont.", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Space group", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Cell a", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Cell b", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Cell c", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Cell alpha", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Cell beta", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Cell gamma", new Font(Font.HELVETICA, 8))); cell = new PdfPCell(new Paragraph("Crystal comments", new Font(Font.HELVETICA, 8))); table.addCell(cell); table.addCell(new Paragraph("Already observed resol.", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Required resol.", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Min. resol.", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Sample comments", new Font(Font.HELVETICA, 8))); table.addCell(new Paragraph("Sample status", new Font(Font.HELVETICA, 8))); table.setHeaderRows(1); // this is the end of the table header table.getDefaultCell().setBorderWidth(1); DecimalFormat df1 = new DecimalFormat("#####0.0"); DecimalFormat df2 = new DecimalFormat("#####0.00"); Iterator it = aList.iterator(); int i = 1; String currentContainer = "next"; String nextContainer = "next"; while (it.hasNext()) { table.getDefaultCell().setGrayFill(0.99f); if (i % 2 == 1) { table.getDefaultCell().setGrayFill(0.9f); } BLSample3VO samplefv = (BLSample3VO) it.next(); LOG.debug("table of datacollections pdf " + samplefv.getBlSampleId()); if (samplefv.getContainerVO() != null && samplefv.getContainerVO().getCode() != null) nextContainer = samplefv.getContainerVO().getCode(); else nextContainer = "next"; // in the case of view sorted by dewar/container, we add a page break afetr each container if (sortView.equals("2") && !currentContainer.equals(nextContainer)) { document.add(table); table.deleteBodyRows(); document.newPage(); } if (samplefv.getCrystalVO().getProteinVO().getAcronym() != null) table.addCell(new Paragraph(samplefv.getCrystalVO().getProteinVO().getAcronym(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getName() != null) table.addCell(new Paragraph(samplefv.getName(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCode() != null) table.addCell(new Paragraph(samplefv.getCode(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getContainerVO() != null && samplefv.getContainerVO().getDewarVO() != null && samplefv.getContainerVO().getDewarVO().getCode() != null) table.addCell(new Paragraph(samplefv.getContainerVO().getDewarVO().getCode(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getContainerVO() != null && samplefv.getContainerVO().getCode() != null) { currentContainer = samplefv.getContainerVO().getCode(); table.addCell(new Paragraph(currentContainer, new Font(Font.HELVETICA, 8))); } else { currentContainer = "current"; table.addCell(""); } if (samplefv.getLocation() != null) table.addCell(new Paragraph(samplefv.getLocation(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getSpaceGroup() != null) table.addCell(new Paragraph(samplefv.getCrystalVO().getSpaceGroup(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getCellA() != null) table.addCell( new Paragraph(df1.format(samplefv.getCrystalVO().getCellA()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getCellB() != null) table.addCell( new Paragraph(df1.format(samplefv.getCrystalVO().getCellB()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getCellC() != null) table.addCell( new Paragraph(df1.format(samplefv.getCrystalVO().getCellC()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getCellAlpha() != null) table.addCell(new Paragraph(df1.format(samplefv.getCrystalVO().getCellAlpha()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getCellBeta() != null) table.addCell(new Paragraph(df1.format(samplefv.getCrystalVO().getCellBeta()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getCellGamma() != null) table.addCell(new Paragraph(df1.format(samplefv.getCrystalVO().getCellGamma()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getCrystalVO().getComments() != null) table.addCell(new Paragraph(samplefv.getCrystalVO().getComments(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getDiffractionPlanVO() != null && samplefv.getDiffractionPlanVO().getObservedResolution() != null) { table.addCell(new Paragraph(df2.format(samplefv.getDiffractionPlanVO().getObservedResolution()), new Font(Font.HELVETICA, 8))); } else if (samplefv.getCrystalVO().getDiffractionPlanVO() != null && samplefv.getCrystalVO().getDiffractionPlanVO().getObservedResolution() != null) table.addCell(new Paragraph( df2.format(samplefv.getCrystalVO().getDiffractionPlanVO().getObservedResolution()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getDiffractionPlanVO() != null && samplefv.getDiffractionPlanVO().getRequiredResolution() != null) { table.addCell(new Paragraph(df2.format(samplefv.getDiffractionPlanVO().getRequiredResolution()), new Font(Font.HELVETICA, 8))); } else if (samplefv.getCrystalVO().getDiffractionPlanVO() != null && samplefv.getCrystalVO().getDiffractionPlanVO().getRequiredResolution() != null) table.addCell(new Paragraph( df2.format(samplefv.getCrystalVO().getDiffractionPlanVO().getRequiredResolution()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getDiffractionPlanVO() != null && samplefv.getDiffractionPlanVO().getMinimalResolution() != null) { table.addCell(new Paragraph(df2.format(samplefv.getDiffractionPlanVO().getMinimalResolution()), new Font(Font.HELVETICA, 8))); } else if (samplefv.getCrystalVO().getDiffractionPlanVO() != null && samplefv.getCrystalVO().getDiffractionPlanVO().getMinimalResolution() != null) table.addCell(new Paragraph( df2.format(samplefv.getCrystalVO().getDiffractionPlanVO().getMinimalResolution()), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getComments() != null && samplefv.getComments() != "") table.addCell(new Paragraph(samplefv.getComments(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (samplefv.getBlSampleStatus() != null) table.addCell(new Paragraph(samplefv.getBlSampleStatus(), new Font(Font.HELVETICA, 8))); else table.addCell(""); if (i % 2 == 1) { table.getDefaultCell().setGrayFill(0.0f); } i++; } document.add(table); document.close(); return baos; }
From source file:it.eng.spagobi.engines.documentcomposition.exporterUtils.PdfCreator.java
License:Mozilla Public License
public FileOutputStream createPdfFile(FileOutputStream fileOutputStream, Map<String, DocumentContainer> documentsMap, boolean defaultStyle) throws MalformedURLException, IOException, DocumentException { logger.debug("IN"); Document document = new Document(PageSize.A4.rotate()); Rectangle rect = document.getPageSize(); docWidth = rect.getWidth();/* w ww .ja va 2 s . com*/ docHeight = rect.getHeight(); logger.debug("document size width: " + docWidth + " height: " + docHeight); //PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream("C:/comp/SpagoBIProva.pdf")); PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream); document.open(); int documentsNumber = documentsMap.keySet().size(); int columnnsNumber = 2; if (defaultStyle == true) { logger.debug("use default style"); int cellsCounter = 0; PdfPTable table = new PdfPTable(columnnsNumber); table.setWidthPercentage(100); 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); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); //table.setWidthPercentage(tableWidthPerc); table.setTotalWidth(tableWidth); table.setLockedWidth(true); } else { // TODO: setALT! } logger.debug("Add table"); table.writeSelectedRows(0, -1, xPos, yPos, writer.getDirectContent()); logger.debug("Document added"); } } document.close(); logger.debug("OUT"); return fileOutputStream; }
From source file:it.eng.spagobi.engines.qbe.crosstable.exporter.CrosstabPDFExporter.java
License:Mozilla Public License
/** * Builds the table for the crosstab// w ww. j a v a2 s.c o m * @param json the JSON representation of the crosstab * @param pdfDocument the pdf document that should contains the crosstab * @param numberFormat the formatter for the numbers * @throws JSONException * @throws BadElementException * @throws DocumentException */ public void export(JSONObject json, Document pdfDocument, DecimalFormat numberFormat) throws SerializationException, JSONException, BadElementException, DocumentException { logger.debug("IN: exporting the crosstab"); //prepare the crosstab for the export CrosstabExporterUtility.calculateDescendants(json); JSONObject columnsRoot = (JSONObject) json.get(CrossTab.CROSSTAB_JSON_COLUMNS_HEADERS); JSONArray columnsRootChilds = columnsRoot.getJSONArray(CrossTab.CROSSTAB_NODE_JSON_CHILDS); JSONObject rowsRoot = (JSONObject) json.get(CrossTab.CROSSTAB_JSON_ROWS_HEADERS); JSONArray rowsRootChilds = rowsRoot.getJSONArray(CrossTab.CROSSTAB_NODE_JSON_CHILDS); JSONArray rowHeadersDescription = json.getJSONArray(CrossTab.CROSSTAB_JSON_ROWS_HEADER_TITLE); JSONArray data = (JSONArray) json.get(CrossTab.CROSSTAB_JSON_DATA); measureMetadata = new MeasureFormatter(json, numberFormat, "##,##0.00"); this.numberFormat = numberFormat; //build the matrix for the content dataMatrix = new Vector<List<PdfPCell>>(); buildDataMatrix(data); //number of headers lavels int rowsDepth = CrosstabExporterUtility.getDepth(rowsRoot); int columnsDepth = CrosstabExporterUtility.getDepth(columnsRoot); //build the table PdfPTable table = new PdfPTable(rowsDepth + dataMatrix.get(0).size()); //build the empty cell on the top left PdfPCell topLeftCell = new PdfPCell(new Phrase("")); topLeftCell.setRowspan(columnsDepth - 1);//-1 because of the title of the rows header topLeftCell.setColspan(rowsDepth); topLeftCell.setBorderColor(Color.WHITE); table.addCell(topLeftCell); List<PdfPCell> cells = new ArrayList<PdfPCell>(); //builds the headers int dataColumnNumber = ((JSONArray) data.get(0)).length(); cells.addAll(buildColumnsHeader(columnsRootChilds, rowHeadersDescription, dataColumnNumber)); cells.addAll(buildRowsHeaders(rowsRootChilds)); logger.debug("Addign the content"); //adds the headers for (int i = 0; i < cells.size(); i++) { table.addCell(cells.get(i)); } table.setWidthPercentage(100); pdfDocument.add(table); logger.debug("IN: exported the crosstab"); }