Search in sources :

Example 11 with Document

use of com.itextpdf.text.Document in project tutorials by eugenp.

the class PDF2HTMLExample method generatePDFFromHTML.

private static void generatePDFFromHTML(String filename) throws ParserConfigurationException, IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("src/output/html.pdf"));
    document.open();
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(filename));
    document.close();
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) Document(com.itextpdf.text.Document) FileInputStream(java.io.FileInputStream)

Example 12 with Document

use of com.itextpdf.text.Document in project tutorials by eugenp.

the class PDF2ImageExample method generatePDFFromImage.

private static void generatePDFFromImage(String filename, String extension) throws IOException, BadElementException, DocumentException {
    Document document = new Document();
    String input = filename + "." + extension;
    String output = "src/output/" + extension + ".pdf";
    FileOutputStream fos = new FileOutputStream(output);
    PdfWriter writer = PdfWriter.getInstance(document, fos);
    writer.open();
    document.open();
    document.add(Image.getInstance((new URL(input))));
    document.close();
    writer.close();
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) Document(com.itextpdf.text.Document) URL(java.net.URL)

Example 13 with Document

use of com.itextpdf.text.Document in project clusterMaker2 by RBVI.

the class GraphicsExportPanel method pdfSave.

private void pdfSave(String format) {
    com.itextpdf.text.Rectangle pageSize = PageSize.LETTER;
    Document document = new Document(pageSize);
    try {
        OutputStream output = new BufferedOutputStream(new FileOutputStream(getFile()));
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Graphics2D g = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight(), new DefaultFontMapper());
        double imageScale = Math.min(pageSize.getWidth() / ((double) estimateWidth() + getBorderPixels()), pageSize.getHeight() / ((double) estimateHeight() + getBorderPixels()));
        g.scale(imageScale, imageScale);
        drawAll(g, 1.0);
        g.dispose();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, new JTextArea("Dendrogram export had problem " + e));
    // logger.error("Exception " + e);
    // e.printStackTrace();
    }
    document.close();
}
Also used : JTextArea(javax.swing.JTextArea) PdfWriter(com.itextpdf.text.pdf.PdfWriter) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) Document(com.itextpdf.text.Document) SVGGraphics2D(org.freehep.graphicsio.svg.SVGGraphics2D) Graphics2D(java.awt.Graphics2D) PSGraphics2D(org.freehep.graphicsio.ps.PSGraphics2D) FileOutputStream(java.io.FileOutputStream) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) BufferedOutputStream(java.io.BufferedOutputStream)

Example 14 with Document

use of com.itextpdf.text.Document in project specify by rhebecaabreu.

the class CartaoEstoria method criaPDF.

