Example usage for com.itextpdf.text.pdf PdfPCell setVerticalAlignment

List of usage examples for com.itextpdf.text.pdf PdfPCell setVerticalAlignment

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfPCell setVerticalAlignment.

Prototype

public void setVerticalAlignment(int verticalAlignment) 

Source Link

Document

Sets the vertical alignment for the cell.

Usage

From source file:psManage.StructSheet.java

public void createPdf(String mainTitle, String subTitle, String url, String userName,
        //String scanType,
        String comment,/*  w w w  . java  2s .c o m*/
        //String thisPassCode,
        String passCodeA, String passCodeB, String fileDir, Boolean noBarCodePrint)
        throws IOException, DocumentException, RuntimeException {
    Document document = null;
    try {
        // step 1
        document = new Document(PageSize.A4, 60, 50, 50, 35);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileDir));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        /*
         Properties props = new Properties();
         String jarPath = System.getProperty("java.class.path");
         String dirPath = jarPath.substring(0, jarPath.lastIndexOf(File.separator)+1);
         FontFactory.registerDirectory("/res");
         FontFactory.register("ipag.ttf");
         Font ipaGothic = FontFactory.getFont("ipag", BaseFont.IDENTITY_H, 
         BaseFont.EMBEDDED, 10); //10 is the size
                
         InputStream is = getClass().getResourceAsStream("/res/ipag.ttf");
         */

        Properties props = new Properties();
        String jarPath = System.getProperty("java.class.path");
        String dirPath = jarPath.substring(0, jarPath.lastIndexOf(File.separator) + 1);
        System.out.println(jarPath);
        System.out.println(dirPath);
        System.out.println(System.getProperty("user.dir"));

        Font ipaGothic = new Font(BaseFont.createFont(System.getProperty("user.dir") + "\\res\\ipag.ttf",
                BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 11);

        Font ipaGothic14 = new Font(BaseFont.createFont(System.getProperty("user.dir") + "\\res\\ipag.ttf",
                BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 14);

        //?(2)
        PdfPTable pdfPTable = new PdfPTable(2);

        pdfPTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
        pdfPTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfPTable.getDefaultCell().setFixedHeight(150);

        pdfPTable.setWidthPercentage(100f);

        int pdfPTableWidth[] = { 10, 90 };
        pdfPTable.setWidths(pdfPTableWidth);

        PdfPCell cell_1_1 = new PdfPCell(new Paragraph("??", ipaGothic));
        cell_1_1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell_1_1.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell_1_1.setFixedHeight(50);
        PdfPCell cell_1_2 = new PdfPCell(new Paragraph(mainTitle, ipaGothic));
        cell_1_2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell_1_2.setHorizontalAlignment(Element.ALIGN_CENTER);

        PdfPCell cell_2_1 = new PdfPCell(new Paragraph("", ipaGothic));
        cell_2_1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell_2_1.setHorizontalAlignment(Element.ALIGN_CENTER);
        PdfPCell cell_2_2 = new PdfPCell(new Paragraph(subTitle, ipaGothic));
        cell_2_2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell_2_2.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell_2_2.setFixedHeight(50);
        pdfPTable.addCell(cell_1_1);
        pdfPTable.addCell(cell_1_2);
        pdfPTable.addCell(cell_2_1);
        pdfPTable.addCell(cell_2_2);

        PdfPCell cellUrlKey = new PdfPCell(new Paragraph("?", ipaGothic));
        cellUrlKey.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellUrlKey.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellUrlKey.setRowspan(2);
        pdfPTable.addCell(cellUrlKey);

        PdfPCell cellUrlValue = new PdfPCell(new Paragraph(url, ipaGothic));
        Chunk chunk = new Chunk(url, ipaGothic); // ??????????????
        System.out.println("??" + chunk.getWidthPoint());
        cellUrlValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
        if (chunk.getWidthPoint() > 410) { // ??????????
            cellUrlValue.setHorizontalAlignment(Element.ALIGN_LEFT);
        } else { // ?????????
            cellUrlValue.setHorizontalAlignment(Element.ALIGN_CENTER);
        }

        cellUrlValue.setFixedHeight(50);
        pdfPTable.addCell(cellUrlValue);

        if (url.length() != 0 && !noBarCodePrint) {
            /* ?
             BarcodeQRCode qr = new BarcodeQRCode(url, 50, 50, null);
             PdfPCell cellUrlValueQr = new PdfPCell(qr.getImage());
             cellUrlValueQr.setVerticalAlignment(Element.ALIGN_MIDDLE);
             cellUrlValueQr.setHorizontalAlignment(Element.ALIGN_CENTER);
             cellUrlValueQr.setFixedHeight(80);
             pdfPTable.addCell(cellUrlValueQr);
             */
            Image image = ZxingUti.getQRCode(url); //  SHIFT_JIS
            com.itextpdf.text.Image iTextImage = com.itextpdf.text.Image.getInstance(image, null);
            PdfPCell cell = new PdfPCell(iTextImage);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(100);
            pdfPTable.addCell(cell); //  SIFT_JIS
        } else {
            PdfPCell cellUrlValueQr = new PdfPCell(new Paragraph("", ipaGothic));
            cellUrlValueQr.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellUrlValueQr.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellUrlValueQr.setFixedHeight(80);
            pdfPTable.addCell(cellUrlValueQr);
        }

        PdfPCell cellUserNameKey = new PdfPCell(new Paragraph("", ipaGothic));
        cellUserNameKey.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellUserNameKey.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellUserNameKey.setRowspan(2);
        pdfPTable.addCell(cellUserNameKey);

        PdfPCell cellUserNameValue = new PdfPCell(new Paragraph(userName, ipaGothic14));
        cellUserNameValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellUserNameValue.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellUserNameValue.setFixedHeight(30);
        pdfPTable.addCell(cellUserNameValue);

        if (userName.length() != 0 && !noBarCodePrint) {
            Barcode128 code128 = new Barcode128();
            code128.setCode(userName);
            code128.setFont(ipaGothic.getBaseFont());
            code128.setBarHeight(40f);
            PdfPCell cellUserNameValueBc = new PdfPCell(code128.createImageWithBarcode(cb, null, null));
            cellUserNameValueBc.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellUserNameValueBc.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellUserNameValueBc.setFixedHeight(80);
            pdfPTable.addCell(cellUserNameValueBc);
        } else {
            PdfPCell cellUserNameValueBc = new PdfPCell(new Paragraph("---", ipaGothic));
            cellUserNameValueBc.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellUserNameValueBc.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellUserNameValueBc.setFixedHeight(80);
            pdfPTable.addCell(cellUserNameValueBc);
        }

        PdfPCell cellPassCodeKey = new PdfPCell(new Paragraph("?", ipaGothic));
        cellPassCodeKey.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellPassCodeKey.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellPassCodeKey.setRowspan(3);
        pdfPTable.addCell(cellPassCodeKey);

        PdfPCell cellPassCodeValue = new PdfPCell(new Paragraph(passCodeA + passCodeB, ipaGothic14));
        cellPassCodeValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellPassCodeValue.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellPassCodeValue.setFixedHeight(30);
        pdfPTable.addCell(cellPassCodeValue);

        if (passCodeA.length() != 0 && !noBarCodePrint) {
            Barcode128 code128 = new Barcode128();
            code128.setCode(passCodeA);
            code128.setFont(ipaGothic.getBaseFont());
            code128.setBarHeight(40f);
            PdfPCell cellPassCodeA_Bc = new PdfPCell(code128.createImageWithBarcode(cb, null, null));
            cellPassCodeA_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellPassCodeA_Bc.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellPassCodeA_Bc.setFixedHeight(80);
            pdfPTable.addCell(cellPassCodeA_Bc);
        } else {
            PdfPCell cellPassCodeA_Bc = new PdfPCell(new Paragraph("---", ipaGothic));
            cellPassCodeA_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellPassCodeA_Bc.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellPassCodeA_Bc.setFixedHeight(80);
            pdfPTable.addCell(cellPassCodeA_Bc);
        }

        if (passCodeB.length() != 0 && !noBarCodePrint) {
            Barcode128 code128 = new Barcode128();
            code128.setCode(passCodeB);
            code128.setFont(ipaGothic.getBaseFont());
            code128.setBarHeight(40f);
            PdfPCell cellPassCodeB_Bc = new PdfPCell(code128.createImageWithBarcode(cb, null, null));
            cellPassCodeB_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellPassCodeB_Bc.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellPassCodeB_Bc.setFixedHeight(80);
            pdfPTable.addCell(cellPassCodeB_Bc);
        } else {
            PdfPCell cellPassCodeB_Bc = new PdfPCell(new Paragraph("---", ipaGothic));
            cellPassCodeB_Bc.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellPassCodeB_Bc.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellPassCodeB_Bc.setFixedHeight(80);
            pdfPTable.addCell(cellPassCodeB_Bc);
        }

        PdfPCell cellCommentKey = new PdfPCell(new Paragraph("?", ipaGothic));
        cellCommentKey.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellCommentKey.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfPTable.addCell(cellCommentKey);

        PdfPCell cellCommentValue = new PdfPCell(new Paragraph(comment, ipaGothic));
        cellCommentValue.setVerticalAlignment(Element.ALIGN_TOP);
        cellCommentValue.setHorizontalAlignment(Element.ALIGN_LEFT);
        cellCommentValue.setFixedHeight(150);
        pdfPTable.addCell(cellCommentValue);

        PdfPCell cellIssueKey = new PdfPCell(new Paragraph("", ipaGothic));
        cellIssueKey.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellIssueKey.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfPTable.addCell(cellIssueKey);

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String strDate = sdf.format(cal.getTime());
        PdfPCell cellIssueValue = new PdfPCell(new Paragraph(strDate, ipaGothic));
        cellIssueValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellIssueValue.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellIssueValue.setFixedHeight(20);
        pdfPTable.addCell(cellIssueValue);

        //??
        document.add(pdfPTable);
        /*
                
         // CODE 128
         document.add(new Paragraph("?? : " + mainTitle, ipaGothic));
         document.add(new Paragraph(" : " + subTitle, ipaGothic));
         document.add(new Paragraph("-------------------------------------------------------"));
         document.add(new Paragraph(" " + strDate));
         document.add(new Paragraph("-------------------------------------------------------"));
                
                
         BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);
         Font font = new Font(bf, 12);
         document.add(new Paragraph("", ipaGothic));
         document.add(new Paragraph(url, ipaGothic));
         code128.setCode(url);
         code128.setFont(bf);
         code128.setX(1);
         //document.add(code128.createImageWithBarcode(cb, null, null));
                
         document.add(new Paragraph("USER", ipaGothic));
         if (userName.length() != 0) {
         document.add(new Paragraph(userName, ipaGothic));
         code128.setCode(userName);
         code128.setFont(bf);
         code128.setBarHeight(40f);
         document.add(code128.createImageWithBarcode(cb, null, null));
         }
                
         document.add(new Paragraph("CODE", ipaGothic));
         if (passCode.length() != 0) {
         document.add(new Paragraph(passCode, ipaGothic));
         code128.setCode(passCode);
         code128.setFont(bf);
         document.add(code128.createImageWithBarcode(cb, null, null));
         }
                
         document.add(new Paragraph("?", ipaGothic));
         document.add(new Paragraph(comment, ipaGothic));
         */
        // step 5
        document.close();
    } catch (RuntimeException ex) {
        document.close();
        throw ex;

    }
}

