Example usage for com.itextpdf.text Document open

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

Introduction

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

Prototype

boolean open

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

Click Source Link

Document

Is the document open or not?

Usage

From source file:com.sapito.pdf.PDFView.PDFGeneratorDireccion.java

public void reportefinanzas(Map<String, Object> model, HttpServletResponse hsr1, List<CuentaBancaria> cuenta)
        throws Exception {
    Document document = new Document();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);

    document.open();
    document.addTitle("Sapito PDFs");
    document.addSubject("Pdf de sapito");
    Font font1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 30);
    font1.setColor(BaseColor.BLACK);/*from   w ww.ja v a2s  .co m*/
    Font font2 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 24);
    font2.setColor(BaseColor.BLACK);
    document.add(new Paragraph("TAILS 2015", font1));
    document.add(new Paragraph("Reportes Direccin\n", font2));

    //------------------------ TAIS  ______________________________
    Image tais = Image.getInstance(new URL("http://localhost:8080/SAPITO/resources/img/tais-banner.jpg"));
    document.add(tais);

    //---------------------  BODY    ---------------------------------------------------------
    Image body = Image.getInstance(new URL("http://localhost:8080/SAPITO/resources/img/body.png"));
    body.setAlignment(Image.UNDERLYING);
    body.setTransparency(new int[] { 0x00, 0x10 });
    body.setAbsolutePosition(50, 250);
    document.add(body);
    //-------------------------------------------------------------------------------------------
    Image footer = Image.getInstance(new URL("http://localhost:8080/SAPITO/resources/img/footer.jpg"));
    footer.setAbsolutePosition(50, 20);
    document.add(footer);
    //----------------------  TITLE ---------------------------
    String titulo = "Reporte de Finanzas"; //Cambiar el titulo del PDF aqui
    Font f = new Font(FontFamily.HELVETICA, 25.0f, Font.BOLD, BaseColor.BLACK);
    Chunk c = new Chunk(titulo + " \n ", f);
    c.setBackground(BaseColor.WHITE);
    Paragraph title = new Paragraph(c);
    title.setAlignment(Element.ALIGN_CENTER);
    //-------------------------  CONTENIDO -------------------------------------------------------
    document.add(title); //Titulo del PDF

    TextoPdf x = (TextoPdf) model.get("todoTexto");
    document.add(new Paragraph(x.getTodoTexto()));

    PdfPTable table = new PdfPTable(1);
    table.setWidthPercentage(100.0f);
    table.setWidths(new float[] { 2.0f });
    table.setSpacingBefore(10);

    // define font for table header row
    Font font = FontFactory.getFont(FontFactory.COURIER_BOLD);
    font.setColor(BaseColor.WHITE);

    // define table header cell
    PdfPCell cell = new PdfPCell();
    cell.setBackgroundColor(BaseColor.BLUE);
    cell.setPadding(5);

    // write table header 
    cell.setPhrase(new Phrase("Total de dinero que se tiene en CUENTAS BANCARIAS", font));
    table.addCell(cell);

    for (int i = 0; i < cuenta.size(); i++) {
        a = cuenta.get(i).getHaber();
        b = b + a;
    }
    String bla = b + " ";
    table.addCell(bla);

    document.add(table);

    //-------------------------- FIN CONTENIDO -----------------
    document.close();

    byte[] bytes = baos.toByteArray();

    hsr1.setContentType("application/pdf");
    hsr1.setContentLength(bytes.length);
    hsr1.getOutputStream().write(bytes);
}

From source file:com.sapito.pdf.PDFView.PDFGeneratorVentas.java

