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.coast.PDFPrinter_iText.java

License:Apache License

/**
 * Prints the document at its actual size. This is the recommended way to print.
 *///from   w w w.j a v a2 s .  co  m
private static void print(String pFileName, String pPayload) throws IOException, Exception {
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(pFileName));
    // step 3
    document.open();
    // step 4
    String _dateTime = LocalDateTime.now().toString();
    document.addTitle("List of All Customers & their assets as of: " + _dateTime);
    document.addCreationDate();
    document.addSubject("List of All Customers & their assets as of: " + _dateTime);

    Font _font = new Font();
    _font.setColor(BaseColor.BLUE);
    _font.setStyle(Font.BOLD);
    _font.setSize(15);

    Chunk _chunk = new Chunk("List of All Customers & their assets as of: " + _dateTime);
    _chunk.setFont(_font);
    Paragraph _header = new Paragraph();
    _header.add(_chunk);
    document.add(_header);
    document.add(new Paragraph(pPayload));

    // step 5
    document.close();
}

From source file:com.coderbd.pos.pdf.BarcodePdf.java

public void generateBarcodePdf(int amount) throws UnsupportedEncodingException {
    try {//from   ww  w . j  av  a  2  s. c  o  m
        String filename = directory + "\\" + barcodeData + ".pdf";
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Barcode128 code25 = new Barcode128();
        code25.setGenerateChecksum(true);
        code25.setCode(barcodeData);
        code25.setSize(10f);
        code25.setX(1.50f);

        PdfPTable pdfPTable = new PdfPTable(3);
        float[] widths = { 0.45f, .10f, .45f };
        pdfPTable.setWidths(widths);
        String codeName = getCodeName(shop.getShopName(), shop.getShopId(), amount);
        System.out.println("BarCodeName:" + codeName);

        Image image = code25.createImageWithBarcode(cb, null, null);

        Paragraph paragraph = new Paragraph(codeName);
        paragraph.setSpacingBefore(10.0f);
        PdfPCell title = new PdfPCell(paragraph);
        title.setBorder(Rectangle.NO_BORDER);

        PdfPCell barcodeCell = new PdfPCell(image, true);
        barcodeCell.setBorder(Rectangle.NO_BORDER);

        PdfPCell blank = new PdfPCell();
        blank.setBorder(Rectangle.NO_BORDER);

        Paragraph blankParagraph = new Paragraph("-                                                    -");
        PdfPCell pdfPCellBlank = new PdfPCell(blankParagraph);
        pdfPCellBlank.setBorder(Rectangle.NO_BORDER);

        for (int i = 0; i < barcodeQuantity; i++) {
            /**
             * This is for barcode pdf title
             */
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);

            /**
             * This is for barcode image
             */
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);

            /**
             * Blank space after barcode image
             */
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
        }
        document.add(pdfPTable);
        document.close();
    } catch (DocumentException | FileNotFoundException dex) {
        System.out.println(dex.getMessage());
    }
}

From source file:com.coderbd.pos.pdf.BarcodePdf.java