From source file:ro.nextreports.engine.exporter.PdfExporter.java

License:Apache License

private PdfPCell renderPdfCell(BandElement bandElement, Object value, int gridRow, int rowSpan, int colSpan,
        boolean image, int column) {
    Map<String, Object> style = buildCellStyleMap(bandElement, value, gridRow, column, colSpan);

    FontFactoryImp fact = new FontFactoryImp();
    com.itextpdf.text.Font fnt;/*from   ww  w. j a  v  a  2  s  .  c om*/
    if (bandElement != null) {
        fontName = (String) style.get(StyleFormatConstants.FONT_NAME_KEY);
        int size = ((Float) style.get(StyleFormatConstants.FONT_SIZE)).intValue();
        fnt = getFont(size);
    } else {
        fnt = getFont(10);
    }

    PdfPCell cell;
    if (image) {
        if (value == null) {
            cell = new PdfPCell(new Phrase(IMAGE_NOT_FOUND));
        } else {
            ImageBandElement ibe = (ImageBandElement) bandElement;
            try {
                byte[] imageBytes = getImage((String) value);
                cell = getImageCell(ibe, imageBytes, column, colSpan);
            } catch (Exception e) {
                cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED));
            }
        }
    } else if (bandElement instanceof HyperlinkBandElement) {
        Hyperlink hyperlink = ((HyperlinkBandElement) bandElement).getHyperlink();
        Anchor anchor = new Anchor(hyperlink.getText(), fnt);
        anchor.setReference(hyperlink.getUrl());
        Phrase ph = new Phrase();
        ph.add(anchor);
        cell = new PdfPCell(ph);
    } else if (bandElement instanceof ReportBandElement) {
        Report report = ((ReportBandElement) bandElement).getReport();
        ExporterBean eb = null;
        try {
            eb = getSubreportExporterBean(report);
            PdfExporter subExporter = new PdfExporter(eb);
            subExporter.export();
            PdfPTable innerTable = subExporter.getTable();
            cell = new PdfPCell(innerTable);
        } catch (Exception e) {
            cell = new PdfPCell();
            e.printStackTrace();
        } finally {
            if ((eb != null) && (eb.getResult() != null)) {
                eb.getResult().close();
            }
        }
    } else if ((bandElement instanceof VariableBandElement) && (VariableFactory
            .getVariable(((VariableBandElement) bandElement).getVariable()) instanceof TotalPageNoVariable)) {
        try {
            cell = new PdfPCell(Image.getInstance(total));
        } catch (BadElementException e) {
            cell = new PdfPCell(new Phrase("NA"));
        }

    } else if (bandElement instanceof ImageColumnBandElement) {
        try {
            String v = StringUtil.getValueAsString(value, null);
            if (StringUtil.BLOB.equals(v)) {
                cell = new PdfPCell(new Phrase(StringUtil.BLOB));
            } else {
                byte[] bytes = StringUtil.decodeImage(v);
                cell = getImageCell(bandElement, bytes, column, colSpan);
            }
        } catch (Exception e) {
            e.printStackTrace();
            cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED));
        }
    } else {
        String stringValue;
        if (style.containsKey(StyleFormatConstants.PATTERN)) {
            stringValue = StringUtil.getValueAsString(value, (String) style.get(StyleFormatConstants.PATTERN),
                    getReportLanguage());
        } else {
            stringValue = StringUtil.getValueAsString(value, null, getReportLanguage());
        }
        if (stringValue == null) {
            stringValue = "";
        }
        if (stringValue.startsWith("<html>")) {
            StringReader reader = new StringReader(stringValue);
            List<Element> elems = new ArrayList<Element>();
            try {
                elems = HTMLWorker.parseToList(reader, new StyleSheet());
                Phrase ph = new Phrase();
                for (int i = 0; i < elems.size(); i++) {
                    Element elem = (Element) elems.get(i);
                    ph.add(elem);
                }
                cell = new PdfPCell(ph);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Phrase ph = new Phrase(stringValue, fnt);
                cell = new PdfPCell(ph);
            }

        } else {
            Phrase ph = new Phrase(stringValue, fnt);
            cell = new PdfPCell(ph);
        }
    }

    cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    cell.setUseDescender(true); // needed for a cell without padding
    cell.setMinimumHeight(MINIMUM_HEIGHT); // needed if there is a row in which all cells are empty

    if (bandElement != null) {
        cell.setRotation(bandElement.getTextRotation());
    }

    if (colSpan > 1) {
        cell.setColspan(colSpan);
    }

    if (rowSpan > 1) {
        cell.setRowspan(rowSpan);
    }

    if (style != null) {

        updateFont(style, fnt);

        if (style.containsKey(StyleFormatConstants.BACKGROUND_COLOR)) {
            Color val = (Color) style.get(StyleFormatConstants.BACKGROUND_COLOR);
            cell.setBackgroundColor(new BaseColor(val));
        }
        if (style.containsKey(StyleFormatConstants.HORIZONTAL_ALIGN_KEY)) {
            if (StyleFormatConstants.HORIZONTAL_ALIGN_LEFT
                    .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
                cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            }
            if (StyleFormatConstants.HORIZONTAL_ALIGN_RIGHT
                    .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
                cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
            }
            if (StyleFormatConstants.HORIZONTAL_ALIGN_CENTER
                    .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            }
        }

        if (style.containsKey(StyleFormatConstants.VERTICAL_ALIGN_KEY)) {
            if (StyleFormatConstants.VERTICAL_ALIGN_TOP
                    .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
                cell.setVerticalAlignment(Element.ALIGN_TOP);
            }
            if (StyleFormatConstants.VERTICAL_ALIGN_MIDDLE
                    .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            }
            if (StyleFormatConstants.VERTICAL_ALIGN_BOTTOM
                    .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
                cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
            }
        }

        if (style.containsKey(StyleFormatConstants.PADDING_LEFT)) {
            Float val = (Float) style.get(StyleFormatConstants.PADDING_LEFT);
            cell.setPaddingLeft(val);
        }
        if (style.containsKey(StyleFormatConstants.PADDING_RIGHT)) {
            Float val = (Float) style.get(StyleFormatConstants.PADDING_RIGHT);
            cell.setPaddingRight(val);
        }
        if (style.containsKey(StyleFormatConstants.PADDING_TOP)) {
            Float val = (Float) style.get(StyleFormatConstants.PADDING_TOP);
            cell.setPaddingTop(val);
        }
        if (style.containsKey(StyleFormatConstants.PADDING_BOTTOM)) {
            Float val = (Float) style.get(StyleFormatConstants.PADDING_BOTTOM);
            cell.setPaddingBottom(val);
        }
        cell.setBorderWidth(0);

        if (style.containsKey(StyleFormatConstants.BORDER_LEFT)) {
            Float val = (Float) style.get(StyleFormatConstants.BORDER_LEFT);
            cell.setBorderWidthLeft(val / 2);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_LEFT_COLOR);
            cell.setBorderColorLeft(new BaseColor(color));
        }
        if (style.containsKey(StyleFormatConstants.BORDER_RIGHT)) {
            Float val = (Float) style.get(StyleFormatConstants.BORDER_RIGHT);
            cell.setBorderWidthRight(val / 2);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_RIGHT_COLOR);
            cell.setBorderColorRight(new BaseColor(color));
        }
        if (style.containsKey(StyleFormatConstants.BORDER_TOP)) {
            Float val = (Float) style.get(StyleFormatConstants.BORDER_TOP);
            cell.setBorderWidthTop(val / 2);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_TOP_COLOR);
            cell.setBorderColorTop(new BaseColor(color));
        }
        if (style.containsKey(StyleFormatConstants.BORDER_BOTTOM)) {
            Float val = (Float) style.get(StyleFormatConstants.BORDER_BOTTOM);
            cell.setBorderWidthBottom(val / 2);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_BOTTOM_COLOR);
            cell.setBorderColorBottom(new BaseColor(color));
        }

        // for subreports we use default no wrap
        if (cell.getTable() == null) {
            cell.setNoWrap(true);
            if (bandElement != null) {
                if (bandElement.isWrapText()) {
                    cell.setNoWrap(false);
                }
            }
        }

        // to see a background image all cells must not have any background!
        if (bean.getReportLayout().getBackgroundImage() != null) {
            cell.setBackgroundColor(null);
        }
    }
    return cell;
}

