Example usage for com.itextpdf.text PageSize A4

List of usage examples for com.itextpdf.text PageSize A4

Introduction

In this page you can find the example usage for com.itextpdf.text PageSize A4.

Prototype

Rectangle A4

To view the source code for com.itextpdf.text PageSize A4.

Click Source Link

Document

This is the a4 format

Usage

From source file:fr.ybonnel.breizhcamppdf.PdfRenderer.java

License:Apache License

private void createFirstPage() throws DocumentException, IOException {
    Rectangle savePagesize = document.getPageSize();
    document.setPageSize(PageSize.A4);
    document.newPage();/*from   www .  ja va2 s  . c o m*/
    Image imageLogo = Image.getInstance(PdfRenderer.class.getResource("/logo.png"));
    imageLogo.scaleToFit(document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(),
            imageLogo.getHeight());
    document.add(imageLogo);

    Paragraph paragraph = new Paragraph("21, 22 et 23 mai");
    paragraph.setSpacingAfter(80);
    paragraph.getFont().setSize(20);
    paragraph.setAlignment(Element.ALIGN_CENTER);
    document.add(paragraph);

    Paragraph title = new Paragraph("Programme 2014");
    title.setSpacingAfter(100);
    title.getFont().setSize(56);
    title.setAlignment(Element.ALIGN_CENTER);
    document.add(title);

    PdfPTable sponsors = new PdfPTable(4);
    sponsors.setWidthPercentage(90f);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/LogoZenika-H-(Quadri).jpg", 2);
    addSponsor(sponsors,
            "http://blogs.technet.com/resized-image.ashx/__size/450x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-80-54/6864.Microsoft-Logo.png",
            2);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/Google_Logo_3564x1189.png", 1);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/cbpaas.jpeg", 1);
    addSponsor(sponsors, "https://github.global.ssl.fastly.net/images/modules/logos_page/GitHub-Logo.png", 1);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/sonarsource-300x94.png", 1);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/serli.png", 1);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/Logo_sqli_group.png", 1);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/SofteamCadextanLogo_w200.png", 1);
    addSponsor(sponsors, "http://www.breizhcamp.org/img/logoSII_w200.png", 1);
    document.add(sponsors);

    Image istic = Image.getInstance("http://www.breizhcamp.org/img/Logo_ISTIC_BLACK_FRA.jpg");
    istic.scaleToFit(150, 150);
    istic.setAlignment(Element.ALIGN_CENTER);
    document.add(istic);

    document.setPageSize(savePagesize);
}

From source file:fr.ybonnel.breizhcamppdf.PdfRenderer.java

License:Apache License

private List<Talk> createProgrammePages() throws DocumentException, IOException {
    List<Talk> talksToExplain = new ArrayList<>();
    document.setPageSize(PageSize.A4.rotate());
    Font font = new Font();
    font.setStyle(Font.BOLD);/*from  w ww . j  a v  a2 s . c o m*/
    font.setSize(14);

    for (String date : service.getDates()) {

        Set<String> tracksInPage = new HashSet<>();

        Map<String, Talk> precedentTalk = new HashMap<>();
        PdfPTable table = createBeginningOfPage(font, date);
        for (String creneau : service.getCreneaux().get(date)) {
            // Nouvelle page  14h
            if (creneau.startsWith("14:00") && !tracksInPage.isEmpty()) {
                document.add(table);

                addLegend(tracksInPage);
                table = createBeginningOfPage(font, date);
            }

            PdfPCell cellCreneau = new PdfPCell();
            cellCreneau.setPaddingBottom(10);
            Paragraph startTime = new Paragraph(creneau);
            startTime.setAlignment(Element.ALIGN_CENTER);
            cellCreneau.addElement(startTime);
            Paragraph endTime = new Paragraph(getEndTime(date, creneau));
            endTime.setAlignment(Element.ALIGN_CENTER);
            cellCreneau.addElement(endTime);
            table.addCell(cellCreneau);
            for (String room : service.getRooms(date)) {
                PdfPCell cell = new PdfPCell();
                cell.setPaddingBottom(10);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);

                Talk talk = service.getTalkByDateAndCreneauxAndRoom(date, creneau, room);
                if (talk != null) {
                    talksToExplain.add(talk);
                    remplirCellWithTalk(cell, talk);
                    cell.setRowspan(getRowSpan(date, talk));
                    precedentTalk.put(room, talk);
                    tracksInPage.add(talk.getTrack());
                    table.addCell(cell);
                } else {
                    talk = precedentTalk.get(room);
                    if (!(talk != null && talk.getEnd().compareTo(creneau) > 0)) {
                        table.addCell(cell);
                    }
                }
            }
        }
        document.add(table);
        addLegend(tracksInPage);
    }
    return talksToExplain;
}

