Example usage for com.itextpdf.text Phrase add

List of usage examples for com.itextpdf.text Phrase add

Introduction

In this page you can find the example usage for com.itextpdf.text Phrase add.

Prototype

@Override
public boolean add(final Element element) 

Source Link

Document

Adds a Chunk, Anchor or another Phrase to this Phrase.

Usage

From source file:ru.trett.cis.services.PDFBuilderImpl.java

License:Open Source License

@Override
public String createPDF() throws IOException, DocumentException, ApplicationException {
    try {/*w ww. j  a  v  a2s  .c  o m*/
        String templatePath = servletContext.getRealPath("WEB-INF/resources/template-settings.xml");
        Map<String, List<String>> data = TemplateParser.parse(new File(templatePath));
        String regularFontPath = servletContext.getRealPath("WEB-INF/resources/fonts/OpenSans-Regular.ttf");
        if (regularFontPath == null)
            throw new ApplicationException("Regular Font file was not found");
        BaseFont bfr = BaseFont.createFont(regularFontPath, BaseFont.IDENTITY_H, true);
        String boldFontPath = servletContext.getRealPath("WEB-INF/resources/fonts/OpenSans-Bold.ttf");
        if (boldFontPath == null)
            throw new ApplicationException("Bold Font file was not found");
        BaseFont bfb = BaseFont.createFont(boldFontPath, BaseFont.IDENTITY_H, true);
        regularFont = new Font(bfr);
        regularFont.setSize(10);
        boldFont = new Font(bfb);
        boldFont.setSize(10);

        File file = File.createTempFile("order", ".pdf");
        Document doc = new Document(PageSize.A4);
        Chunk glue = new Chunk(new VerticalPositionMark());
        PdfWriter.getInstance(doc, new FileOutputStream(file));
        doc.open();
        doc.newPage();

        //title
        Paragraph p = new Paragraph(data.get("header").get(0), boldFont);
        p.setAlignment(Element.ALIGN_CENTER);
        doc.add(p);
        doc.add(Chunk.NEWLINE);

        //body
        for (String text : data.get("text")) {
            p = new Paragraph(text, regularFont);
            doc.add(p);
        }

        //table
        doc.add(Chunk.NEWLINE);
        List<String> cols = data.get("cols");
        PdfPTable table = new PdfPTable(cols.size());
        table.setWidthPercentage(100);

        cols.forEach(x -> table.addCell(getCell(x, PdfPCell.ALIGN_CENTER, boldFont)));

        for (Asset asset : assets) {
            table.addCell(getCell(String.format("%s %s %s", asset.getDeviceModel().getDeviceType().getType(),
                    asset.getDeviceModel().getDeviceBrand().getBrand(), asset.getDeviceModel().getModel()),
                    PdfPCell.ALIGN_CENTER, regularFont));
            table.addCell(getCell(asset.getSerialNumber(), PdfPCell.ALIGN_CENTER, regularFont));
            table.addCell(getCell(asset.getInventoryNumber(), PdfPCell.ALIGN_CENTER, regularFont));
        }

        table.getRows();
        doc.add(table);
        doc.add(Chunk.NEWLINE);
        doc.add(Chunk.NEWLINE);

        //signers
        Phrase phrase = new Phrase(new Chunk(data.get("signers").get(0) + " ", regularFont));
        phrase.add(Chunk.NEWLINE);
        phrase.add(new Chunk(issuer.getFirstName() + " " + issuer.getLastName() + "  \\__________________",
                regularFont));
        phrase.add(Chunk.NEWLINE);
        phrase.add(Chunk.NEWLINE);
        phrase.add(new Chunk(data.get("signers").get(1) + " ", regularFont));
        phrase.add(Chunk.NEWLINE);
        phrase.add(new Chunk(employee.getFirstName() + " " + employee.getLastName() + "  \\__________________",
                regularFont));
        doc.add(phrase);

        //date
        doc.add(Chunk.NEWLINE);
        doc.add(Chunk.NEWLINE);
        p = new Paragraph(data.get("place").get(0), regularFont);
        p.add(new Chunk(glue));
        p.add(date.format(dateFormat));
        doc.add(p);
        doc.close();
        return file.getPath();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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);//from  w  w  w .j a v  a 2s . c  o m
    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:se.inera.intyg.rehabstod.service.export.pdf.PdfExportServiceImpl.java

License:Open Source License

private Phrase getGrader(SjukfallEnhet is) {
    boolean first = true;
    Phrase grader = new Phrase();
    for (Integer grad : is.getGrader()) {
        if (!first) {
            grader.add(new Chunk(UNICODE_RIGHT_ARROW_SYMBOL + " ", unicodeCapableFont));
        }// w  ww.  j a  va2s.  c  o  m
        if (grad == is.getAktivGrad()) {
            grader.add(new Chunk(grad.toString() + "% ", PdfExportConstants.TABLE_CELL_BOLD));
        } else {
            grader.add(new Chunk(grad.toString() + "% ", PdfExportConstants.TABLE_CELL_NORMAL));
        }
        first = false;
    }
    return grader;
}

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

License:Open Source License

private Phrase getPersonnummerColumn(SjukfallEnhet is) {
    Phrase p = new Phrase();
    p.add(new Chunk(is.getPatient().getId() != null ? is.getPatient().getId() : "",
            PdfExportConstants.TABLE_CELL_NORMAL));
    return p;/*from   ww  w  .j  av a  2 s. c  o  m*/
}

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

License:Open Source License

private Phrase getlangdText(SjukfallEnhet is) {
    Phrase p = new Phrase();
    p.add(new Chunk(String.format(FORMAT_ANTAL_DAGAR, is.getDagar()), PdfExportConstants.TABLE_CELL_NORMAL));
    return p;/*  www  .  j av a 2s. c  o m*/
}

From source file:tn.com.hitechart.eds.Util.pdfRpport.FirstPdf.java

private void addContent(Document document) throws DocumentException {

    // Second parameter is the number of the chapter

    Phrase phrase = new Phrase("", catFontblanc);
    phrase.add("");
    Chapter catPart = new Chapter(new Paragraph(phrase), 0);
    Chapter catParttask = new Chapter(new Paragraph(phrase), 0);
    Paragraph p = new Paragraph("", catFontblanc);

    Section subCatPart = catPart.addSection(p);

    // subCatPart.add(new Paragraph("Hello1111111111111111111111 "+user.getLogin()));

    //subPara = new Paragraph("Subcategory 22222222222222 "+pointage.getDate_timeInM(), subFont);
    //subCatPart = catPart.addSection(subPara);
    //subCatPart.add(new Paragraph("Paragraph 1"));
    //subCatPart.add(new Paragraph("Paragraph 2"));
    //subCatPart.add(new Paragraph("Paragraph 3"));

    // add a list
    //createList(subCatPart);
    //Paragraph paragraph = new Paragraph();
    //addEmptyLine(paragraph, 5);
    //subCatPart.add(paragraph);

    // add a table
    createTable(subCatPart);//from   w  w  w.ja va  2 s  . c  o  m

    // now add all this to the document
    document.add(catPart);
    //document.add(catParttask);
    // Next section
    // anchor = new Anchor("Second Chapter", catFont);
    //anchor.setName("Second Chapter");

    // Second parameter is the number of the chapter
    //catPart = new Chapter(new Paragraph(anchor), 1);

    //subPara = new Paragraph("Subcategory", subFont);
    //subCatPart = catPart.addSection(subPara);
    //subCatPart.add(new Paragraph("This is a very important message"));

    // now add all this to the document
    //document.add(catPart);

}

From source file:ui.MainDriver.java

public void printTicket() {

    for (int t = 0; t < currentTicketList.size(); t++) {
        String myCodeText = currentTicketList.get(t).getTicketID() + " %0A "
                + currentTicketList.get(t).getBooking().getBookingID() + " - "
                + currentTicketList.get(t).getSchedule().getRoute().getDestination() + " - "
                + currentTicketList.get(t).getPassengerName() + " - "
                + currentTicketList.get(t).getPassengerContactNo() + " - "
                + currentTicketList.get(t).getSeatNo();
        String filePath = "src/ticket/" + currentTicketList.get(t).getTicketID() + ".png";
        int size = 165;
        String fileType = "png";
        File myFile = new File(filePath);
        try {// w w w.java2  s. co m
            Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
            int width = byteMatrix.getWidth();
            BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
            image.createGraphics();

            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, width, width);
            graphics.setColor(Color.BLACK);

            for (int i = 0; i < width; i++)
                for (int j = 0; j < width; j++)
                    if (byteMatrix.get(i, j))
                        graphics.fillRect(i, j, 1, 1);

            ImageIO.write(image, fileType, myFile);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("src/ticketpdf/" + currentTicketList.get(t).getTicketID() + "DATE"
                            + new SimpleDateFormat("ddMMyyHHmmss").format(new Date()) + ".pdf"));
            document.open();

            Phrase p1 = new Phrase();
            Paragraph para = new Paragraph();
            com.itextpdf.text.Font font = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.COURIER,
                    32, com.itextpdf.text.Font.BOLDITALIC);
            com.itextpdf.text.Font timesRomanfont = new com.itextpdf.text.Font(
                    com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 16, com.itextpdf.text.Font.BOLD);
            Chunk cc = new Chunk("#################################################################",
                    timesRomanfont);
            p1.add(cc);
            p1.add(Chunk.NEWLINE);
            p1.add(Chunk.NEWLINE);
            Chunk c = new Chunk("CJH BUS EXPRESS SDN. BHD.", font);
            p1.add(c);
            p1.add(Chunk.NEWLINE);
            p1.add(Chunk.NEWLINE);
            p1.add(cc);
            para.setAlignment(Element.ALIGN_CENTER);
            para.add(p1);
            document.add(para);

            Image image1 = Image.getInstance(filePath);
            document.add(image1);

            Phrase phrase = new Phrase();
            Paragraph paragraph = new Paragraph();

            Chunk c1 = new Chunk("Ticket ID                       : " + currentTicketList.get(t).getTicketID(),
                    timesRomanfont);
            phrase.add(c1);
            phrase.add(Chunk.NEWLINE);

            Chunk c2 = new Chunk(
                    "Booking ID                    : " + currentTicketList.get(t).getBooking().getBookingID(),
                    timesRomanfont);
            phrase.add(c2);
            phrase.add(Chunk.NEWLINE);

            Chunk c3 = new Chunk(
                    "Destination                    : "
                            + currentTicketList.get(t).getSchedule().getRoute().getDestination(),
                    timesRomanfont);
            phrase.add(c3);
            phrase.add(Chunk.NEWLINE);

            Chunk cDate = new Chunk(
                    "Departure Date             : " + currentTicketList.get(t).getSchedule().getDepartureDate(),
                    timesRomanfont);
            phrase.add(cDate);
            phrase.add(Chunk.NEWLINE);

            Chunk cTime = new Chunk(
                    "Departure Time            : " + currentTicketList.get(t).getSchedule().getDepartureTime(),
                    timesRomanfont);
            phrase.add(cTime);
            phrase.add(Chunk.NEWLINE);

            Chunk c4 = new Chunk("Passenger Name            : " + currentTicketList.get(t).getPassengerName(),
                    timesRomanfont);
            phrase.add(c4);
            phrase.add(Chunk.NEWLINE);

            Chunk c5 = new Chunk("Passenger Contact No   : " + currentTicketList.get(t).getPassengerContactNo(),
                    timesRomanfont);
            phrase.add(c5);
            phrase.add(Chunk.NEWLINE);

            Chunk c6 = new Chunk("Seat No                           : " + currentTicketList.get(t).getSeatNo(),
                    timesRomanfont);
            phrase.add(c6);
            phrase.add(Chunk.NEWLINE);

            if (reprintCounter == true) {
                Chunk cR = new Chunk("--- REPRINT ---", timesRomanfont);
                phrase.add(cR);
                phrase.add(Chunk.NEWLINE);
            }

            Chunk c7 = new Chunk("#################################################################",
                    timesRomanfont);
            phrase.add(c7);

            paragraph.add(phrase);
            document.add(paragraph);

            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //            try {
        //                PdfReader reader = new PdfReader("src/ticketpdf/" + currentTicketList.get(t).getTicketID() + "DATE" + new SimpleDateFormat("ddMMyyHHmmss").format(new Date()) + ".pdf");
        //                int n = reader.getNumberOfPages();
        //                PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("src/ticketpdfWM/" + currentTicketList.get(t).getTicketID() + "WMDATE" + new SimpleDateFormat("ddMMyyHHmmss").format(new Date()) + ".pdf"));
        //                int i = 0;
        //                PdfContentByte under;
        //                Image img = Image.getInstance("src/images/b1.jpg");
        //                img.setAbsolutePosition(200, 400);
        //                while (i < n) {
        //                  i++;
        //                  under = stamp.getUnderContent(i);
        //                  under.addImage(img);
        //                }
        //                stamp.close();
        //            }
        //            catch (Exception de) {
        //                de.printStackTrace();
        //            }
    }
}

From source file:Utilidades.PDF.java

private PdfPTable createFile() {
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell;/* w  w w. ja va 2 s. c  om*/
    //Font green = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.GREEN);
    int i = 0;
    for (Pregunta p : listaPreguntas) {
        cell = new PdfPCell(new Phrase(p.getTexto()));
        // Agregamos la pregunta
        table.addCell(cell);
        // Agregamos las respuestas
        ArrayList<Respuesta> respuestasPregunta = listaRespuestas.get(i);
        Phrase phrase = new Phrase();
        for (Respuesta r : respuestasPregunta) {
            if (!r.isCorrecta())
                phrase.add(r.getTexto() + "\n");
            else {
                phrase.add("* " + r.getTexto() + "\n");

            }
        }
        cell.addElement(phrase);
        table.addCell(cell);
        i++;
    }

    return table;
}