From source file:rs.marko.helper.Kreiranje.java

public static File createPdfStatistika(List<Stavkadnevneberbe> stavke, Date pocetak, Date kraj) {
    Document document = new Document();
    File yourFile = null;/* w  w  w .j a v  a  2 s  .  c  o m*/
    try {
        yourFile = new File(System.getProperty("user.home") + "/Desktop/" + pocetak + "_" + kraj + ".pdf");
        if (!yourFile.exists()) {
            yourFile.createNewFile();
        }
        FileOutputStream oFile = new FileOutputStream(yourFile, false);
        document.setPageSize(PageSize.A4.rotate());
        PdfWriter.getInstance(document, oFile);
        document.open();
        //                        String text = "";
        //                        for (int i = 0; i < 10000; i++) {
        //                                text += "test";
        //              ,          }
        //            String jmbg = stavke.get(0).getDobavljac().getJmbg();

        PdfPTable table = new PdfPTable(
                new float[] { 300f, 150f, 150f, 150f, 150f, 150f, 150f, 150f, 150f, 150f, 150f, 150f, 150f });
        table.setWidthPercentage(100);
        table.setSpacingBefore(0f);
        table.setSpacingAfter(0f);
        Paragraph p = new Paragraph("Od: " + pocetak + " do: " + kraj);
        p.add(new Paragraph(" "));
        document.add(p);
        PdfPCell c = new PdfPCell(new Phrase(""));
        //            PdfPCell c1 = new PdfPCell(new Phrase("Sifra"));
        //            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        //            c1.setRowspan(2);
        //            c1.setColspan(2);
        //            table.addCell(c1);
        PdfPCell c1 = new PdfPCell(new Phrase("Dobavljac"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        c1.setRowspan(2);
        c1.setColspan(2);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Tacne"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(2);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("I Klasa"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(2);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("II Klasa"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(2);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("III Klasa"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(2);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Svega"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(3);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Kol"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Iznos"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Kol"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Iznos"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Kol"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Iznos"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Kol"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Iznos"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Kol"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Cena"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase("Iznos"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        Font font = new Font(BaseFont.createFont(), 10, Font.NORMAL);

        //                table.setHeaderRows(3);
        List<String> jmbgovi = new ArrayList<>();
        Map<Double, Double> kt = new HashMap<>();
        Map<Double, Double> k1 = new HashMap<>();
        Map<Double, Double> k2 = new HashMap<>();
        Map<Double, Double> k3 = new HashMap<>();

        Map<Double, Double> ukupnoTacneVrednost = new HashMap<>();
        Map<Double, Double> ukupnoPrvaKlasaVrednost = new HashMap<>();
        Map<Double, Double> ukupnoDrugaKlasaVrednost = new HashMap<>();
        Map<Double, Double> ukupnoTrecaKlasaVrednost = new HashMap<>();
        for (Stavkadnevneberbe stavkaDnevneBerbe : stavke) {
            if (!jmbgovi.contains(stavkaDnevneBerbe.getDnevnaberba().getDobavljac().getJmbg())) {
                jmbgovi.add(stavkaDnevneBerbe.getDnevnaberba().getDobavljac().getJmbg());
            }
        }
        Double ukupnoKolicinaTacne = 0.0;
        Double ukupnoKolicinaPrvaKlasa = 0.0;
        Double ukupnoKolicinaDrugaKlasa = 0.0;
        Double ukupnoKolicinaTrecaKlasa = 0.0;
        Double ukupnoCenaTacne = 0.0;
        Double ukupnoCenaPrvaKlasa = 0.0;
        Double ukupnoCenaDrugaKlasa = 0.0;
        Double ukupnoCenaTrecaKlasa = 0.0;
        Double ukupanIznosSvih = 0.0;
        for (String j : jmbgovi) {
            String imeDobavljaca = null;
            for (Stavkadnevneberbe stavkaDnevneBerbe : stavke) {
                if (stavkaDnevneBerbe.getDnevnaberba().getDobavljac().getJmbg().equals(j)) {
                    if (imeDobavljaca == null) {
                        imeDobavljaca = stavkaDnevneBerbe.getDnevnaberba().getDobavljac().getIme() + " "
                                + stavkaDnevneBerbe.getDnevnaberba().getDobavljac().getPrezime();

                    }
                    if (!kt.containsKey(stavkaDnevneBerbe.getCenatacne())
                            && stavkaDnevneBerbe.getTacne() != 0) {
                        kt.put(stavkaDnevneBerbe.getCenatacne(), new Double(0));
                    }
                    if (!k1.containsKey(stavkaDnevneBerbe.getCenaprvaklasa())
                            && stavkaDnevneBerbe.getCenaprvaklasa() != 0) {
                        k1.put(stavkaDnevneBerbe.getCenaprvaklasa(), new Double(0));
                    }
                    if (!k2.containsKey(stavkaDnevneBerbe.getCenadrugaklasa())
                            && stavkaDnevneBerbe.getCenadrugaklasa() != 0) {
                        k2.put(stavkaDnevneBerbe.getCenadrugaklasa(), new Double(0));
                    }
                    if (!k3.containsKey(stavkaDnevneBerbe.getCenatrecaklasa())
                            && stavkaDnevneBerbe.getCenatrecaklasa() != 0) {
                        k3.put(stavkaDnevneBerbe.getCenatrecaklasa(), new Double(0));
                    }
                }
            }
            for (Stavkadnevneberbe s : stavke) {
                if (s.getDnevnaberba().getDobavljac().getJmbg().equals(j)) {
                    if (kt.containsKey(s.getCenatacne())) {
                        kt.put(s.getCenatacne(), kt.get(s.getCenatacne()) + s.getTacne());
                    }
                    if (k1.containsKey(s.getCenaprvaklasa())) {
                        k1.put(s.getCenaprvaklasa(), k1.get(s.getCenaprvaklasa()) + s.getPrvaklasa());
                    }
                    if (k2.containsKey(s.getCenadrugaklasa())) {
                        k2.put(s.getCenadrugaklasa(), k2.get(s.getCenadrugaklasa()) + s.getDrugaklasa());
                    }
                    if (k3.containsKey(s.getCenatrecaklasa())) {
                        k3.put(s.getCenatrecaklasa(), k3.get(s.getCenatrecaklasa()) + s.getTrecaklasa());
                    }
                }
            }
            c1 = new PdfPCell(new Phrase(imeDobavljaca));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            c1.setColspan(2);
            table.addCell(c1);
            Iterator it = kt.entrySet().iterator();
            Double ukupnoKolicinaT = 0.0;
            Double ukupnoCenaT = 0.0;
            Double ukupanIznos = 0.0;
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                ukupnoKolicinaT += (Double) pair.getValue();
                ukupnoCenaT += (Double) pair.getKey();
                ukupanIznos += ((Double) pair.getValue() * (Double) pair.getKey());
                //ukupno vrednost
                if (ukupnoTacneVrednost.containsKey((Double) pair.getKey())) {
                    ukupnoTacneVrednost.put((Double) pair.getKey(),
                            ukupnoTacneVrednost.get((Double) pair.getKey())
                                    + ((Double) pair.getKey() * (Double) pair.getValue()));
                } else {
                    ukupnoTacneVrednost.put((Double) pair.getKey(),
                            (Double) pair.getKey() * (Double) pair.getValue());
                }
            }
            c1 = new PdfPCell(new Phrase(round(ukupnoKolicinaT, 2) + "", font));
            table.addCell(c1);
            c1 = new PdfPCell(new Phrase(round(ukupanIznos, 2) + "", font));
            table.addCell(c1);

            it = k1.entrySet().iterator();
            Double ukupnoKolicinaP = 0.0;
            Double ukupnoCenaP = 0.0;
            Double ukupanIznosP = 0.0;
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                ukupnoKolicinaP += (Double) pair.getValue();
                ukupnoCenaP += (Double) pair.getKey();
                ukupanIznosP += ((Double) pair.getValue() * (Double) pair.getKey());
                if (ukupnoPrvaKlasaVrednost.containsKey((Double) pair.getKey())) {
                    ukupnoPrvaKlasaVrednost.put((Double) pair.getKey(),
                            ukupnoPrvaKlasaVrednost.get((Double) pair.getKey())
                                    + ((Double) pair.getKey() * (Double) pair.getValue()));
                } else {
                    ukupnoPrvaKlasaVrednost.put((Double) pair.getKey(),
                            (Double) pair.getKey() * (Double) pair.getValue());
                }
            }
            c1 = new PdfPCell(new Phrase(round(ukupnoKolicinaP, 2) + "", font));
            table.addCell(c1);
            c1 = new PdfPCell(new Phrase(round(ukupanIznosP, 2) + "", font));
            table.addCell(c1);

            it = k2.entrySet().iterator();
            Double ukupnoKolicinaD = 0.0;
            Double ukupnoCenaD = 0.0;
            Double ukupanIznosD = 0.0;
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                ukupnoKolicinaD += (Double) pair.getValue();
                ukupnoCenaD += (Double) pair.getKey();
                ukupanIznosD += ((Double) pair.getValue() * (Double) pair.getKey());
                if (ukupnoDrugaKlasaVrednost.containsKey((Double) pair.getKey())) {
                    ukupnoDrugaKlasaVrednost.put((Double) pair.getKey(),
                            ukupnoDrugaKlasaVrednost.get((Double) pair.getKey())
                                    + ((Double) pair.getKey() * (Double) pair.getValue()));
                } else {
                    ukupnoDrugaKlasaVrednost.put((Double) pair.getKey(),
                            (Double) pair.getKey() * (Double) pair.getValue());
                }
            }
            c1 = new PdfPCell(new Phrase(round(ukupnoKolicinaD, 2) + "", font));
            table.addCell(c1);
            c1 = new PdfPCell(new Phrase(round(ukupanIznosD, 2) + "", font));
            table.addCell(c1);

            it = k3.entrySet().iterator();
            Double ukupnoKolicinaTr = 0.0;
            Double ukupnoCenaTr = 0.0;
            Double ukupanIznosT = 0.0;
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                ukupnoKolicinaTr += (Double) pair.getValue();
                ukupnoCenaTr += (Double) pair.getKey();
                ukupanIznosT += ((Double) pair.getValue() * (Double) pair.getKey());
                if (ukupnoTrecaKlasaVrednost.containsKey((Double) pair.getKey())) {
                    ukupnoTrecaKlasaVrednost.put((Double) pair.getKey(),
                            ukupnoTrecaKlasaVrednost.get((Double) pair.getKey())
                                    + ((Double) pair.getKey() * (Double) pair.getValue()));
                } else {
                    ukupnoTrecaKlasaVrednost.put((Double) pair.getKey(),
                            (Double) pair.getKey() * (Double) pair.getValue());
                }
            }
            c1 = new PdfPCell(new Phrase(round(ukupnoKolicinaTr, 2) + "", font));
            table.addCell(c1);
            c1 = new PdfPCell(new Phrase(round(ukupanIznosT, 2) + "", font));
            table.addCell(c1);
            Double ukupnoKolicina = ukupnoKolicinaT + ukupnoKolicinaP + ukupnoKolicinaD + ukupnoKolicinaTr;
            Double ukupnoVrednost = ukupanIznos + ukupanIznosP + ukupanIznosD + ukupanIznosT;
            Double ukupnoCena = ukupnoVrednost / ukupnoKolicina;
            c1 = new PdfPCell(new Phrase(round(ukupnoKolicina, 2) + "", font));
            table.addCell(c1);
            c1 = new PdfPCell(new Phrase(round(ukupnoCena, 2) + "", font));
            table.addCell(c1);
            c1 = new PdfPCell(new Phrase(round(ukupnoVrednost, 2) + "", font));
            table.addCell(c1);

            ukupanIznosSvih += ukupnoVrednost;

            ukupnoKolicinaTacne += ukupnoKolicinaT;
            ukupnoKolicinaPrvaKlasa += ukupnoKolicinaP;
            ukupnoKolicinaDrugaKlasa += ukupnoKolicinaD;
            ukupnoKolicinaTrecaKlasa += ukupnoKolicinaTr;

            ukupnoCenaTacne += ukupanIznos;
            ukupnoCenaPrvaKlasa += ukupanIznosP;
            ukupnoCenaDrugaKlasa += ukupanIznosD;
            ukupnoCenaTrecaKlasa += ukupanIznosT;

            kt.clear();
            k1.clear();
            k2.clear();
            k3.clear();
        }
        c1 = new PdfPCell(new Phrase("UKUPNO"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(2);
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoKolicinaTacne + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoCenaTacne + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoKolicinaPrvaKlasa + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoCenaPrvaKlasa + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoKolicinaDrugaKlasa + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoCenaDrugaKlasa + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoKolicinaTrecaKlasa + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(ukupnoCenaTrecaKlasa + "", font));
        table.addCell(c1);

        Double ukupnoKolicinaSve = ukupnoKolicinaTacne + ukupnoKolicinaPrvaKlasa + ukupnoKolicinaDrugaKlasa
                + ukupnoKolicinaTrecaKlasa;
        Double prosecnaCena = ukupanIznosSvih / ukupnoKolicinaSve;

        c1 = new PdfPCell(new Phrase(round(ukupnoKolicinaSve, 2) + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(round(prosecnaCena, 2) + "", font));
        table.addCell(c1);
        c1 = new PdfPCell(new Phrase(round(ukupanIznosSvih, 2) + "", font));
        table.addCell(c1);

        //PDV
        c1 = new PdfPCell(new Phrase("PDV"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(2);
        table.addCell(c1);
        table.addCell(c);

        Iterator it = ukupnoTacneVrednost.entrySet().iterator();
        Double ukupnoTacnePdv = 0.0;
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            if ((Double) pair.getKey() == 110) {
                ukupnoTacnePdv += (((Double) pair.getValue() / (Double) pair.getKey()) * 106);
            } else {
                ukupnoTacnePdv += (Double) pair.getValue();
            }
        }
        c1 = new PdfPCell(new Phrase(round(ukupnoTacnePdv * (110.0f / 100.0f), 2) + "", font));
        table.addCell(c1);
        table.addCell(c);

        it = ukupnoPrvaKlasaVrednost.entrySet().iterator();
        Double ukupnoPrvaKlasaPdv = 0.0;
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            ukupnoPrvaKlasaPdv += (Double) pair.getValue();
        }
        c1 = new PdfPCell(new Phrase(round(ukupnoPrvaKlasaPdv * (110.0f / 100.0f), 2) + "", font));
        table.addCell(c1);
        table.addCell(c);

        it = ukupnoDrugaKlasaVrednost.entrySet().iterator();
        Double ukupnoDrugaKlasaPdv = 0.0;
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            ukupnoDrugaKlasaPdv += (Double) pair.getValue();
        }
        c1 = new PdfPCell(new Phrase(round(ukupnoDrugaKlasaPdv * (110.0f / 100.0f), 2) + "", font));
        table.addCell(c1);
        table.addCell(c);

        it = ukupnoTrecaKlasaVrednost.entrySet().iterator();
        Double ukupnoTrecaKlasaPdv = 0.0;
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            ukupnoTrecaKlasaPdv += (Double) pair.getValue();
        }
        c1 = new PdfPCell(new Phrase(round(ukupnoTrecaKlasaPdv * (110.0f / 100.0f), 2) + "", font));
        table.addCell(c1);
        c = new PdfPCell(new Phrase(""));
        c.setColspan(2);
        table.addCell(c);

        Double ukupnoSvePDV = ukupnoTacnePdv + ukupnoPrvaKlasaPdv + ukupnoDrugaKlasaPdv + ukupnoTrecaKlasaPdv;
        c1 = new PdfPCell(new Phrase(round(ukupnoSvePDV * (110.0f / 100.0f), 2) + "", font));
        table.addCell(c1);

        c = new PdfPCell(new Phrase(""));
        c.setColspan(10);
        table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);
        //            table.addCell(c);

        c1 = new PdfPCell(new Phrase("DOBITAK"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setColspan(2);
        table.addCell(c1);

        c1 = new PdfPCell(
                new Phrase(round((ukupnoSvePDV * (110.0f / 100.0f)) - ukupanIznosSvih, 2) + "", font));
        table.addCell(c1);

        document.add(table);
        document.add(new Paragraph(" "));
    } catch (DocumentException ex) {
        Logger.getLogger(Kreiranje.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Kreiranje.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Kreiranje.class.getName()).log(Level.SEVERE, null, ex);
    }
    document.close();
    return yourFile;
}