public void generateBarcodePdf(boolean fixedRate, Product p) throws UnsupportedEncodingException {
    try {/*from w  ww. j  av a  2 s.c om*/
        String filename = directory + "\\" + barcodeData + ".pdf";
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Barcode128 code25 = new Barcode128();
        code25.setGenerateChecksum(true);
        code25.setCode(barcodeData);
        code25.setSize(10f);
        code25.setX(1.50f);

        PdfPTable pdfPTable = new PdfPTable(3);
        float[] widths = { 0.45f, .10f, .45f };
        pdfPTable.setWidths(widths);
        String codeName = getCodeName(shop.getShopName(), shop.getShopId(), (int) p.getProductBuyRate());
        System.out.println("BarCodeName:" + codeName);

        Image image = code25.createImageWithBarcode(cb, null, null);

        Paragraph paragraph = new Paragraph(codeName);
        paragraph.setSpacingBefore(10.0f);
        PdfPCell title = new PdfPCell(paragraph);
        title.setBorder(Rectangle.NO_BORDER);

        PdfPCell barcodeCell = new PdfPCell(image, true);
        barcodeCell.setBorder(Rectangle.NO_BORDER);

        PdfPCell blank = new PdfPCell();
        blank.setBorder(Rectangle.NO_BORDER);

        Paragraph blankParagraph = new Paragraph("-                                                    -");
        PdfPCell pdfPCellBlank = new PdfPCell(blankParagraph);
        pdfPCellBlank.setBorder(Rectangle.NO_BORDER);

        for (int i = 0; i < barcodeQuantity; i++) {
            /**
             * This is for barcode pdf title
             */
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);

            /**
             * This is for barcode image
             */
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);

            /**
             * Blank space after barcode image
             */
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
        }
        document.add(pdfPTable);
        document.close();
    } catch (DocumentException | FileNotFoundException dex) {
        System.out.println(dex.getMessage());
    }
}

From source file:com.coderbd.pos.pdf.CodePDF.java

public boolean genCodeVer2Pdf() throws UnsupportedEncodingException {

    if (isFixedRated == true) {
        barcodeQuantity = 12;/*from   w  ww.  ja  v  a  2s.  c  om*/
    } else {
        barcodeQuantity = 15;
    }

    try {
        String filename = "";

        if (isFixedRated) {
            filename = directory + "\\" + product.getProductBarcode() + "_FIXED_TK" + ".pdf";
        } else {
            filename = directory + "\\" + product.getProductBarcode() + ".pdf";
        }

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Barcode128 code25 = new Barcode128();
        code25.setGenerateChecksum(true);
        code25.setCode(product.getProductBarcode());
        code25.setSize(10f);
        code25.setX(1.50f);

        PdfPTable pdfPTable = new PdfPTable(7);
        pdfPTable.setWidthPercentage(98);
        float[] widths = { 0.22f, 0.04f, 0.22f, 0.04f, 0.22f, 0.04f, 0.22f };
        pdfPTable.setWidths(widths);
        String codeName = getCodeName(product.getShop().getShopName(), product.getShop().getShopId(),
                (int) product.getProductBuyRate());

        System.out.println("BarCodeName:" + codeName);

        Image image = code25.createImageWithBarcode(cb, null, null);

        Font titleArialFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
        Font priceFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.BOLD);

        Paragraph paragraph = new Paragraph(codeName, titleArialFont);
        paragraph.setSpacingBefore(10.0f);
        PdfPCell title = new PdfPCell(paragraph);
        title.setBorder(Rectangle.NO_BORDER);

        Paragraph fixedTKParagraph = new Paragraph("FIXED TK: " + product.getProductSellRate(), priceFont);
        PdfPCell fixedRateCell = new PdfPCell(fixedTKParagraph);
        fixedRateCell.setBorder(Rectangle.NO_BORDER);

        PdfPCell barcodeCell = new PdfPCell(image, true);
        barcodeCell.setBorder(Rectangle.NO_BORDER);

        PdfPCell blank = new PdfPCell();
        blank.setBorder(Rectangle.NO_BORDER);

        Paragraph blankParagraph = new Paragraph("-                           -");
        PdfPCell pdfPCellBlank = new PdfPCell(blankParagraph);
        pdfPCellBlank.setBorder(Rectangle.NO_BORDER);

        for (int i = 0; i < barcodeQuantity; i++) {
            /**
             * This is for barcode pdf title
             */
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(title);

            /**
             * This is for barcode image
             */
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(barcodeCell);
            /**
             *
             */
            if (isFixedRated == true) {
                pdfPTable.addCell(fixedRateCell);
                pdfPTable.addCell(blank);
                pdfPTable.addCell(fixedRateCell);
                pdfPTable.addCell(blank);
                pdfPTable.addCell(fixedRateCell);
                pdfPTable.addCell(blank);
                pdfPTable.addCell(fixedRateCell);
            }
            /**
             * Blank space after barcode image
             */
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
            pdfPTable.addCell(blank);
            pdfPTable.addCell(pdfPCellBlank);
        }
        document.add(pdfPTable);
        document.add(new Paragraph(
                "Shop Name: " + product.getShop().getShopName() + ", Name:" + product.getProductName()
                        + "\n,Qty: " + product.getProductStock() + ", " + new Date().toString(),
                titleArialFont));
        document.close();
        return true;
    } catch (DocumentException | FileNotFoundException dex) {
        System.out.println(dex.getMessage());
        return false;
    }
}

