Example usage for com.itextpdf.text Document close

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

Introduction

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

Prototype

boolean close

To view the source code for com.itextpdf.text Document close.

Click Source Link

Document

Has the document already been closed?

Usage

From source file:com.semfapp.adamdilger.semf.Pdf.java

License:Open Source License

private void createPDFNew(String filePath, String htmlString, @Nullable ArrayList<ImageFile> images) {

    File file = null;//  w w  w. j  a  va2 s. c o m
    try {
        file = new File(filePath);

        // step 1
        Document document = new Document();

        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12.5f);

        // step 3
        document.open();

        // step 4

        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(activity.getAssets().open("styles.css"));
        cssResolver.addCss(cssFile);

        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // Pipelines

        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);

        Drawable d = activity.getResources().getDrawable(R.drawable.logo_icon);
        Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] bitmapData = stream.toByteArray();

        Image image = Image.getInstance(bitmapData);
        image.setAbsolutePosition(35, 763);
        image.scalePercent(12);
        document.add(image);

        p.parse(new ByteArrayInputStream(htmlString.getBytes(StandardCharsets.UTF_8)));

        if (images != null) {
            System.out.println("Adding IMage");

            for (int x = 0; x < images.size(); x++) {
                Image cursor = images.get(x).getImage();

                float ratio = cursor.getPlainHeight() / cursor.getPlainWidth();

                float imgWidth = document.getPageSize().getWidth() - 100;
                float imgHeight = document.getPageSize().getHeight() - 100;

                cursor.scaleToFit(new Rectangle(imgWidth, imgHeight));

                document.add(cursor);
            }
        }

        // step 5
        document.close();

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

From source file:com.shiyq.itext.PdfUtil.java

public static void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();//  w  ww.j  av  a2s. c  o m
    //?
    int fontsize = 10;
    FontSelector selector = getSelector(fontsize);
    String title = "20165??";
    //?
    int titleFontSize = 20;
    //
    addTile(document, title, titleFontSize);
    String describe = "--2016-5-4 12:23:23 ";
    //
    addDescribe(document, describe, selector);
    String[] head = { "??", "?", "?", "????", "??", "",
            "", "??", "??", "???", "????" };
    String[] code = { "card_no", "card_type", "plate", "owner_name", "blance", "start_time", "end_time",
            "month_money", "spread_time", "spread_emp_no", "spread_emp_name" };
    List<Map<String, Object>> list = setList();
    PdfPTable table = new PdfPTable(head.length + 1);
    //
    createTableHead(table, head, selector);
    //
    createTableContent(table, list, code, selector);
    document.add(table);
    document.close();
}

From source file:com.skatettoo.reportes.Generador.java

public String generarPDF() throws Exception {
    try {/*w  w w.ja v  a 2s .  c  o m*/
        String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("img");
        path = path.substring(0, path.indexOf("\\build"));
        path = path + "\\web\\img\\";
        Document doc = new Document(PageSize.A4, 36, 36, 10, 10);
        PdfPTable tabla = new PdfPTable(4);
        PdfWriter.getInstance(doc, new FileOutputStream(path + "\\archivo\\reporte.pdf\\"));
        doc.open();
        Image img = Image.getInstance(path + "Skatetoo4.png");
        img.scaleAbsolute(40, 40);
        img.setAlignment(Element.ALIGN_LEFT);
        doc.add(img);
        doc.addTitle(this.titulo);
        doc.addAuthor("\n ");
        doc.addAuthor("\n ");
        doc.addAuthor("\n ");
        doc.addAuthor("\n ");
        tabla.setWidthPercentage(100);
        tabla.setWidths(new float[] { 1.4f, 0.8f, 0.8f, 0.8f });
        Object font = new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
        PdfPCell cell = new PdfPCell(new Phrase("Reporte de tatuadores", (Font) font));
        cell.setColspan(4);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPaddingTop(0f);
        cell.setPaddingBottom(7f);
        cell.setBackgroundColor(new BaseColor(0, 0, 0));
        cell.setBorder(0);
        cell.setBorderWidthBottom(2f);
        tabla.addCell(cell);
        tabla.addCell("Tatuador");
        tabla.addCell("Cantidad de diseos");
        tabla.addCell("Citas realizadas");
        tabla.addCell("Noticias publicadas");
        for (Usuario u : this.getUsu()) {
            tabla.addCell(u.getNombre() + " " + u.getApellido());
            tabla.addCell(String.valueOf(u.getDisenioList().size()));
            tabla.addCell(String.valueOf(u.getCitaList1().size()));
            tabla.addCell(String.valueOf(u.getNoticiaList().size()));
        }
        doc.add(tabla);
        doc.bottomMargin();
        /* doc.add(new Paragraph("Tatuador mas solicitado"));
         for(Usuario u : this.getUs()){
        doc.add(new Paragraph(u.getNombre() + " " + u.getApellido()));
         }*/
        doc.close();
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext();

        externalContext.responseReset();
        externalContext.setResponseContentType("application/pdf");
        externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"reporte.pdf\"");

        FileInputStream inputStream = new FileInputStream(new File(path + "\\archivo\\reporte.pdf\\"));
        OutputStream outputStream = externalContext.getResponseOutputStream();

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }

        inputStream.close();
        context.responseComplete();
    } catch (Exception e) {
        throw e;
    }
    return "";
}