From source file:se.billes.pdf.renderer.model.text.TableParagraph.java

License:Open Source License

@Override
public void onRender(PdfPCell cell) throws PdfRenderException {

    PdfPTable table = new PdfPTable(widths.length);

    try {/*from   w  ww.  j  av  a  2  s  .  co  m*/
        table.setTotalWidth(getTotalWidthsAsPs());
        table.setLockedWidth(true);
        table.setSpacingAfter(0f);

        for (AbstractParagraph tableCell : cells) {

            PdfPCell c = new PdfPCell();
            float[] padding = new float[] { 0f, 0f, 0f, 0f };
            if (tableCell instanceof TableCell) {
                padding = ((TableCell) tableCell).getPadding();
            }
            c.setBorderWidth(0f);
            c.setLeft(0);
            c.setTop(0);
            c.setRight(0);
            c.setBottom(0);
            c.setUseAscender(true);
            c.setIndent(0);
            c.setHorizontalAlignment(Element.ALIGN_LEFT);
            c.setVerticalAlignment(Element.ALIGN_TOP);
            c.setPaddingLeft(SizeFactory.millimetersToPostscriptPoints(padding[0]));
            c.setPaddingBottom(SizeFactory.millimetersToPostscriptPoints(padding[3]));
            c.setPaddingRight(SizeFactory.millimetersToPostscriptPoints(padding[2]));
            c.setPaddingTop(SizeFactory.millimetersToPostscriptPoints(padding[1]));
            c.setBorder(0);
            tableCell.onRender(c);

            table.addCell(c);

        }
        cell.addElement(table);
    } catch (Exception e) {
        throw new PdfRenderException(e);
    }
}

