Example usage for com.itextpdf.text Document Document

List of usage examples for com.itextpdf.text Document Document

Introduction

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

Prototype


public Document(Rectangle pageSize) 

Source Link

Document

Constructs a new Document -object.

Usage

From source file:architecture.ee.web.view.document.AbstractPdfView.java

License:Apache License

/**
 * Create a new document to hold the PDF contents.
 * <p>By default returns an A4 document, but the subclass can specify any
 * Document, possibly parameterized via bean properties defined on the View.
 * @return the newly created iText Document instance
 * @see com.lowagie.text.Document#Document(com.lowagie.text.Rectangle)
 *//*from   w ww  .  j  ava2 s  .  co  m*/
protected Document newDocument() {
    return new Document(PageSize.A4);
}

From source file:automatedbillingsoftware.helper.HtmlToPdf.java

public void createPdf(String file, HashMap<String, Object> scopes) throws IOException, DocumentException {
    // step 1//from   w  w  w  .  ja v  a2 s . c  o  m
    Document document = new Document(PageSize.A4);

    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    writer.setTagged();
    // step 3
    document.open();

    // step 4
    //        String readHtml = readHtml(HTML);
    System.out.println("challan" + DEST.contains("Challan-"));
    String readHtml = DEST.contains("Challan-") ? getDefaultTempProperties().getProperty("challan")
            : getDefaultTempProperties().getProperty("invoice");
    System.out.println("readHtml=>" + readHtml);
    String testTemplate = getTestTemplate(readHtml, scopes);
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, new StringReader(testTemplate));

    //  XMLWorkerHelper.getInstance().parseXHtml(writer, );
    // step 5
    document.close();
}

From source file:be.rheynaerde.poolsheets.AbstractPoolSheet.java

License:Open Source License

protected void buildSheet() throws DocumentException {
    Rectangle pageSize = configuration.isLandscape(AbstractPoolSheetConfiguration.TITLE_PAGE)
            ? PageSize.A4.rotate()//w w  w.j  ava2  s . co  m
            : PageSize.A4;
    Document document = new Document(pageSize);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);

    document.open();
    document.addTitle(configuration.getTitle());
    document.addCreator(CREATOR);

    buildTitle(document);
    buildTable(document);

    if (configuration.includeOrderOfBouts()) {
        if (configuration.putBoutOrderOnNewPage()) {
            pageSize = configuration.isLandscape(AbstractPoolSheetConfiguration.BOUT_ORDER_PAGE)
                    ? PageSize.A4.rotate()
                    : PageSize.A4;
            document.setPageSize(pageSize);
            document.newPage();
        }

        buildBoutOrder(document);
    }

    document.close();
    sheet = baos.toByteArray();
}

From source file:bemyguest.controller.ValidationResevation.java