From source file:fr.ybonnel.breizhcamppdf.PdfRenderer.java

License:Apache License

private void createTalksPages(List<Talk> talksToExplain) throws DocumentException, IOException {
    document.setPageSize(PageSize.A4);
    document.newPage();//  w ww. j  ava  2  s.c  o  m

    Paragraph paragraph = new Paragraph("Liste des talks");
    paragraph.setSpacingAfter(25);
    paragraph.getFont().setSize(25);
    paragraph.setAlignment(Element.ALIGN_CENTER);
    document.add(paragraph);

    for (TalkDetail talk : Lists.transform(talksToExplain, new Function<Talk, TalkDetail>() {
        @Override
        public TalkDetail apply(Talk input) {
            return TalkService.INSTANCE.getTalkDetail(input);
        }
    })) {

        if (talk == null) {
            continue;
        }

        Paragraph empty = new Paragraph(" ");
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);
        table.setKeepTogether(true);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        PdfPCell cell;
        Chunk titleTalk = new Chunk(talk.getTitle(), talkFontTitle);
        titleTalk.setLocalDestination("talk" + talk.getId());
        float[] withTitle = { 0.05f, 0.95f };
        PdfPTable titleWithFormat = new PdfPTable(withTitle);
        titleWithFormat.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        titleWithFormat.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

        Image image = AvatarService.INSTANCE.getImage(PdfRenderer.class
                .getResource("/formats/" + talk.getTalk().getFormat().replaceAll(" ", "") + ".png"));
        titleWithFormat.addCell(image);
        titleWithFormat.addCell(new Paragraph(titleTalk));

        table.addCell(titleWithFormat);

        table.addCell(empty);

        table.addCell(new Paragraph("Salle " + talk.getTalk().getRoom() + " de " + talk.getTalk().getStart()
                + "  " + talk.getTalk().getEnd(), presentFont));

        table.addCell(empty);

        cell = new PdfPCell();
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
        for (Element element : HTMLWorker
                .parseToList(new StringReader(markdownProcessor.markdown(talk.getDescription())), null)) {
            if (element instanceof Paragraph) {
                ((Paragraph) element).setAlignment(Element.ALIGN_JUSTIFIED);
            }
            cell.addElement(element);
        }
        table.addCell(cell);

        table.addCell(empty);

        table.addCell(new Paragraph("Prsent par :", presentFont));

        float[] widthSpeaker = { 0.05f, 0.95f };
        for (Speaker speaker : talk.getSpeakers()) {
            PdfPTable speakerWithAvatar = new PdfPTable(widthSpeaker);
            speakerWithAvatar.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            speakerWithAvatar.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

            speakerWithAvatar.addCell(AvatarService.INSTANCE.getImage(speaker.getAvatar()));
            speakerWithAvatar.addCell(new Phrase(speaker.getFullname()));
            table.addCell(speakerWithAvatar);
        }

        table.addCell(empty);
        table.addCell(empty);
        document.add(table);
    }
}

From source file:fr.ybonnel.breizhcamppdf.RoomPdfRenderer.java

License:Apache License

private List<Talk> createProgrammePages() throws DocumentException, IOException {
    List<Talk> talksToExplain = new ArrayList<>();
    document.setPageSize(PageSize.A4);
    Font font = new Font();
    font.setStyle(Font.BOLD);/*w ww . ja  v a 2  s .  c o  m*/
    font.setSize(14);

    for (String date : service.getDates()) {

        for (String room : service.getRooms(date)) {

            Set<String> tracksInPage = new HashSet<>();

            Map<String, Talk> precedentTalk = new HashMap<>();
            PdfPTable table = createBeginningOfPage(font, date, room);
            for (String creneau : service.getCreneaux().get(date)) {

                PdfPCell cellCreneau = new PdfPCell();
                cellCreneau.setPaddingBottom(10);
                Paragraph startTime = new Paragraph(creneau);
                startTime.setAlignment(Element.ALIGN_CENTER);
                cellCreneau.addElement(startTime);
                Paragraph endTime = new Paragraph(getEndTime(date, creneau));
                endTime.setAlignment(Element.ALIGN_CENTER);
                cellCreneau.addElement(endTime);
                table.addCell(cellCreneau);

                PdfPCell cell = new PdfPCell();
                cell.setPaddingBottom(10);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);

                Talk talk = service.getTalkByDateAndCreneauxAndRoom(date, creneau, room);
                if (talk != null) {
                    talksToExplain.add(talk);
                    remplirCellWithTalk(cell, talk);
                    cell.setRowspan(getRowSpan(date, talk));
                    precedentTalk.put(room, talk);
                    tracksInPage.add(talk.getTrack());
                    table.addCell(cell);
                } else {
                    talk = precedentTalk.get(room);
                    if (!(talk != null && talk.getEnd().compareTo(creneau) > 0)) {
                        table.addCell(cell);
                    }
                }

            }
            document.add(table);
            addLegend(tracksInPage);
        }
    }
    return talksToExplain;
}