From source file:se.billes.pdf.renderer.request.factory.CellFactory.java

License:Open Source License

public PdfPCell createCell(Block block) {
    float[] margins = block.getMargins();
    PdfPCell cell = new PdfPCell();
    cell.setBorderWidth(0);/* w  w w.ja v  a 2  s.  c om*/
    cell.setVerticalAlignment(VerticalAlign.getByName(block.getVerticalAlign()).getAlignment());
    cell.setLeft(0);
    cell.setTop(0);
    cell.setRight(0);
    cell.setBottom(0);
    cell.setUseAscender(block.isUseAscender());
    cell.setIndent(0);
    cell.setPaddingLeft(SizeFactory.millimetersToPostscriptPoints(margins[0]));
    cell.setPaddingBottom(SizeFactory.millimetersToPostscriptPoints(margins[3]));
    cell.setPaddingRight(SizeFactory.millimetersToPostscriptPoints(margins[1]));
    cell.setPaddingTop(SizeFactory.millimetersToPostscriptPoints(margins[2]));
    cell.setFixedHeight(SizeFactory.millimetersToPostscriptPoints(block.getPosition()[3]));
    cell.setBorder(0);
    cell.setCellEvent(new CellBlockEvent().createEvent(block));
    cell.setRotation(block.getRotation());
    return cell;
}