From source file:com.coderbd.pos.pdf.OrderFileBuilder.java

public String makePdf(ShopOrder shopOrder) {
    Document document = new Document(new Rectangle(205, 800));
    String fileName = directory + "\\" + shopOrder.getCustomerOrder().getOrderBarcode() + ".pdf";
    String receiptText = receipt.getIndentedOrder(shopOrder);
    System.out.println(receiptText);

    document.setMargins(3, 2, 2, 2);/*from w  w w  .j a  va2 s  .  c  o  m*/

    Font courierFont = FontFactory.getFont("courier");
    courierFont.setSize(10f);

    try {
        PdfWriter.getInstance(document, new FileOutputStream(fileName));

        document.open();
        document.add(new Paragraph(receiptText, courierFont));
        document.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return fileName;
}

From source file:com.coderbd.pos.pdf.OrderReportPDF.java

public boolean genPdf() throws UnsupportedEncodingException {
    List<SupplierOrderProductReport> soprs = sor.getSupplierOrderProductReports();
    IDBuilder idBuilder = new IDBuilder();
    Font innerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
    Font headerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.BOLD);
    try {//w  w w  .  java  2 s  .com
        String timestamp = idBuilder.getUniqueTimeStampID();
        String filename = sor.getSupplier().getSupplierName() + "_O" + sor.getSupplierOrderId() + "_"
                + timestamp + ".pdf";
        String filePath = directory + "\\" + filename;

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
        document.open();

        PdfPTable pdfPTable = new PdfPTable(5);

        pdfPTable.setWidthPercentage(90);
        float[] widths = { 0.40f, 0.13f, 0.15f, 0.15f, 0.17f };
        pdfPTable.addCell(new Paragraph("Product Name", headerFont));
        pdfPTable.addCell(new Paragraph("Buy Rate", headerFont));
        pdfPTable.addCell(new Paragraph("Primary Qty", headerFont));
        pdfPTable.addCell(new Paragraph("Unsold Qty", headerFont));
        pdfPTable.addCell(new Paragraph("Unsold Amount", headerFont));

        pdfPTable.setWidths(widths);
        for (SupplierOrderProductReport sopr : soprs) {

            SupplierOrderProduct sop = sopr.getSupplierOrderProduct();
            String name = sop.getSupplierProductName();
            Double rate = sop.getSupplierRate();
            Integer pQty = sop.getSupplierProductQuantity();
            Integer unQty = sopr.getUnSoldProductQuantity();
            Double unAmount = sopr.getUnSoldProductAmount();

            Paragraph nameParag = new Paragraph(name, innerFont);
            Paragraph buyRateParag = new Paragraph(rate.toString(), innerFont);
            Paragraph primaryQuantityParag = new Paragraph(pQty.toString(), innerFont);
            Paragraph unsoldQuantityParag = new Paragraph(unQty.toString(), innerFont);
            Paragraph unsoldAmountParag = new Paragraph(unAmount.toString(), innerFont);

            PdfPCell nameCell = new PdfPCell(nameParag);
            PdfPCell buyRateCell = new PdfPCell(buyRateParag);
            PdfPCell primaryQuantityCell = new PdfPCell(primaryQuantityParag);
            PdfPCell unsoldQuantityCell = new PdfPCell(unsoldQuantityParag);
            PdfPCell unsoldAmountCell = new PdfPCell(unsoldAmountParag);

            pdfPTable.addCell(nameCell);
            pdfPTable.addCell(buyRateCell);
            pdfPTable.addCell(primaryQuantityCell);
            pdfPTable.addCell(unsoldQuantityCell);
            pdfPTable.addCell(unsoldAmountCell);
        }
        document.add(getHeader());

        document.add(pdfPTable);
        document.add(getFooter());
        document.add(new Paragraph("            Report Generated: " + new Date().toString(), innerFont));
        document.close();
        return true;
    } catch (DocumentException | FileNotFoundException dex) {
        System.out.println(dex.getMessage());
        return false;
    }
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioEstoqueProdutos(boolean valoresNegativos, File destino) {
    Document doc = new Document();
    try {/*from w w  w . j  a  v a 2  s.  co  m*/
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        //String subTitulo = "Estoque Verificado no dia "
        //        + new SimpleDateFormat("dd/MM/yyyy HH:mm").format(Calendar.getInstance().getTime());

        String subTitulo = "Balano Geral de Estoque do perodo 01/12/2017  31/12/2017";
        inserirHead(doc,
                "Mercadinho Popular\n" + "Edvaneide Torres Vilar de Carvalho\n" + "CNPJ: 07.643.907/0001-18",
                subTitulo);

        PdfPTable table = getTableEstoqueProdutos(valoresNegativos);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioEstoqueProdutos(boolean valoresNegativos, boolean pararLista,
        Calendar dataInicio, Calendar dataFim, List<String> codigos_retirados, double total_requisitado,
        File destino) {//  ww  w  . j av a2 s. c o m
    Document doc = new Document();
    try {
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter instance = PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();

        //String subTitulo = "Estoque Verificado no dia "
        //        + new SimpleDateFormat("dd/MM/yyyy HH:mm").format(Calendar.getInstance().getTime());

        String data = "";
        try {
            data += OperacaoStringUtil.formatDataValor(dataInicio) + "  "
                    + OperacaoStringUtil.formatDataValor(dataFim);
        } catch (NullPointerException e) {
            data += ("01/01/" + (Calendar.getInstance().get(Calendar.YEAR) - 1)) + "  ";
            data += ("31/12/" + (Calendar.getInstance().get(Calendar.YEAR) - 1));
        }

        String subTitulo = "Balano Geral de Estoque do perodo " + data; //" 01/12/2017  31/12/2017";
        inserirHead(doc,
                "Mercadinho Popular\n" + "Edvaneide Torres Vilar de Carvalho\n" + "CNPJ: 07.643.907/0001-18",
                subTitulo);

        PdfPTable table = getTableEstoqueProdutos(valoresNegativos, pararLista, codigos_retirados,
                total_requisitado);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioDebitoClientes(double maiorQue, File destino) {
    Document doc = new Document();
    try {//from  w w w.  j a va 2 s . c om
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        String subTitulo;
        if (maiorQue == 0) {
            subTitulo = "Dbito dos Clientes \n Refernte ao dia "
                    + new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime());
        } else {
            subTitulo = "Clientes com Dbitos Maiores que "
                    + OperacaoStringUtil.formatarStringValorMoedaComDescricao(maiorQue)
                    + "\n Refernte ao dia "
                    + new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime());
        }
        inserirHead(doc, "Dbito dos Clientes", subTitulo);

        PdfPTable table = getTableDebitoClientes(maiorQue);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }

}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioBalancoProdutos(Date inicio, Date fim, File destino) {
    Document doc = new Document();
    try {//from   w ww.j  av a 2  s .  co  m
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        String subTitulo = "Sada de produtos \n Refernte do dia "
                + new SimpleDateFormat("dd/MM/yyyy").format(inicio) + " ao dia "
                + new SimpleDateFormat("dd/MM/yyyy").format(fim);
        inserirHead(doc, "Balano de Produtos", subTitulo);

        PdfPTable table = getTableBalancoProdutos(inicio, fim);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }

}