public void criaPDF(JTable table_1, java.util.List<Integer> codigosReq) throws FileNotFoundException, DocumentException {
    Document document = new Document();
    try {
        document.setPageSize(PageSize.A4);
        document.setMargins(50, 50, 50, 50);
        nomepdf = MeusProjetos.getNome_pdf() + ".pdf";
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(nomepdf));
        ModelPDFRodape mf = new ModelPDFRodape();
        mf.setTopo("Specify");
        writer.setPageEvent(mf);
        document.open();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        String data = dateFormat.format(date);
        RequisitoDAO rdao = new RequisitoDAO();
        Font f = FontFactory.getFont(FontFactory.HELVETICA, 13, Font.NORMAL);
        Font ff = FontFactory.getFont(FontFactory.HELVETICA, 13, Font.BOLD);
        Paragraph title2 = new Paragraph("Cartões de Estória", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD));
        title2.setAlignment(Element.ALIGN_CENTER);
        Chapter chapter2 = new Chapter(title2, 2);
        chapter2.setNumberDepth(0);
        ProjetoDAO pread = new ProjetoDAO();
        for (Requisito r : rdao.readOrderBy()) {
            for (Projeto p : pread.read()) {
                for (int i = 0; i < codigosReq.size(); i++) {
                    if (r.getProjcodigo() == p.getCodigo() && r.getCodigo() == codigosReq.get(0)) {
                        Paragraph title_project2 = new Paragraph("Projeto: " + p.getNome(), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD));
                        title_project2.setAlignment(Element.ALIGN_CENTER);
                        chapter2.add(title_project2);
                        break;
                    }
                }
            }
        }
        Paragraph data_hora2 = new Paragraph("Gerado em " + data, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL));
        data_hora2.setAlignment(Element.ALIGN_CENTER);
        chapter2.add(data_hora2);
        Paragraph listUserStories = new Paragraph("Lista de Cartões de Estória", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL));
        Section section2 = chapter2.addSection(listUserStories);
        for (Requisito r : rdao.readOrderBy()) {
            if (table_1.getSelectedRow() != -1) {
                for (int j = 0; j < (codigosReq.size()); j++) {
                    if (r.getCodigo() == codigosReq.get(j)) {
                        Paragraph p = new Paragraph();
                        Paragraph q = new Paragraph();
                        Paragraph rr = new Paragraph();
                        Paragraph s = new Paragraph();
                        Paragraph t = new Paragraph();
                        p.setFont(ff);
                        p.add("Identificador: ");
                        p.setFont(f);
                        p.add(r.getId());
                        p.setFont(ff);
                        p.add(" Código: ");
                        p.setFont(f);
                        p.add(String.valueOf(r.getCodigo()));
                        q.setFont(ff);
                        q.add("Como um " + r.getSujeito() + " eu quero " + r.getAcaoRestricao() + " a fim de " + r.getValorRazao());
                        Paragraph qq = new Paragraph();
                        qq.setFont(ff);
                        qq.add("Estimativa: ");
                        qq.setFont(f);
                        qq.add(r.getEstimativa() + " hora(s)");
                        rr.setFont(ff);
                        rr.add("Grau Prioridade: ");
                        rr.setFont(f);
                        rr.add(r.getGrauPrioridade().toString());
                        s.setFont(ff);
                        s.add("Grau Dificuldade: ");
                        s.setFont(f);
                        s.add(r.getGrauDificuldade().toString());
                        t = new Paragraph(" ");
                        section2.add(p);
                        section2.add(q);
                        section2.add(rr);
                        section2.add(s);
                        section2.add(t);
                    }
                }
            }
        }
        document.add(chapter2);
        document.add(Chunk.NEXTPAGE);
    } catch (DocumentException ex) {
        System.out.println("Error:" + ex);
    } catch (FileNotFoundException ex) {
        System.out.println("Error:" + ex);
    } finally {
        document.close();
    }
    try {
        Desktop.getDesktop().open(new File(nomepdf));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) Requisito(model.bean.Requisito) ModelPDFRodape(model.bean.ModelPDFRodape) Chapter(com.itextpdf.text.Chapter) FileNotFoundException(java.io.FileNotFoundException) Projeto(model.bean.Projeto) IOException(java.io.IOException) Document(com.itextpdf.text.Document) Section(com.itextpdf.text.Section) Date(java.util.Date) Font(com.itextpdf.text.Font) Paragraph(com.itextpdf.text.Paragraph) RequisitoDAO(model.dao.RequisitoDAO) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DocumentException(com.itextpdf.text.DocumentException) ProjetoDAO(model.dao.ProjetoDAO) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Example 15 with Document

use of com.itextpdf.text.Document in project specify by rhebecaabreu.

the class DocumentoReqCartaoEstoria method criaPDF.

/**
 * @param args
 * @throws FileNotFoundException
 * @throws DocumentException
 * @throws IOException
 * @throws MalformedURLException
 */