From source file:se.billes.pdf.renderer.request.factory.CellFactory.java

License:Open Source License

public PdfPCell getFillCell() {
    PdfPCell fillCell = new PdfPCell();
    fillCell.setBorderWidth(0f);/*from w  w  w.  j  a  va  2  s  .c om*/
    fillCell.setLeft(0);
    fillCell.setTop(0);
    fillCell.setRight(0);
    fillCell.setBottom(0);
    fillCell.setUseAscender(true);
    fillCell.setIndent(0);
    fillCell.setHorizontalAlignment(Element.ALIGN_LEFT);
    fillCell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    fillCell.setPaddingLeft(0f);
    fillCell.setPaddingBottom(0f);
    fillCell.setPaddingRight(0f);
    fillCell.setPaddingTop(0f);
    fillCell.setBorder(0);
    renderEmptyCell(fillCell);

    return fillCell;
}

From source file:se.inera.intyg.rehabstod.service.export.pdf.HeaderEventHandler.java

License:Open Source License

private PdfPCell printedBy(String userName, String enhetsNamn) {
    LocalDateTime now = LocalDateTime.now();

    Phrase printedBy = new Phrase("", PdfExportConstants.TABLE_CELL_NORMAL);
    printedBy.add(new Chunk("Utskrift av " + userName));
    printedBy.add(Chunk.NEWLINE);/* w  w  w  .java  2 s.c  om*/
    printedBy.add(new Chunk(enhetsNamn));
    printedBy.add(Chunk.NEWLINE);
    printedBy.add(new Chunk(YearMonthDateFormatter.print(now)));
    printedBy.add(new Chunk(" - "));
    printedBy.add(new Chunk(HourMinuteFormatter.print(now)));

    PdfPCell cell = new PdfPCell(printedBy);
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    cell.setBorder(Rectangle.NO_BORDER);

    return cell;
}