public void crearPDFFactura(HttpServletResponse hsr1) throws Exception {
    Document document = new Document();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);

    document.open();
    document.addTitle("Sapito PDFs");
    document.addSubject("Pdf de sapito");

    //------------------------ TAIS  ______________________________
    Image tais = Image.getInstance(new URL("http://localhost:8080/SAPITO/resources/img/tais-banner.jpg"));
    document.add(tais);/*from   w ww.  ja v a2  s  .c o  m*/

    //---------------------  BODY    ---------------------------------------------------------
    Image body = Image.getInstance(new URL("http://localhost:8080/SAPITO/resources/img/body.png"));
    body.setAlignment(Image.UNDERLYING);
    body.setTransparency(new int[] { 0x00, 0x10 });
    body.setAbsolutePosition(50, 250);
    document.add(body);
    //-------------------------------------------------------------------------------------------
    Image footer = Image.getInstance(new URL("http://localhost:8080/SAPITO/resources/img/footer.jpg"));
    footer.setAbsolutePosition(50, 20);
    document.add(footer);
    //----------------------  TITLE ---------------------------
    String titulo = "Facturas"; //Cambiar el titulo del PDF aqui
    Font f = new Font(FontFamily.HELVETICA, 25.0f, Font.BOLD, BaseColor.BLACK);
    Chunk c = new Chunk(titulo + " \n ", f);
    c.setBackground(BaseColor.WHITE);
    Paragraph title = new Paragraph(c);
    title.setAlignment(Element.ALIGN_CENTER);
    //-------------------------  CONTENIDO -------------------------------------------------------
    document.add(title); //Titulo del PDF
    PdfPTable table = new PdfPTable(1);

    table.addCell("Hola");
    table.addCell("Soy");
    table.addCell("el");
    table.addCell("pdf");
    table.addCell("del");
    table.addCell("sapito");
    document.add(table);

    document.close();

    byte[] bytes = baos.toByteArray();

    hsr1.setContentType("application/pdf");
    hsr1.setContentLength(bytes.length);
    hsr1.getOutputStream().write(bytes);
}

From source file:com.sarav.donormgmttool.EmailWithPdf.java

/**
 * Writes the content of a PDF file (using iText API)
 * to the {@link OutputStream}.//from w w  w  .ja va 2s. com
 * @param outputStream {@link OutputStream}.
 * @throws Exception
 */
public void writePdf(OutputStream outputStream) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, outputStream);

    document.open();

    document.addTitle("Test PDF");
    document.addSubject("Testing email PDF");
    document.addKeywords("iText, email");
    document.addAuthor("Jee Vang");
    document.addCreator("Jee Vang");

    Paragraph paragraph = new Paragraph();
    paragraph.add(new Chunk("hello!"));
    document.add(paragraph);

    document.close();
}

From source file:com.sarav.donormgmttool.SendMail.java