public void criaPDF(JTable table_1, java.util.List<Integer> codigosReq) throws FileNotFoundException, DocumentException {
    // Listing 1. Instantiation of document object
    Document document = new Document();
    try {
        document.setPageSize(PageSize.A4);
        document.setMargins(50, 50, 50, 50);
        nomepdf = MeusProjetos.getNome_pdf() + ".pdf";
        // Listing 2. Creation of PdfWriter object
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(nomepdf));
        ModelPDFRodape mf = new ModelPDFRodape();
        mf.setTopo("Specify");
        writer.setPageEvent(mf);
        document.open();
        // Listing 3. Creation of chapter object
        Paragraph title1 = new Paragraph("Documento de Requisitos", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD));
        title1.setAlignment(Element.ALIGN_CENTER);
        Chapter chapter1 = new Chapter(title1, 1);
        chapter1.setNumberDepth(0);
        // Listing 4. Creation of section object
        RequisitoDAO rrdao = new RequisitoDAO();
        // Listing 4. Creation of section object
        ProjetoDAO pread = new ProjetoDAO();
        for (Requisito r : rrdao.readOrderBy()) {
            for (Projeto p : pread.read()) {
                for (int i = 0; i < codigosReq.size(); i++) {
                    if (r.getProjcodigo() == p.getCodigo() && r.getCodigo() == codigosReq.get(0)) {
                        Paragraph title_project2 = new Paragraph("Projeto: " + p.getNome(), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD));
                        title_project2.setAlignment(Element.ALIGN_CENTER);
                        chapter1.add(title_project2);
                        break;
                    }
                }
            }
        }
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        String data = dateFormat.format(date);
        Paragraph data_hora = new Paragraph("Gerado em " + data, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL));
        data_hora.setAlignment(Element.ALIGN_CENTER);
        chapter1.add(data_hora);
        Paragraph listRequirement = new Paragraph("Lista de Requisitos", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL));
        Section section1 = chapter1.addSection(listRequirement);
        RequisitoDAO rdao = new RequisitoDAO();
        Font f = FontFactory.getFont(FontFactory.HELVETICA, 13, Font.NORMAL);
        Font ff = FontFactory.getFont(FontFactory.HELVETICA, 13, Font.BOLD);
        for (Requisito r : rdao.readOrderBy()) {
            if (table_1.getSelectedRow() != -1) {
                for (int j = 0; j < (codigosReq.size()); j++) {
                    if (r.getCodigo() == codigosReq.get(j)) {
                        if (r.getTipoReq().toString().equals("FUNCIONAL")) {
                            Paragraph o = new Paragraph();
                            o.setFont(ff);
                            o.add("Tipo de Requisito: ");
                            o.setFont(f);
                            o.add("Funcional" + "       ");
                            o.setFont(ff);
                            o.add(" Código: ");
                            o.setFont(f);
                            o.add(String.valueOf(r.getCodigo()));
                            section1.add(o);
                        }
                        if (r.getTipoReq().toString().equals("NAOFUNCIONAL")) {
                            Paragraph o = new Paragraph();
                            o.setFont(ff);
                            o.add("Tipo de Requisito: ");
                            o.setFont(f);
                            o.add("Não-funcional" + "       ");
                            o.setFont(ff);
                            o.add(" Código: ");
                            o.setFont(f);
                            o.add(String.valueOf(r.getCodigo()));
                            section1.add(o);
                        }
                        if (r.getTipoReq().toString().equals("USUARIO")) {
                            Paragraph o = new Paragraph();
                            o.setFont(ff);
                            o.add("Tipo de Requisito: ");
                            o.setFont(f);
                            o.add("       Usuário" + "       ");
                            o.setFont(ff);
                            o.add(" Código: ");
                            o.setFont(f);
                            o.add(String.valueOf(r.getCodigo()));
                            section1.add(o);
                        }
                        if (r.getTipoReq().toString().equals("NEGOCIO")) {
                            Paragraph o = new Paragraph();
                            o.setFont(ff);
                            o.add("Tipo de Requisito: ");
                            o.setFont(f);
                            o.add("       Negócio" + "       ");
                            o.setFont(ff);
                            o.add(" Código: ");
                            o.setFont(f);
                            o.add(String.valueOf(r.getCodigo()));
                            section1.add(o);
                        }
                        Paragraph p = new Paragraph();
                        Paragraph q = new Paragraph();
                        Paragraph rr = new Paragraph();
                        Paragraph s = new Paragraph();
                        Paragraph t = new Paragraph();
                        p.setFont(ff);
                        p.add("Identificador: ");
                        p.setFont(f);
                        p.add(r.getId());
                        q.setFont(ff);
                        q.add("O(a) " + r.getSujeito() + " deve ser capaz de " + r.getAcaoRestricao() + " para " + r.getValorRazao());
                        rr.setFont(ff);
                        rr.add("Grau Prioridade: ");
                        rr.setFont(f);
                        rr.add(r.getGrauPrioridade().toString());
                        s.setFont(ff);
                        s.add("Grau Dificuldade: ");
                        s.setFont(f);
                        s.add(r.getGrauDificuldade().toString());
                        t = new Paragraph(" ");
                        section1.add(p);
                        section1.add(q);
                        section1.add(rr);
                        section1.add(s);
                        section1.add(t);
                    }
                }
            }
        }
        // Listing 3. Creation of chapter object
        Paragraph title2 = new Paragraph("Cartões de Estória", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD));
        title2.setAlignment(Element.ALIGN_CENTER);
        Chapter chapter2 = new Chapter(title2, 2);
        chapter2.setNumberDepth(0);
        for (Requisito r : rrdao.readOrderBy()) {
            for (Projeto p : pread.read()) {
                for (int i = 0; i < codigosReq.size(); i++) {
                    if (r.getProjcodigo() == p.getCodigo() && r.getCodigo() == codigosReq.get(0)) {
                        Paragraph title_project2 = new Paragraph("Projeto: " + p.getNome(), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD));
                        title_project2.setAlignment(Element.ALIGN_CENTER);
                        chapter2.add(title_project2);
                        break;
                    }
                }
            }
        }
        Paragraph data_hora2 = new Paragraph("Gerado em " + data, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL));
        data_hora2.setAlignment(Element.ALIGN_CENTER);
        chapter2.add(data_hora2);
        Paragraph listUserStories = new Paragraph("Lista de Cartões de Estória", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL));
        Section section2 = chapter2.addSection(listUserStories);
        for (Requisito r : rdao.readOrderBy()) {
            if (table_1.getSelectedRow() != -1) {
                for (int j = 0; j < (codigosReq.size()); j++) {
                    if (r.getCodigo() == codigosReq.get(j)) {
                        Paragraph p = new Paragraph();
                        Paragraph q = new Paragraph();
                        Paragraph rr = new Paragraph();
                        Paragraph s = new Paragraph();
                        Paragraph t = new Paragraph();
                        p.setFont(ff);
                        p.add("Identificador: ");
                        p.setFont(f);
                        p.add(r.getId());
                        p.setFont(ff);
                        p.add("       Código: ");
                        p.setFont(f);
                        p.add(String.valueOf(r.getCodigo()));
                        q.setFont(ff);
                        q.add("Como um " + r.getSujeito() + " eu quero " + r.getAcaoRestricao() + " a fim de " + r.getValorRazao());
                        Paragraph qq = new Paragraph();
                        qq.setFont(ff);
                        qq.add("Estimativa: ");
                        qq.setFont(f);
                        qq.add(r.getEstimativa() + " hora(s)");
                        rr.setFont(ff);
                        rr.add("Grau Prioridade: ");
                        rr.setFont(f);
                        rr.add(r.getGrauPrioridade().toString());
                        s.setFont(ff);
                        s.add("Grau Dificuldade: ");
                        s.setFont(f);
                        s.add(r.getGrauDificuldade().toString());
                        t = new Paragraph(" ");
                        // Paragraph p = new Paragraph();
                        section2.add(p);
                        section2.add(q);
                        section2.add(qq);
                        section2.add(rr);
                        section2.add(s);
                        section2.add(t);
                    }
                }
            }
        }
        // Listing 5. Addition of a chapter to the main document
        document.add(chapter1);
        document.add(chapter2);
        document.add(Chunk.NEXTPAGE);
    } catch (DocumentException ex) {
        System.out.println("Error:" + ex);
    } catch (FileNotFoundException ex) {
        System.out.println("Error:" + ex);
    } finally {
        document.close();
    }
    try {
        Desktop.getDesktop().open(new File(nomepdf));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) Requisito(model.bean.Requisito) ModelPDFRodape(model.bean.ModelPDFRodape) Chapter(com.itextpdf.text.Chapter) FileNotFoundException(java.io.FileNotFoundException) Projeto(model.bean.Projeto) IOException(java.io.IOException) Document(com.itextpdf.text.Document) Section(com.itextpdf.text.Section) Date(java.util.Date) Font(com.itextpdf.text.Font) Paragraph(com.itextpdf.text.Paragraph) RequisitoDAO(model.dao.RequisitoDAO) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DocumentException(com.itextpdf.text.DocumentException) ProjetoDAO(model.dao.ProjetoDAO) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Aggregations

Document (com.itextpdf.text.Document)32 FileOutputStream (java.io.FileOutputStream)25 PdfWriter (com.itextpdf.text.pdf.PdfWriter)22 IOException (java.io.IOException)12 DocumentException (com.itextpdf.text.DocumentException)10 Rectangle (com.itextpdf.text.Rectangle)10 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)10 File (java.io.File)10 Font (com.itextpdf.text.Font)8 Paragraph (com.itextpdf.text.Paragraph)8 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)6 FileNotFoundException (java.io.FileNotFoundException)6 Graphics2D (java.awt.Graphics2D)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)4 Chapter (com.itextpdf.text.Chapter)4 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 OutputStream (java.io.OutputStream)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4