From source file:servlet.GenerarPDF.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w  ww .j  a  va  2  s.c  om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    Document document = new Document();

    try {
        ConexionDB sqlite = new ConexionDB();
        java.sql.Connection cn = sqlite.Conectar();
        Statement st = cn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM mascotas;");

        response.setContentType("APPLICATION/download");
        response.setHeader("Content-Disposition", "filename=Mascotas.pdf");
        PdfWriter.getInstance(document, response.getOutputStream());
        document.open();

        Image image = Image.getInstance(
                "C:/Users/Cristian/Documents/NetBeansProjects/adopc-mascotas/web/imagenes/logo9.png");
        image.scaleAbsolute(100, 100);
        document.add(image);

        Paragraph preface = new Paragraph("LOVE MY PET");
        preface.setAlignment(Element.ALIGN_CENTER);
        document.add(preface);

        PdfPTable table = new PdfPTable(5); // 3 columns.
        table.setWidthPercentage(100); //Width 100%
        table.setSpacingBefore(10f); //Space before table
        table.setSpacingAfter(10f); //Space after table

        //Set Column widths
        float[] columnWidths = { 1f, 1f, 1f, 1f, 1f };
        table.setWidths(columnWidths);

        PdfPCell cellusuario = new PdfPCell(new Paragraph("usario"));
        cellusuario.setBorderColor(BaseColor.BLUE);
        cellusuario.setPaddingLeft(10);
        cellusuario.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellusuario.setVerticalAlignment(Element.ALIGN_MIDDLE);

        PdfPCell celltipo = new PdfPCell(new Paragraph("tipo"));
        celltipo.setBorderColor(BaseColor.BLUE);
        celltipo.setPaddingLeft(10);
        celltipo.setHorizontalAlignment(Element.ALIGN_CENTER);
        celltipo.setVerticalAlignment(Element.ALIGN_MIDDLE);

        PdfPCell cellnombre = new PdfPCell(new Paragraph("nombre"));
        cellnombre.setBorderColor(BaseColor.BLUE);
        cellnombre.setPaddingLeft(10);
        cellnombre.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellnombre.setVerticalAlignment(Element.ALIGN_MIDDLE);

        PdfPCell cellraza = new PdfPCell(new Paragraph("raza"));
        cellraza.setBorderColor(BaseColor.BLUE);
        cellraza.setPaddingLeft(10);
        cellraza.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellraza.setVerticalAlignment(Element.ALIGN_MIDDLE);

        PdfPCell celledad = new PdfPCell(new Paragraph("edad"));
        celledad.setBorderColor(BaseColor.BLUE);
        celledad.setPaddingLeft(10);
        celledad.setHorizontalAlignment(Element.ALIGN_CENTER);
        celledad.setVerticalAlignment(Element.ALIGN_MIDDLE);

        table.addCell(cellusuario);
        table.addCell(celltipo);
        table.addCell(cellnombre);
        table.addCell(cellraza);
        table.addCell(celledad);

        while (rs.next()) {

            PdfPCell cell1 = new PdfPCell(new Paragraph(rs.getString(1)));
            cell1.setPaddingLeft(1);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Paragraph(rs.getString(2)));
            cell2.setPaddingLeft(2);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(new Paragraph(rs.getString(3)));
            cell3.setPaddingLeft(3);
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell3);

            PdfPCell cell4 = new PdfPCell(new Paragraph(rs.getString(4)));
            cell4.setPaddingLeft(4);
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell4.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell4);

            PdfPCell cell5 = new PdfPCell(new Paragraph(rs.getString(5)));
            cell5.setPaddingLeft(5);
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell5.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell5);

        }

        document.add(table);
        //Add more content here
        cn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();
}