public void writePdf(OutputStream outputStream, float ramount, java.sql.Date rdate, int rnum, String rname,
        String rmode) throws Exception {

    Document document = new Document();

    //step2 http://developers.itextpdf.com/examples/xml-worker-itext5/html-tables
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);

    String PDFContent = "<center>\n" + "        <table style=\"text-align:center\">\n" + "            <tr>\n"
            + "                <td>\n"
            + "                    <img src=\"Images/Small - Team Everest Logo.png\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
            + "                </td>\n" + "                <td>\n"
            + "                    <h1 style=\"font-family: Arial\"><b><u>Team Everest</u></b></h1><b>(Regd No: 2292/09)</b>\n"
            + "                    <p style=\"font-family: Times\"> 5/1B, Magaveerar Street, Arnipalayam, Arni  632301<br>\n"
            + "                        Tiruvannamalai Dt, Tamilnadu, India Phone: +91 89399 12365<br>\n"
            + "                        Email: info@teameverestindia.org   Website: www.teameverestindia.org</p>\n"
            + "                </td>\n" + "            </tr>\n"
            + "            <tr><td><br><br><br><br></td></tr>\n" + "            <tr>\n"
            + "                <td>\n" + "                    <b>Reciept No:</b>*********\n"
            + "                </td>\n" + "                <td>\n"
            + "                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
            + "                </td>\n" + "                <td>\n"
            + "                    <b>Date:</b>********\n" + "                </td>\n" + "            </tr>\n"
            + "            <tr><td><br><br><br><br></td></tr>\n" + "            <tr>\n"
            + "                <td></td>\n" + "                <td>\n"
            + "                    <p style=\"font-family: Times\">Received the sum of Rs. <b>*****/- (*******)</b>, with thanks from\n"
            + "                        <b>*******</b>  as donation for Team Everest.</p>\n"
            + "                </td>\n" + "            </tr>\n"
            + "            <tr><td><br><br><br><br></td></tr>\n" + "            <tr>\n"
            + "                <td></td>\n" + "                <td>\n"
            + "                    <p style=\"text-align:left\"><b>Amount:</b> Rs.********&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Mode of Donation:</b>********</p>\n"
            + "                </td>\n" + "            </tr>\n"
            + "            <tr><td><br><br><br><br></td></tr>\n" + "            <tr><td><br><br><br><br></td>\n"
            + "                <td style=\"text-align:right\">\n"
            + "                    <p>For Team Everest&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>\n"
            + "                    <img src =\"Images/Signature.JPG\">\n" + "                </td>\n"
            + "            </tr>\n" + "            <tr><td><br><br><br><br></td></tr>\n" + "            <tr>\n"
            + "                <td></td>\n" + "                <td>\n"
            + "                    <p>*Donations are exempted under 80G of Income Tax Act<br>\n"
            + "                       1961 C, No. DIT (E) No. 2 (19) 1011. Pan No: AABTT6850G.</p>\n"
            + "                </td>\n" + "            </tr>\n" + "        </table>\n" + "        </center>";
    document.open();

    document.addTitle("EReceipt");
    document.addSubject("EreceiptPDF");
    document.addKeywords("iText, email");
    document.addAuthor("AKSarav");
    document.addCreator("AkSarav");

    //HTMLWorker htmlworker = new HTMLWorker(document);

    StringBuilder contentBuilder = new StringBuilder();
    Paragraph paragraph = new Paragraph();

    //System.out.println("PDFCONTENT"+PDFContent);
    contentBuilder.append(PDFContent);/*w w  w. ja  va2 s.c  o  m*/

    //HTML Processor
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    // Pipelines
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);

    //XMLWorker
    XMLWorker worker = new XMLWorker(html, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(contentBuilder.toString().getBytes()));

    // Closing the document

    document.close();
}

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  ww.  ja v a2 s.  com*/
    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();
    //?//from w  ww .jav a2s .  co 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 {//from w w  w.ja  va2s  .  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  ww. ja  v  a 2s  . c om
    document.open();
    // createCharacterPDF(document);
    createContent(document);
    document.close();
}

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

private String createDocument(Document doc, PdfWriter docWriter, LicensePFEntity license)
         throws FileNotFoundException, DocumentException {

     String fileName = "Requerimento de Habilitao de PJ - Cnpj: " + license.getCompany().getCnpj() + ".pdf";

     docWriter = PdfWriter.getInstance(doc, new FileOutputStream(filePath + fileName));

     // document header attributes
     doc.addAuthor("EnforceMaps");
     doc.addCreationDate();/*from   w  w w .j  a  v a2 s .  c  om*/
     doc.addProducer();
     doc.addCreator("EnforceMaps");
     doc.addTitle("Requerimento de Habilitao de PJ");
     doc.setPageSize(PageSize.A4);

     // open document
     doc.open();

     return fileName;
 }

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

License:Open Source License

private String createDocument(Document doc, PdfWriter docWriter, LicensePCEntity license, String type)
        throws FileNotFoundException, DocumentException {

    String fileName = "Requerimento de " + type + " Licensa: " + license.getCompany().getCnpj() + ".pdf";

    docWriter = PdfWriter.getInstance(doc, new FileOutputStream(filePath + fileName));

    // document header attributes
    doc.addAuthor("EnforceMaps");
    doc.addCreationDate();/*w w w.  j a v a  2 s. c o m*/
    doc.addProducer();
    doc.addCreator("EnforceMaps");
    doc.addTitle("Requerimento de " + type + " Licensa: " + license.getCompany().getCnpj());
    doc.setPageSize(PageSize.A4);

    // open document
    doc.open();

    return fileName;
}