From source file:com.softwaremagico.tm.pdf.complete.PdfDocument.java

License:Open Source License

private void generatePDF(Document document, PdfWriter writer) throws EmptyPdfBodyException, Exception {
    addMetaData(document);/*  w w  w. j a v a 2 s  .c  o m*/
    document.open();
    // createCharacterPDF(document);
    createContent(document);
    document.close();
}

From source file:com.solidmaps.webapp.report.EnableCompanyRequerimentFederalPDF.java

public void generate(LicensePFEntity license, EnableRequerimentTypeFederalEnum type,
         EnableRequerimentSubjectFederalEnum subject, UserEntity user) {

     Document doc = new Document();
     PdfWriter docWriter = null;//from w  w  w .  j  ava2 s  .c  om

     try {

         this.createDocument(doc, docWriter, license);
         this.createTable(doc, type, subject, license, user);

     } catch (DocumentException dex) {
         dex.printStackTrace();
     } catch (Exception ex) {
         ex.printStackTrace();
     } finally {
         if (doc != null) {
             // close the document
             doc.close();
         }
         if (docWriter != null) {
             // close the writer
             docWriter.close();
         }
     }

 }

From source file:com.solidmaps.webapp.report.LicenseCivilIncludeProductPDF.java

License:Open Source License

public String generate(LicensePCEntity license, String type, List<LicensePCProductEntity> listProducts) {

    Document doc = new Document();
    PdfWriter docWriter = null;//  w w  w .java 2 s.c o  m
    String fileDocPath = "";

    try {

        fileDocPath = this.createDocument(doc, docWriter, license, type);
        this.createParagraph(doc, license, type, listProducts);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    return fileDocPath;

}

From source file:com.solidmaps.webapp.report.MapCivilPdfGenerator.java

License:Open Source License

public String createPDF() {

    logger.info("[map ex generate begin] company: " + company.getIdCompany());

    Document doc = new Document();
    PdfWriter docWriter = null;/* ww  w. j av  a  2 s .com*/
    String fileName = "";

    try {

        fileName = this.createDocument(doc, docWriter);
        this.createParagraph(doc);
        this.createProductsTable(doc);
        this.createSignature(doc);
        this.createInvoiceTable(doc);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    logger.info("[map ex generate] Filename: " + fileName);

    return fileName;
}

From source file:com.solidmaps.webapp.report.MapExercitoPdfGenerator.java

License:Open Source License

public String createPDF() {

    logger.info("[map ex generate begin] company: " + company.getIdCompany());

    Document doc = new Document();
    PdfWriter docWriter = null;/*from  w w  w .j  a v a2s. co  m*/
    String fileName = "";

    try {

        fileName = this.createDocument(doc, docWriter);
        this.createParagraph(doc);
        this.createProductsTable(doc);
        this.createFooter(doc);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    logger.info("[map ex generate] Filename: " + fileName);

    return fileName;
}

From source file:com.solidmaps.webapp.report.RenovationRequireExercitoPDF.java

public void generate(LicenseEXEntity license) {

    Document doc = new Document();
    PdfWriter docWriter = null;/* ww w .  j  a  v  a  2s .com*/

    try {

        this.createDocument(doc, docWriter, license);
        this.createParagraph(doc, license);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

}

From source file:com.solidmaps.webapp.report.RequerimentAlterLicenseExercitoPDF.java

/**
 * //w w  w  . ja  v  a2s.co m
 * @param license 
 *       Licensa vigente do Exrcito
 * @param listCharterType 
 *       Lista de Atividades a incluir
 * @param listProducts
 *       Lista de Produtos a incluir, bem como os limites de Armazenamento
 * @param text
 *       Texto sobre a altees Cadastrais
 * @param type
 *       Tipo de alterao cadastral (Endereo, Responsvel, etc)
 */
public String generate(LicenseEXEntity license, List<CharterTypeEntity> listCharterType,
        List<LicenseExCharterProductEntity> listProducts, String text, String type) {

    Document doc = new Document();
    PdfWriter docWriter = null;
    String fileDocPath = "";

    try {

        fileDocPath = this.createDocument(doc, docWriter, license);
        this.createParagraph(doc, license);
        this.createCharter(doc, license, listCharterType);
        this.createIncludeProduct(doc, license, listProducts);
        this.createAlterLicense(doc, license, text, type);
        this.createSignature(doc, license);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    return fileDocPath;

}