From source file:Servlets.GenerarPinesGrupo.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String[] idEstudiante = req.getParameterValues("imprimir");

    resp.setContentType("application/pdf");
    OutputStream out = resp.getOutputStream();

    String foto = getServletContext().getRealPath("/recursos/img/logoColegio.png");
    req.setCharacterEncoding("UTF-8");

    try {//from w w  w  .  j a v a 2s. c  o  m
        try {
            Document documento = new Document();
            PdfWriter.getInstance(documento, out);

            documento.open();

            Paragraph par1 = new Paragraph();
            Font fontit = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.GRAY);
            par1.add(new Phrase("Pin de Acceso HeartsTics", fontit));
            par1.setAlignment(Element.ALIGN_CENTER);
            par1.add(new Phrase(Chunk.NEWLINE));
            par1.add(new Phrase(Chunk.NEWLINE));
            documento.add(par1);
            //                Image image = Image.getInstance("E:\\ArchivosVarios\\logoColegio.png");
            //                image.scalePercent(50);
            Image image = Image.getInstance(foto);
            image.scalePercent(60);

            Font fuentetabla = FontFactory.getFont("Arial", 8, Font.BOLD, BaseColor.BLACK);
            Font fuentetablaPin = FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK);
            Font fuentetablaHeader = FontFactory.getFont("Arial", 9, Font.BOLD, BaseColor.BLACK);
            Servicio controlador = new Servicio();
            for (int i = 0; i < idEstudiante.length; i++) {
                int idEst = Integer.parseInt(idEstudiante[i]);
                Pin estudiante = controlador.pinEstudiante(idEst);
                PdfPTable tabla = new PdfPTable(3);

                tabla.setWidthPercentage(60);

                PdfPCell celdaHeader = new PdfPCell(
                        new Paragraph("COLEGIO SAGRADOS CORAZONES", fuentetablaHeader));
                celdaHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);
                celdaHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
                celdaHeader.setColspan(3);
                tabla.addCell(celdaHeader);
                PdfPCell celda01 = new PdfPCell(image);
                //PdfPCell celda01 = new PdfPCell(new Paragraph("imagen"));
                celda01.setHorizontalAlignment(Element.ALIGN_CENTER);
                celda01.setVerticalAlignment(Element.ALIGN_MIDDLE);
                celda01.setRowspan(2);
                celda01.setBorder(Rectangle.NO_BORDER);
                celda01.setBorder(Rectangle.LEFT);
                tabla.addCell(celda01);

                PdfPCell celda1 = new PdfPCell(new Paragraph(
                        "Estudiante: " + estudiante.getNombres() + " " + estudiante.getApellidos(),
                        fuentetabla));
                PdfPCell celda2 = new PdfPCell(new Paragraph("Pin: " + estudiante.getIdPin(), fuentetablaPin));
                PdfPCell celda3 = new PdfPCell(
                        new Paragraph("Fecha creacion: " + estudiante.getInicio(), fuentetabla));
                PdfPCell celda4 = new PdfPCell(
                        new Paragraph("Fecha vencimiento: " + estudiante.getFin(), fuentetabla));

                celda1.setHorizontalAlignment(Element.ALIGN_CENTER);
                celda1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                celda1.setColspan(2);
                celda1.setBorder(Rectangle.NO_BORDER);
                celda1.setBorder(Rectangle.RIGHT);

                celda2.setHorizontalAlignment(Element.ALIGN_CENTER);
                celda2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                celda2.setColspan(2);
                celda2.setBorder(Rectangle.NO_BORDER);
                celda2.setBorder(Rectangle.RIGHT);

                PdfPTable tableFecha = new PdfPTable(2);
                tableFecha.setWidthPercentage(60);
                celda3.setHorizontalAlignment(Element.ALIGN_CENTER);
                celda4.setHorizontalAlignment(Element.ALIGN_CENTER);

                tabla.addCell(celda1);
                tabla.addCell(celda2);
                tableFecha.addCell(celda3);
                tableFecha.addCell(celda4);
                Paragraph par2 = new Paragraph();

                par2.add(new Phrase(Chunk.NEWLINE));
                documento.add(par2);

                documento.add(tabla);
                documento.add(tableFecha);

            }
            documento.close();

        } catch (Exception e) {
            e.getMessage();
        }
    } finally {
        out.close();
    }

}

From source file:Servlets.GenerarPinEstudiante.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    int idEstudiante = Integer.parseInt(req.getParameter("idEst"));

    resp.setContentType("application/pdf");
    OutputStream out = resp.getOutputStream();

    String foto = getServletContext().getRealPath("/recursos/img/logoColegio.png");
    req.setCharacterEncoding("UTF-8");

    try {/*from   w ww . j  a v a2 s .co  m*/
        try {
            Document documento = new Document();
            PdfWriter.getInstance(documento, out);

            documento.open();

            Paragraph par1 = new Paragraph();
            Font fontit = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.GRAY);
            par1.add(new Phrase("Pin de Acceso HeartsTics", fontit));
            par1.setAlignment(Element.ALIGN_CENTER);
            par1.add(new Phrase(Chunk.NEWLINE));
            par1.add(new Phrase(Chunk.NEWLINE));
            documento.add(par1);
            //                Image image = Image.getInstance("E:\\ArchivosVarios\\logoColegio.png");
            //                image.scalePercent(50);
            Image image = Image.getInstance(foto);
            image.scalePercent(60);

            Font fuentetabla = FontFactory.getFont("Arial", 8, Font.BOLD, BaseColor.BLACK);
            Font fuentetablaPin = FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK);
            Font fuentetablaHeader = FontFactory.getFont("Arial", 9, Font.BOLD, BaseColor.BLACK);

            Servicio controlador = new Servicio();
            Pin estudiante = controlador.pinEstudiante(idEstudiante);
            PdfPTable tabla = new PdfPTable(3);

            tabla.setWidthPercentage(60);

            PdfPCell celdaHeader = new PdfPCell(new Paragraph("COLEGIO SAGRADOS CORAZONES", fuentetablaHeader));
            celdaHeader.setVerticalAlignment(Element.ALIGN_MIDDLE);
            celdaHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
            celdaHeader.setColspan(3);
            tabla.addCell(celdaHeader);
            PdfPCell celda01 = new PdfPCell(image);
            //PdfPCell celda01 = new PdfPCell(new Paragraph("imagen"));
            celda01.setHorizontalAlignment(Element.ALIGN_CENTER);
            celda01.setVerticalAlignment(Element.ALIGN_MIDDLE);
            celda01.setRowspan(2);
            celda01.setBorder(Rectangle.NO_BORDER);
            celda01.setBorder(Rectangle.LEFT);
            tabla.addCell(celda01);

            PdfPCell celda1 = new PdfPCell(new Paragraph(
                    "Estudiante: " + estudiante.getNombres() + " " + estudiante.getApellidos(), fuentetabla));
            PdfPCell celda2 = new PdfPCell(new Paragraph("Pin: " + estudiante.getIdPin(), fuentetablaPin));
            PdfPCell celda3 = new PdfPCell(
                    new Paragraph("Fecha creacion: " + estudiante.getInicio(), fuentetabla));
            PdfPCell celda4 = new PdfPCell(
                    new Paragraph("Fecha vencimiento: " + estudiante.getFin(), fuentetabla));

            celda1.setHorizontalAlignment(Element.ALIGN_CENTER);
            celda1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            celda1.setColspan(2);
            celda1.setBorder(Rectangle.NO_BORDER);
            celda1.setBorder(Rectangle.RIGHT);

            celda2.setHorizontalAlignment(Element.ALIGN_CENTER);
            celda2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            celda2.setColspan(2);
            celda2.setBorder(Rectangle.NO_BORDER);
            celda2.setBorder(Rectangle.RIGHT);

            PdfPTable tableFecha = new PdfPTable(2);
            tableFecha.setWidthPercentage(60);
            celda3.setHorizontalAlignment(Element.ALIGN_CENTER);
            celda4.setHorizontalAlignment(Element.ALIGN_CENTER);

            tabla.addCell(celda1);
            tabla.addCell(celda2);
            tableFecha.addCell(celda3);
            tableFecha.addCell(celda4);
            Paragraph par2 = new Paragraph();

            par2.add(new Phrase(Chunk.NEWLINE));
            documento.add(par2);

            documento.add(tabla);
            documento.add(tableFecha);

            documento.close();

        } catch (Exception e) {
            e.getMessage();
        }
    } finally {
        out.close();
    }

}