@FXML
private void handleButtonValiderAction(ActionEvent event) throws FileNotFoundException, DocumentException {
    Resrevation e = tab_reservation.getSelectionModel().getSelectedItem();
    if (e == null) {

        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Warning Dialog");
        alert.setHeaderText(null);/*from w  w w. jav a 2 s .c om*/
        alert.setContentText("selectionner un demande de  reservation a traiter svp!");

        alert.showAndWait();
        LoadData();
        setCellTable();
    }

    else {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Confiramtion");
        alert.setHeaderText(null);
        alert.setContentText("vous etes sur d'accepter cette demande de reservation");
        Optional<ButtonType> answer = alert.showAndWait();

        if (answer.get() == ButtonType.OK) {
            ResevationDAO dao = new ResevationDAO();
            if (dao.ajouter_Reservation(e)) {

                LoadData();
                setCellTable();

                alert.setTitle("Success Dialog");
                alert.setHeaderText(null);
                alert.setContentText("Demandes accepter avec success");

                alert.showAndWait();

                try {

                    Document d = new Document(PageSize.A4.rotate());
                    PdfWriter.getInstance(d, new FileOutputStream(e.getUser().getNom() + "Facture.pdf"));
                    d.open();

                    d.add(new Paragraph("BeMyGuest Facture :",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.RED)));
                    d.add(new Paragraph(new Date().toString()));
                    d.add(new Paragraph(
                            "-------------------------------------------------------------------------------------------------------------"));
                    d.add(new Paragraph("             "));
                    d.add(new Paragraph("             "));
                    PdfPTable pdt = new PdfPTable(7);
                    PdfPCell cell = new PdfPCell(new Paragraph("Composant de facture"));
                    cell.setColspan(7);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setBackgroundColor(BaseColor.RED);
                    pdt.addCell(cell);

                    pdt.addCell(new Paragraph("Nom:",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph("Prenom:",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph("Date Debut:",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph("Date Fin:",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph("nbre chambre:",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph("nbre personne:",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph("Prix:",
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph(e.getUserDemandant().getNom(),
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph(e.getUserDemandant().getPrenom(),
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph(e.getDateDebut().toString(),
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph(e.getDateFin().toString(),
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(e.getDateDebut().toString());

                    int nb = e.getPropriete().getNbrChambre();

                    float pr = e.getPropriete().getPrix();
                    int nbp = e.getPropriete().getNbrVoyageur();
                    pdt.addCell(new Paragraph(Integer.toString(nb),
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph(Integer.toString(nbp),
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));
                    pdt.addCell(new Paragraph(Float.toString(pr),
                            FontFactory.getFont(FontFactory.TIMES_BOLD, 8, Font.BOLD, BaseColor.BLACK)));

                    pdt.addCell(Float.toString(pr));

                    d.add(pdt);
                    d.close();
                } catch (Exception b) {

                    JOptionPane.showMessageDialog(null, b);
                }
            } else {
                alert.setTitle("Error Dialog");
                alert.setHeaderText(null);
                alert.setContentText("Cette Propriete Reserver pendant cette date ");

                alert.showAndWait();

                LoadData();
                setCellTable();
            }
        }

        else {
            LoadData();
            setCellTable();
        }
    }

}

From source file:biblioteca.BibliotecaFXMLController.java

@FXML /// Arquivo - PDF - Tabela Alunos
public void pdf() throws FileNotFoundException, DocumentException, IOException, SQLException {
    Document doc = null;//from   w w w  . j  a v a 2  s  .c  o m
    OutputStream os = null;
    try {
        contest conn = new contest();
        //cria o documento tamanho A4, margens de 2,54cm
        doc = new Document(PageSize.A4);
        //cria a stream de sada
        os = new FileOutputStream("TabelaA.pdf");
        //associa a stream de sada ao
        PdfWriter.getInstance(doc, os);
        //abre o documento
        doc.open();
        //adiciona o texto ao PDF 
        PdfPTable tabelaA = new PdfPTable(new float[] { 0.2f, 0.7f, 0.7f, 0.9f, 0.7f });
        PdfPCell header = new PdfPCell(new Paragraph("Tabela de Alunos"));
        header.setColspan(5);
        tabelaA.addCell(header);
        tabelaA.addCell("ID");
        tabelaA.addCell("Matrcula");
        tabelaA.addCell("Nome");
        tabelaA.addCell("C.P.F");
        tabelaA.addCell("Telefone");
        Statement stA = conn.conectar1().createStatement();
        ResultSet rsa = stA.executeQuery("SELECT idAluno,matriculaAluno,nomeAluno,nCPF,Telefone FROM `Aluno`;");
        while (rsa.next()) {
            Aluno a = new Aluno(0, 0, "", "", "");
            a.setIdaluno(rsa.getInt("idAluno"));
            a.setMatricula(rsa.getInt("matriculaAluno"));
            a.setNoAluno(rsa.getString("nomeAluno"));
            a.setCpf(rsa.getString("nCPF"));
            a.setTelefone(rsa.getString("Telefone"));

            tabelaA.addCell(String.valueOf(a.getIdaluno()));
            tabelaA.addCell(String.valueOf(a.getMatricula()));
            tabelaA.addCell(a.getNoAluno());
            tabelaA.addCell(a.getCpf());
            tabelaA.addCell(a.getTelefone());

        }
        doc.add(tabelaA);
    } finally {
        if (doc != null) {
            //fechamento do documento
            doc.close();
        }
        if (os != null) {
            //fechamento da stream de sada
            os.close();
        }
    }
}

From source file:biblioteca.BibliotecaFXMLController.java

@FXML /// Arquivo - PDF - Tabela Livros
public void pdfL() throws FileNotFoundException, DocumentException, IOException, SQLException {
    Document doc = null;//from w  ww .  ja  va2  s .  c om
    OutputStream os = null;
    try {
        contest conn = new contest();
        //cria o documento tamanho A4, margens de 2,54cm
        doc = new Document(PageSize.A4);
        //cria a stream de sada
        os = new FileOutputStream("TabelaL.pdf");
        //associa a stream de sada ao
        PdfWriter.getInstance(doc, os);
        //abre o documento
        doc.open();
        //adiciona o texto ao PDF
        PdfPTable tabelaL = new PdfPTable(new float[] { 0.1f, 0.7f, 0.7f, 0.1f });
        PdfPCell header = new PdfPCell(new Paragraph("Tabela de Livros"));
        header.setColspan(4);
        tabelaL.addCell(header);
        tabelaL.addCell("ID");
        tabelaL.addCell("Ttulo");
        tabelaL.addCell("Autor");
        tabelaL.addCell("Qtde.");

        Statement st = conn.conectar1().createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM `Livro`;");
        while (rs.next()) {
            Livro l = new Livro(0, "", "", 0, 0);
            l.setIdlivro(rs.getInt("idlivro"));
            l.setTiLivro(rs.getString("nomeLivro"));
            l.setNoAutor(rs.getString("nomeAutor"));
            l.setQtdeLivro(rs.getInt("qtdelivro"));

            tabelaL.addCell(String.valueOf(l.getIdlivro()));
            tabelaL.addCell(l.getTiLivro());
            tabelaL.addCell(l.getNoAutor());
            tabelaL.addCell(String.valueOf(l.getQtdeLivro()));
        }
        doc.add(tabelaL);
    } finally {
        if (doc != null) {
            //fechamento do documento
            doc.close();
        }
        if (os != null) {
            //fechamento da stream de sada
            os.close();
        }
    }
}

From source file:biblioteca.BibliotecaFXMLController.java

@FXML /// Arquivo - PDF - Tabela Reservas
public void pdfR() throws FileNotFoundException, DocumentException, IOException, SQLException {
    Document doc = null;//from w w  w. ja  v a2  s .  c o  m
    OutputStream os = null;
    try {
        contest conn = new contest();
        //cria o documento tamanho A4, margens de 2,54cm
        doc = new Document(PageSize.A4);
        //cria a stream de sada
        os = new FileOutputStream("Tabela de Reservas.pdf");
        //associa a stream de sada ao
        PdfWriter.getInstance(doc, os);
        //abre o documento
        doc.open();
        //adiciona o texto ao PDF
        PdfPTable tabelaR = new PdfPTable(new float[] { 0.2f, 0.7f, 0.7f, 0.7f, 0.7f });
        PdfPCell header = new PdfPCell(new Paragraph("Tabela de Reservas"));
        header.setColspan(5);
        tabelaR.addCell(header);
        tabelaR.addCell("ID");
        tabelaR.addCell("Ttulo do Livro");
        tabelaR.addCell("Aluno");
        tabelaR.addCell("Data de Saida");
        tabelaR.addCell("Data de Entrega");

        Statement stR = conn.conectar1().createStatement();
        ResultSet rsr = stR.executeQuery(
                "SELECT Itens.id_Itens as \"Item\", Itens.id_reserva AS \"Reserva\", Aluno.nomeAluno "
                        + "as \"Aluno\", Itens.id_Livro as \"idLivro\", Livro.nomeLivro AS \"NomeLivro\", Reserva.dataRetirada AS \"DataSaida\", Reserva.dataEntrega as \"DataEntrega\""
                        + "FROM Itens JOIN Reserva ON id_reserva = Reserva.idReserva\n"
                        + "JOIN Aluno ON Reserva.idAluno = Aluno.idAluno\n"
                        + "JOIN Livro ON Livro.idLivro = Itens.id_Livro;");
        while (rsr.next()) {
            Reserva r = new Reserva(0, 0, "", 0, "", "", "");

            r.setIdItem(rsr.getInt("Item"));
            r.setIdReserva(rsr.getInt("Reserva"));
            r.setNoAluno(rsr.getString("Aluno"));
            r.setIdLivro(rsr.getInt("idLivro"));
            r.setNoLivro(rsr.getString("NomeLivro"));
            r.setDaSaida(rsr.getString("DataSaida"));
            r.setDaEntrada(rsr.getString("DataEntrega"));

            tabelaR.addCell(String.valueOf(r.getIdReserva()));
            tabelaR.addCell(r.getNoLivro());
            tabelaR.addCell(r.getNoAluno());
            tabelaR.addCell(r.getDaSaida());
            tabelaR.addCell(r.getDaEntrada());

        }
        doc.add(tabelaR);
    } finally {
        if (doc != null) {
            //fechamento do documento
            doc.close();
        }
        if (os != null) {
            //fechamento da stream de sada
            os.close();
        }
    }
}

From source file:bouttime.report.boutsheet.BoutSheetReport.java

License:Open Source License

/**
 * Generate a bout sheet report that has no data in it (only the image).
 * The length is the given number of pages.
 * //from ww  w.j a v  a  2s.  c  o  m
 * @param numPages
 * @return True if the report was generated.
 */
public boolean generateBlank(Dao dao, Integer numPages) {
    // step 1: creation of a document-object
    // rotate to make page landscape
    Document document = new Document(PageSize.A4.rotate());

    try {

        // step 2: creation of the writer
        FileOutputStream fos = createOutputFile();
        if (fos == null) {
            return false;
        }
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        writer.setPageEvent(this);

        // step 3: we open the document
        document.open();

        // step 4: we grab the ContentByte and do some stuff with it
        PdfContentByte cb = writer.getDirectContent();

        BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
        float pageWidth = cb.getPdfDocument().getPageSize().getWidth();
        float midPage = pageWidth / 2;

        setHeaderString(dao);

        int count = 1;
        while (true) {
            drawBout(cb, bf, 35, midPage - 35, null);
            drawBout(cb, bf, midPage + 35, pageWidth - 35, null);

            if (++count > numPages) {
                break;
            }

            document.newPage();
        }

    } catch (DocumentException de) {
        logger.error("Document Exception", de);
        return false;
    } catch (IOException ioe) {
        logger.error("IO Exception", ioe);
        return false;
    }

    // step 5: we close the document
    document.close();

    return true;
}

From source file:bouttime.report.boutsheet.BoutSheetReport.java

License:Open Source License

/**
 * Generate a bout sheet report for the given list of bouts.
 * It is assumed that the list is in the desired order (no sorting is done here).
 *
 * @param list//w w  w  .  jav  a 2  s .co m
 * @return True if the report was generated.
 */
public boolean generateReport(Dao dao, List<Bout> list) {

    // step 1: creation of a document-object
    // rotate to make page landscape
    Document document = new Document(PageSize.A4.rotate());

    try {

        // step 2: creation of the writer
        FileOutputStream fos = createOutputFile();
        if (fos == null) {
            return false;
        }
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        writer.setPageEvent(this);

        // step 3: we open the document
        document.open();

        // step 4: we grab the ContentByte and do some stuff with it
        PdfContentByte cb = writer.getDirectContent();

        BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
        float pageWidth = cb.getPdfDocument().getPageSize().getWidth();
        float midPage = pageWidth / 2;

        setHeaderString(dao);

        int count = 0;
        for (Bout b : list) {
            boolean rightSide = false;
            if ((count++ % 2) == 0) {
                if (count > 2) {
                    // We could put this after Bout 2 is added, but
                    // that could leave a blank last page.
                    document.newPage();
                }

                // Bout 1 (Left side)
                drawBout(cb, bf, 35, midPage - 35, b);
            } else {
                // Bout 2 (Right side)
                drawBout(cb, bf, midPage + 35, pageWidth - 35, b);
                rightSide = true;
            }

            // Print the watermark, if necessary
            boolean doWatermark = false;
            String gClass = b.getGroup().getClassification();
            String wmValues = dao.getBoutsheetWatermarkValues();
            if ((wmValues != null) && !wmValues.isEmpty()) {
                String[] tokens = wmValues.split(",");
                for (String s : tokens) {
                    if (s.trim().equalsIgnoreCase(gClass)) {
                        doWatermark = true;
                        break;
                    }
                }
            }
            if (doWatermark) {
                int rotation = 45;
                PdfContentByte ucb = writer.getDirectContentUnder();
                BaseFont helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
                ucb.saveState();
                ucb.setColorFill(BaseColor.LIGHT_GRAY);
                ucb.beginText();
                ucb.setFontAndSize(helv, 86);
                float centerWidth = document.getPageSize().getWidth() / 4;
                if (rightSide) {
                    centerWidth = centerWidth * 3;
                }
                ucb.showTextAligned(Element.ALIGN_CENTER, gClass, centerWidth,
                        document.getPageSize().getHeight() / 2, rotation);
                ucb.endText();
                ucb.restoreState();
            }
        }

    } catch (DocumentException de) {
        logger.error("Document Exception", de);
        return false;
    } catch (IOException ioe) {
        logger.error("IO Exception", ioe);
        return false;
    }

    // step 5: we close the document
    document.close();

    return true;
}

From source file:bouttime.utility.boutsheet.BoutSheetMaker.java

License:Open Source License

/**
 * @param args the command line arguments
 *//*from   w  w  w  . j  a va2 s.  c om*/
public static void main(String[] args) {
    // step 1: creation of a document-object
    // rotate to make page landscape
    Document document = new Document(PageSize.A4.rotate());

    try {

        // step 2: creation of the writer
        PdfWriter writer;
        if (args.length >= 1) {
            writer = PdfWriter.getInstance(document, new FileOutputStream(args[0]));
        } else {
            System.err.println("ERROR : Must specify output file.");
            return;
        }

        // step 3: we open the document
        document.open();

        // step 4: we grab the ContentByte and do some stuff with it
        PdfContentByte cb = writer.getDirectContent();

        BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
        float pageWidth = cb.getPdfDocument().getPageSize().getWidth();
        float midPage = pageWidth / 2;

        drawBout(cb, bf, 35, midPage - 35);
        drawBout(cb, bf, midPage + 35, pageWidth - 35);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
        return;
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
        return;
    }

    // step 5: we close the document
    document.close();

    if ((args.length == 3) && (Boolean.parseBoolean(args[2]))) {
        showPDF(args[0]);
    }
}