From source file:gestionEtat.imprimerBulletin.BulletinCtrl.java

@Override
public void enregistrerBulletin() {
    if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().containsKey("user")) {

        if (eleveAnneeClasse.getId() != null) {
            classematieres.clear();/*  ww  w . j a  v a  2  s  .co m*/
            matieres.clear();
            notesPropres.clear();
            matiereCompses.clear();
            eleveAnneeClasse = eleveAnneeClasseFacade.find(eleveAnneeClasse.getId());
            if (sequence != null) {
                Annee an = anneeFacade.findByEtatSingle(true);
                Eleveanneeclasse eleveClasse = eleveAnneeClasseFacade.getEleveAnneeClaseByAnneClasse(
                        eleveAnneeClasse.getEleve().getIdeleve(), an.getIdannee());
                if (eleveClasse != null) {
                    List<Naturematiere> nature = natureMatiereFacade.findAll();
                    if (!nature.isEmpty()) {
                        List<Evaluation> notes = evaluationFacadeLocal.getByEleveAnneeSequence(
                                eleveAnneeClasse.getEleve().getIdeleve(), an.getIdannee(),
                                sequence.getIdsequencean());

                        /*if (!notes.isEmpty()) {*/
                        try {

                            String bulletinName = "" + eleveAnneeClasse.getEleve().getNom() + "_"
                                    + eleveAnneeClasse.getEleve().getPrenom() + "_"
                                    + sequence.getIdsequence().getNom() + ".pdf";
                            Document bulletin = new Document();

                            PdfWriter.getInstance(bulletin, new FileOutputStream(Utilitaires.path + "/"
                                    + Utilitaires.repertoireParDefautBulletin + "/" + bulletinName));

                            bulletin.setMargins(5, 5, 5, 5);
                            bulletin.addCreator("School Manager");
                            bulletin.setPageSize(PageSize.A4);

                            bulletin.open();

                            //tableau qui va contenir les notes
                            PdfPTable table = new PdfPTable(5);
                            table.setComplete(true);

                            //entete de bulletin
                            table.addCell(
                                    PrintUtils.createPdfPCell("COLLEGE POZAM Anne Scolaire " + an.getCode()
                                            + " - " + (an.getCode() + 1) + " " + an.getTheme(), 5, detail));
                            table.addCell(PrintUtils.createPdfPCell(
                                    "BULLETIN DE NOTES DE : " + sequence.getIdsequence().getNom(), 5, true));
                            table.addCell(PrintUtils
                                    .createPdfPCell("Nom : " + eleveAnneeClasse.getEleve().getNom(), 2, false));

                            PdfPCell cellLogo = new PdfPCell(new Paragraph("  "));
                            cellLogo.setRowspan(2);
                            table.addCell(cellLogo);

                            table.addCell(PrintUtils.createPdfPCell(
                                    "Matricule : " + eleveAnneeClasse.getEleve().getMatricule(), 2, false));

                            table.addCell(PrintUtils.createPdfPCell(
                                    "Prnom : " + eleveAnneeClasse.getEleve().getPrenom(), 2, false));

                            //table.addCell("kenne");
                            PdfPCell cellClasse = new PdfPCell(
                                    new Paragraph("Classe : " + eleveClasse.getIdclasse().getNom()));
                            cellClasse.setColspan(2);
                            table.addCell(cellClasse);

                            //debut de saisis de notes
                            PdfPTable entete = new PdfPTable(5);
                            table.addCell(new Paragraph("Dicipline"));
                            table.addCell(PrintUtils.createPdfPCell("Note ", true));
                            table.addCell(PrintUtils.createPdfPCell("Cofficient ", true));
                            table.addCell(PrintUtils.createPdfPCell("Total", true));
                            table.addCell(PrintUtils.createPdfPCell("Apprciation", true));

                            //partie qui contient es totaux
                            PdfPTable total = new PdfPTable(5);

                            int totalCoef = 0;
                            float totalPoint = 0;

                            classematieres = classeMatiereFacade
                                    .get(eleveAnneeClasse.getIdclasse().getIdclasse());
                            if (!classematieres.isEmpty()) {
                                for (Classematiere m : classematieres) {
                                    matieres.add(m.getIdmatiere());
                                }

                                /*for (Evaluation e : notes) {
                                 if (matieres.contains(e.getIdmatiere())) {
                                 notesPropres.add(e);
                                 matiereCompses.add(e.getIdmatiere());
                                 }
                                 }*/
                                for (int i = 0; i < nature.size(); i++) {
                                    table.addCell(
                                            PrintUtils.createPdfPCell("Matires " + nature.get(i).getLibelle(),
                                                    5, false, PrintUtils.blueFont));

                                    int totalCoefBloc = 0;
                                    int totalPointBloc = 0;

                                    for (int a = 0; a < matieres.size(); a++) {

                                        if (matiereCompses.contains(matieres.get(a))) {
                                            //if (Objects.equals(notesPropres.get(a).getIdmatiere(), nature.get(i).getIdnaturematiere())) {;
                                            //decompte des coefficient et nombre point globaux
                                            //totalCoef += notesPropres.get(a).getIdmatiere().getCoeficient();
                                            // totalPoint += notesPropres.get(a).getNote() * notesPropres.get(a).getIdmatiere().getCoeficient();

                                            //decompte des coef et point par groupe                                          
                                            //totalCoefBloc += notesPropres.get(a).getIdmatiere().getCoeficient();
                                            //totalPointBloc += notesPropres.get(a).getNote() * notesPropres.get(a).getIdmatiere().getCoeficient();
                                            //                                                    table.addCell(PrintUtils.createPdfPCell("" + notesPropres.get(a).getIdmatiere().getIdtypematiere().getLibelle(), false, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                            //table.addCell(PrintUtils.createPdfPCell("" + notesPropres.get(a).getNote(), true, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                            //table.addCell(PrintUtils.createPdfPCell("" + notesPropres.get(a).getIdmatiere().getCoeficient(), true, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                            //table.addCell(PrintUtils.createPdfPCell("" + (notesPropres.get(a).getNote() * notesPropres.get(a).getIdmatiere().getCoeficient()), true, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                            //table.addCell(PrintUtils.createPdfPCell("" + notesPropres.get(a).getObservation(), true, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.ITALIC)));
                                            // }
                                        } else {
                                            if (Objects.equals(
                                                    matieres.get(a).getIdnaturematiere().getIdnaturematiere(),
                                                    nature.get(i).getIdnaturematiere())) {
                                                //decompte des coefficient et nombre point globaux
                                                //                                                    totalCoef += matieres.get(a).get
                                                totalPoint += 0;
                                                //decompte des coef et point par groupe                                          
                                                //                                                    totalCoefBloc += matieres.get(a).getCoeficient();
                                                totalPointBloc += 0;

                                                //                                                    table.addCell(PrintUtils.createPdfPCell("" + matieres.get(a).getIdtypematiere().getLibelle(), false, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                                table.addCell(PrintUtils.createPdfPCell("O", true, new Font(
                                                        Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                                //                                                    table.addCell(PrintUtils.createPdfPCell("" + matieres.get(a).getCoeficient(), true, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                                table.addCell(PrintUtils.createPdfPCell("0", true, new Font(
                                                        Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
                                                table.addCell(PrintUtils.createPdfPCell("Pas encore valu",
                                                        true, new Font(Font.FontFamily.TIMES_ROMAN, 10,
                                                                Font.ITALIC)));
                                            }
                                        }
                                    }

                                    //le texte Total pour chaque bloc
                                    PdfPCell cellTotalBlocText = new PdfPCell(
                                            new Paragraph("Total Matires " + nature.get(i).getLibelle() + " ",
                                                    new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL)));
                                    cellTotalBlocText.setColspan(2);
                                    table.addCell(cellTotalBlocText);

                                    table.addCell(PrintUtils.createPdfPCell("" + totalCoefBloc, true, new Font(
                                            Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.RED)));

                                    table.addCell(PrintUtils.createPdfPCell("" + totalPointBloc, 2, true,
                                            new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL,
                                                    BaseColor.RED)));

                                    table.addCell(PrintUtils.createPdfPCell(
                                            "Moyenne Matires  " + nature.get(i).getLibelle(), 3, false,
                                            PrintUtils.blueFont));
                                    if (totalCoefBloc != 0) {
                                        table.addCell(PrintUtils.createPdfPCell(
                                                "" + (totalPointBloc / totalCoefBloc) + "/20", 2, true,
                                                PrintUtils.redFont));
                                    } else {
                                        table.addCell(PrintUtils.createPdfPCell("......", 2, true,
                                                PrintUtils.redFont));
                                    }
                                    table.addCell(PrintUtils.createPdfPCell("  ", 5, false));
                                }
                            } else {

                            }
                            //on ecrit totaux
                            table.addCell(PrintUtils.createPdfPCell("Totaux  ", 2, false,
                                    new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLUE)));

                            //on met le total de coefficient
                            table.addCell(PrintUtils.createPdfPCell("" + totalCoef, true,
                                    new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.RED)));

                            //on met le total de point
                            table.addCell(PrintUtils.createPdfPCell("" + totalPoint, 2, true,
                                    new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.RED)));

                            table.addCell(PrintUtils.createPdfPCell(
                                    "Moyenne de la " + sequence.getIdsequence().getNom(), 3, false,
                                    new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLUE)));
                            if (totalCoef != 0) {
                                table.addCell(PrintUtils.createPdfPCell((totalPoint / totalCoef) + "/20", 2,
                                        true,
                                        new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED)));
                            }

                            bulletin.add(table);
                            bulletin.close();
                            JsfUtil.addSuccessMessage(
                                    "Opration russie,redirigez-vous dans le repertoire bulletin");
                        } catch (DocumentException ex) {
                            Logger.getLogger(BulletinCtrl.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(BulletinCtrl.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        /*} else {
                         JsfUtil.addErrorMessage("L'lve selectionn n a aucune note  la squence slectionne");
                         }*/
                    } else {
                        JsfUtil.addErrorMessage("Aucune nature de matiere parametre");
                    }
                } else {
                    JsfUtil.addErrorMessage("l'eleve n a pas de classe");
                }
            } else {
                JsfUtil.addErrorMessage("veuillez selectionner une squence !");
            }
        } else {
            JsfUtil.addErrorMessage("Veuillez selectionner un lve");
        }
    } else {
        String sc = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(sc + "/login.html");
        } catch (IOException ex) {
            Logger.getLogger(BulletinCtrl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:gestionEtat.imprimerBulletin.BulletinCtrl.java

@Override
public void imprimerBulletinPdf(int eleve, int sequence, Annee annee) {
    //on declare une bulletin qui est un document;
    Document bulletin = new Document(PageSize.A4);

    //on declare le nom du bulletin
    String bulletinName;/*from   w w w .jav a  2 s .c o  m*/

    //l eleve qui a le bulletin
    Eleve eleve1 = eleveFacade.findById(eleve);

    // la sequence  la quelle l'on veut imprimer le bulletin         
    Sequenceannee sequence1 = new Sequenceannee();

    Eleveanneeclasse anneeClasse = eleveAnneeClasseFacade.getEleveAnneeClaseByAnneClasse(eleve1.getIdeleve(),
            annee.getIdannee());
    if (anneeClasse != null) {
        bulletinName = eleve1.getNom() + "_" + eleve1.getPrenom() + "_" + sequence1.getIdsequence().getNom()
                + ".pdf";
        try {
            PdfWriter.getInstance(bulletin, new FileOutputStream(bulletinName));
            bulletin.open();
            bulletin.add(new Phrase("hello word"));

        } catch (Exception e) {
            e.getMessage();
        }
    } else {
        JsfUtil.addErrorMessage("L'lve selectionn n 'a aucune classe");
    }
    bulletin.close();
}

From source file:gov.utah.dts.det.ccl.actions.facility.information.license.LicensesPrintAction.java

License:Open Source License

@Action(value = "print-license-cert")
public void doPrintCertificate() {
    loadLicense();//from www  .jav  a  2 s .com

    try {
        ByteArrayOutputStream ba = LicenseCertificate.generate(license, request);
        if (ba != null && ba.size() > 0) {
            // This is where the response is set
            String filename = "license_certificate.pdf";
            response.setContentLength(ba.size());
            response.setContentType("application/pdf");
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");

            ServletOutputStream out = response.getOutputStream();
            ba.writeTo(out);
            out.flush();
        }
    } catch (Exception ex) {
        // Generate an error pdf
        ByteArrayOutputStream ba = null;
        try {
            ba = new ByteArrayOutputStream();
            Document document = new Document(PageSize.A4, 50, 50, 100, 100);
            @SuppressWarnings("unused")
            PdfWriter writer = PdfWriter.getInstance(document, ba);
            document.open();
            document.add(new Paragraph("An error occurred while generating the letter document.",
                    FontFactory.getFont("Times-Roman", 12, Font.NORMAL)));
            document.close();
            if (ba != null && ba.size() > 0) {
                // This is where the response is set
                // String filename = getTrackingRecordScreening().getPerson().getFirstAndLastName() + " - " +
                // screeningLetter.getLetterType() + ".pdf";
                response.setContentLength(ba.size());
                response.setContentType("application/pdf");
                response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
                // response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
                response.setHeader("Content-disposition", "attachment");

                ServletOutputStream out = response.getOutputStream();
                ba.writeTo(out);
                out.flush();
            }

        } catch (Exception e) {
            log.error("AN ERROR OCCURRED GENERATING ERROR PDF DOCUMENT: " + e);
        }
    }
}

From source file:gov.utah.dts.det.ccl.actions.facility.information.license.LicensesPrintAction.java

License:Open Source License

@Action(value = "print-license-letter")
public void doPrintLetter() {
    loadLicense();//from  w ww .ja  va  2  s. c  om

    try {
        ByteArrayOutputStream ba = LicenseLetter.generate(license, request);
        if (ba != null && ba.size() > 0) {
            // This is where the response is set
            String filename = "license_letter.pdf";
            response.setContentLength(ba.size());
            response.setContentType("application/pdf");
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");

            ServletOutputStream out = response.getOutputStream();
            ba.writeTo(out);
            out.flush();
        }
    } catch (Exception ex) {
        // Generate an error pdf
        ByteArrayOutputStream ba = null;
        try {
            ba = new ByteArrayOutputStream();
            Document document = new Document(PageSize.A4, 50, 50, 100, 100);
            @SuppressWarnings("unused")
            PdfWriter writer = PdfWriter.getInstance(document, ba);
            document.open();
            document.add(new Paragraph("An error occurred while generating the letter document.",
                    FontFactory.getFont("Times-Roman", 12, Font.NORMAL)));
            document.close();
            if (ba != null && ba.size() > 0) {
                // This is where the response is set
                // String filename = getTrackingRecordScreening().getPerson().getFirstAndLastName() + " - " +
                // screeningLetter.getLetterType() + ".pdf";
                response.setContentLength(ba.size());
                response.setContentType("application/pdf");
                response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
                // response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
                response.setHeader("Content-disposition", "attachment");

                ServletOutputStream out = response.getOutputStream();
                ba.writeTo(out);
                out.flush();
            }

        } catch (Exception e) {
            log.error("AN ERROR OCCURRED GENERATING ERROR PDF DOCUMENT: " + e);
        }
    }
}

From source file:gov.utah.dts.det.ccl.actions.reports.generators.ExpiredLicensesReport.java

private static void writePdf(Person specialist, List<FacilityLicenseView> licenses, OutputStream ba)
        throws DocumentException, BadElementException {
    Document document = null;/* w ww . ja v a  2  s .co m*/
    PdfPTable doctable;
    PdfPTable datatable;
    PdfPTable factable;
    Paragraph paragraph;
    Facility facility;
    document = new Document(PageSize.A4);
    @SuppressWarnings("unused")
    PdfWriter writer = PdfWriter.getInstance(document, ba);

    document.open();

    doctable = getDocumentTable(specialist);

    if (licenses != null && licenses.size() > 0) {
        doctable.getDefaultCell().setPaddingBottom(4);
        doctable.getDefaultCell().setBorderWidthBottom(.5f);
        for (FacilityLicenseView license : licenses) {
            facility = license.getFacility();
            datatable = getDetailTable();
            datatable.getDefaultCell().setPaddingLeft(4);
            datatable.getDefaultCell().setPaddingRight(2);
            datatable.getDefaultCell().setColspan(1);
            // Add Facility Name information
            if (StringUtils.isNotBlank(license.getFacilityName())) {
                datatable.addCell(new Phrase(license.getFacilityName().toUpperCase(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add License Number information
            datatable.getDefaultCell().setPaddingLeft(2);
            if (license.getLicenseNumber() != null) {
                datatable.addCell(new Phrase(license.getLicenseNumber().toString(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add Service Code information
            if (StringUtils.isNotBlank(license.getServiceCodeCode())) {
                datatable.addCell(new Phrase(license.getServiceCodeCode(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add License Type information
            if (StringUtils.isNotBlank(license.getSubtype())) {
                datatable.addCell(new Phrase(license.getSubtype(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add License Expiration date information
            datatable.getDefaultCell().setPaddingRight(4);
            if (StringUtils.isNotBlank(license.getExpirationDateFormatted())) {
                datatable.addCell(new Phrase(license.getExpirationDateFormatted(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }

            // Add Mailing Location information
            sb = new StringBuilder();
            if (facility != null && facility.getMailingAddress() != null
                    && StringUtils.isNotBlank(facility.getMailingAddress().getAddressOne())) {
                sb.append(facility.getMailingAddress().getAddressOne());

                // add address two
                if (StringUtils.isNotBlank(facility.getMailingAddress().getAddressTwo())) {
                    if (sb.length() > 0) {
                        sb.append(" ");
                    }
                    sb.append(facility.getMailingAddress().getAddressTwo());
                }

                // Add city, state, zip
                if (StringUtils.isNotBlank(facility.getMailingAddress().getCityStateZip())) {
                    if (sb.length() > 0) {
                        sb.append(", ");
                    }
                    sb.append(facility.getMailingAddress().getCityStateZip());
                }
            }
            datatable.getDefaultCell().setColspan(5);
            datatable.getDefaultCell().setPaddingLeft(4);
            datatable.getDefaultCell().setPaddingRight(4);
            if (sb.length() > 0) {
                datatable.addCell(new Phrase(sb.toString().toUpperCase(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }

            //
            // Add Facility details information
            // 
            factable = getFacilityDetailsTable();

            // Add primary phone information
            paragraph = new Paragraph(fixedLeading);
            paragraph.add(new Phrase("Primary Phone: ", smallfontB));
            if (facility != null && facility.getPrimaryPhone() != null
                    && StringUtils.isNotBlank(facility.getPrimaryPhone().getFormattedPhoneNumber())) {
                paragraph.add(new Phrase(facility.getPrimaryPhone().getFormattedPhoneNumber(), smallfont));
            }
            factable.getDefaultCell().setPaddingLeft(4);
            factable.getDefaultCell().setPaddingRight(2);
            factable.addCell(paragraph);

            // Add SAFE Provider ID number
            paragraph = new Paragraph(fixedLeading);
            paragraph.add(new Phrase("SAFE: ", smallfontB));
            if (facility != null && facility.getSafeProviderId() != null) {
                paragraph.add(new Phrase(facility.getSafeProviderId().toString(), smallfontB));
            }
            factable.getDefaultCell().setColspan(2);
            factable.addCell(paragraph);

            // Add the facility information table to the details table
            datatable.getDefaultCell().setPadding(0);
            datatable.addCell(factable);
            // Add the detail to the document table
            doctable.addCell(datatable);
        }
    } else {
        // No open applications
        datatable = getDetailTable();
        datatable.getDefaultCell().setPaddingTop(10);
        datatable.getDefaultCell().setColspan(3);
        datatable.getDefaultCell().setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
        datatable.addCell(new Phrase("No expired licenses found to display.", smallfont));
        doctable.getDefaultCell().setPaddingBottom(0);
        doctable.getDefaultCell().setBorderWidthBottom(0);
        doctable.addCell(datatable);
    }

    // Add the document table to the document
    document.add(doctable);

    document.close();
}

From source file:gov.utah.dts.det.ccl.actions.reports.generators.FacilityLicenseSummaryReport.java

private static void writePdf(Person specialist, Date endDate, FacilityLicenseSummarySortBy sortBy,
        List<FacilityLicenseView> licenses, OutputStream ba) throws DocumentException, BadElementException {
    Document document = null;//from ww  w  .jav a 2s  . c  o m
    PdfPTable doctable;
    PdfPTable datatable;
    PdfPTable factable;
    Paragraph paragraph;
    Facility facility;
    document = new Document(PageSize.A4);
    @SuppressWarnings("unused")
    PdfWriter writer = PdfWriter.getInstance(document, ba);

    document.open();

    doctable = getDocumentTable(specialist, endDate, sortBy);

    if (licenses != null && licenses.size() > 0) {
        doctable.getDefaultCell().setPaddingBottom(4);
        doctable.getDefaultCell().setBorderWidthBottom(.5f);
        for (FacilityLicenseView license : licenses) {
            facility = license.getFacility();
            datatable = getDetailTable();
            datatable.getDefaultCell().setPaddingLeft(4);
            datatable.getDefaultCell().setPaddingRight(2);
            datatable.getDefaultCell().setColspan(1);
            // Add Facility Name information
            if (StringUtils.isNotBlank(license.getFacilityName())) {
                datatable.addCell(new Phrase(license.getFacilityName(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add License Number information
            datatable.getDefaultCell().setPaddingLeft(2);
            if (license.getLicenseNumber() != null) {
                datatable.addCell(new Phrase(license.getLicenseNumber().toString(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add Service Code information
            if (StringUtils.isNotBlank(license.getServiceCodeCode())) {
                datatable.addCell(new Phrase(license.getServiceCodeCode(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add License Type information
            if (StringUtils.isNotBlank(license.getSubtype())) {
                datatable.addCell(new Phrase(license.getSubtype(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }
            // Add License Expiration date information
            datatable.getDefaultCell().setPaddingRight(4);
            if (StringUtils.isNotBlank(license.getExpirationDateFormatted())) {
                datatable.addCell(new Phrase(license.getExpirationDateFormatted(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }

            // Add Mailing Location information
            sb = new StringBuilder();
            if (facility != null && facility.getMailingAddress() != null
                    && StringUtils.isNotBlank(facility.getMailingAddress().getAddressOne())) {
                sb.append(facility.getMailingAddress().getAddressOne());

                // add address two
                if (StringUtils.isNotBlank(facility.getMailingAddress().getAddressTwo())) {
                    if (sb.length() > 0) {
                        sb.append(" ");
                    }
                    sb.append(facility.getMailingAddress().getAddressTwo());
                }

                // Add city, state, zip
                if (StringUtils.isNotBlank(facility.getMailingAddress().getCityStateZip())) {
                    if (sb.length() > 0) {
                        sb.append(", ");
                    }
                    sb.append(facility.getMailingAddress().getCityStateZip());
                }
            }
            datatable.getDefaultCell().setColspan(5);
            datatable.getDefaultCell().setPaddingLeft(4);
            datatable.getDefaultCell().setPaddingRight(4);
            if (sb.length() > 0) {
                datatable.addCell(new Phrase(sb.toString().toUpperCase(), smallfont));
            } else {
                datatable.addCell(BLANK);
            }

            //
            // Add Facility details information
            // 
            factable = getFacilityDetailsTable();

            // Add primary phone information
            paragraph = new Paragraph(fixedLeading);
            paragraph.add(new Phrase("Primary Phone: ", smallfontB));
            if (facility != null && facility.getPrimaryPhone() != null
                    && StringUtils.isNotBlank(facility.getPrimaryPhone().getFormattedPhoneNumber())) {
                paragraph.add(new Phrase(facility.getPrimaryPhone().getFormattedPhoneNumber(), smallfont));
            }
            factable.getDefaultCell().setPaddingLeft(4);
            factable.getDefaultCell().setPaddingRight(2);
            factable.addCell(paragraph);

            // Add SAFE Provider ID number
            paragraph = new Paragraph(fixedLeading);
            paragraph.add(new Phrase("SAFE: ", smallfontB));
            if (facility != null && facility.getSafeProviderId() != null) {
                paragraph.add(new Phrase(facility.getSafeProviderId().toString(), smallfontB));
            }
            factable.getDefaultCell().setColspan(2);
            factable.addCell(paragraph);

            // Add the facility information table to the details table
            datatable.getDefaultCell().setPadding(0);
            datatable.addCell(factable);
            // Add the detail to the document table
            doctable.addCell(datatable);
        }
    } else {
        // No open applications
        datatable = getDetailTable();
        datatable.getDefaultCell().setPaddingTop(10);
        datatable.getDefaultCell().setColspan(3);
        datatable.getDefaultCell().setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
        datatable.addCell(new Phrase("No expired licenses found to display.", smallfont));
        doctable.getDefaultCell().setPaddingBottom(0);
        doctable.getDefaultCell().setBorderWidthBottom(0);
        doctable.addCell(datatable);
    }

    // Add the document table to the document
    document.add(doctable);

    document.close();
}