Example usage for com.lowagie.text Document isOpen

List of usage examples for com.lowagie.text Document isOpen

Introduction

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

Prototype

public boolean isOpen() 

Source Link

Document

Checks if the document is open.

Usage

From source file:org.efaps.esjp.common.file.FileUtil_Base.java

License:Apache License

/**
 * @param _files pdfs to be combined into one file
 * @param _fileName name of the file to be generated
 * @param _paginate paginat or not/*  ww w  . j  ava2s  .  c om*/
 * @return file
 * @throws EFapsException on error
 */
public File combinePdfs(final List<File> _files, final String _fileName, final boolean _paginate)
        throws EFapsException {
    File ret = null;
    if (_files.size() == 1) {
        ret = _files.get(0);
    } else {
        try {
            final List<InputStream> pdfs = new ArrayList<>();
            for (final File file : _files) {
                pdfs.add(new FileInputStream(file));
            }
            ret = getFile(_fileName, "pdf");
            final OutputStream outputStream = new FileOutputStream(ret);
            final Document document = new Document();
            try {
                final List<PdfReader> readers = new ArrayList<>();
                int totalPages = 0;
                final Iterator<InputStream> iteratorPDFs = pdfs.iterator();

                // Create Readers for the pdfs.
                while (iteratorPDFs.hasNext()) {
                    final InputStream pdf = iteratorPDFs.next();
                    final PdfReader pdfReader = new PdfReader(pdf);
                    readers.add(pdfReader);
                    totalPages += pdfReader.getNumberOfPages();
                }
                final PdfSmartCopy copy = new PdfSmartCopy(document, outputStream);
                final Iterator<PdfReader> iteratorPDFReader = readers.iterator();
                document.open();
                while (iteratorPDFReader.hasNext()) {
                    final PdfReader pdfReader = iteratorPDFReader.next();
                    for (int i = 0; i < pdfReader.getNumberOfPages(); i++) {
                        final PdfImportedPage importedPage = copy.getImportedPage(pdfReader, i + 1);
                        copy.addPage(importedPage);

                        if (_paginate) {
                            LOG.debug("Missing page ", totalPages);
                        }
                    }
                }
                outputStream.flush();
                document.close();
                outputStream.close();
                // CHECKSTYLE:OFF
            } catch (final Exception e) {
                // CHECKSTYLE:ON
                e.printStackTrace();
            } finally {
                if (document.isOpen()) {
                    document.close();
                }
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (final IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        } catch (final FileNotFoundException e) {
            LOG.error("FileNotFoundException", e);
        }
    }
    return ret;
}

From source file:org.egov.ptis.actions.reports.SearchNoticesAction.java

License:Open Source License

/**
 * @param streamOfPDFFiles//ww  w.j a  v a 2  s  . c  o  m
 * @param outputStream
 * @return
 */
private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Entered into concatPDFs method");
    Document document = null;
    try {
        final List<InputStream> pdfs = streamOfPDFFiles;
        final List<PdfReader> readers = new ArrayList<>();
        final Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        // Create Readers for the pdfs.
        while (iteratorPDFs.hasNext()) {
            final InputStream pdf = iteratorPDFs.next();
            final PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            if (null == document)
                document = new Document(pdfReader.getPageSize(1));
        }
        // Create a writer for the outputstream
        final PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        final PdfContentByte cb = writer.getDirectContent(); // Holds the
                                                             // PDF
                                                             // data

        PdfImportedPage page;
        int pageOfCurrentReaderPDF = 0;
        final Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            final PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();

    } catch (final Exception e) {
        LOGGER.error("Exception in concat PDFs : ", e);

    } finally {
        if (document.isOpen())
            document.close();
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (final IOException ioe) {
            LOGGER.error("Exception in concat PDFs : ", ioe);
        }
    }
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Exit from concatPDFs method");
    return outputStream.toByteArray();
}

From source file:org.egov.wtms.web.controller.reports.GenerateBillForConsumerCodeController.java

License:Open Source License

private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) {

    Document document = null;
    try {//from  w w  w.j ava  2  s . c o m
        final List<InputStream> pdfs = streamOfPDFFiles;
        final List<PdfReader> readers = new ArrayList<>();
        final Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        // Create Readers for the pdfs.
        while (iteratorPDFs.hasNext()) {
            final InputStream pdf = iteratorPDFs.next();
            final PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            if (null == document)
                document = new Document(pdfReader.getPageSize(1));
        }
        // Create a writer for the outputstream
        final PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        final PdfContentByte cb = writer.getDirectContent(); // Holds the
        // PDF
        // data

        PdfImportedPage page;
        int pageOfCurrentReaderPDF = 0;
        final Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            final PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();

    } catch (final Exception e) {

        LOGGER.error("Exception in concat PDFs : ", e);
    } finally {
        if (document.isOpen())
            document.close();
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (final IOException ioe) {
            LOGGER.error("Exception in concat PDFs : ", ioe);
        }
    }
    return outputStream.toByteArray();
}

From source file:org.egov.wtms.web.controller.reports.GenerateConnectionBillController.java

License:Open Source License

private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) {

    Document document = null;
    try {//  w  ww.  j av  a  2s  . c o m
        final List<InputStream> pdfs = streamOfPDFFiles;
        final List<PdfReader> readers = new ArrayList<PdfReader>();
        final Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        // Create Readers for the pdfs.
        while (iteratorPDFs.hasNext()) {
            final InputStream pdf = iteratorPDFs.next();
            final PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            if (null == document)
                document = new Document(pdfReader.getPageSize(1));
        }
        // Create a writer for the outputstream
        final PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        final PdfContentByte cb = writer.getDirectContent(); // Holds the
        // PDF
        // data

        PdfImportedPage page;
        int pageOfCurrentReaderPDF = 0;
        final Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            final PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();

    } catch (final Exception e) {

        LOGGER.error("Exception in concat PDFs : ", e);
    } finally {
        if (document.isOpen())
            document.close();
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (final IOException ioe) {
            LOGGER.error("Exception in concat PDFs : ", ioe);
        }
    }

    return outputStream.toByteArray();
}

From source file:org.egov.wtms.web.controller.reports.SearchNoticeController.java

License:Open Source License

private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) {

    Document document = null;
    try {//from w w  w  . j  a va 2  s  . c om
        final List<InputStream> pdfs = streamOfPDFFiles;
        final List<PdfReader> readers = new ArrayList<>();
        final Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        // Create Readers for the pdfs.
        while (iteratorPDFs.hasNext()) {
            final InputStream pdf = iteratorPDFs.next();
            final PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            if (null == document)
                document = new Document(pdfReader.getPageSize(1));
        }
        // Create a writer for the outputstream
        final PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        final PdfContentByte cb = writer.getDirectContent(); // Holds the
        // PDF
        // data

        PdfImportedPage page;
        int pageOfCurrentReaderPDF = 0;
        final Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            final PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();

    } catch (final Exception e) {

        LOGGER.error("Exception in concat PDFs : ", e);
    } finally {
        if (document.isOpen())
            document.close();
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (final IOException ioe) {
            LOGGER.error("Exception in concat PDFs : ", ioe);
        }
    }

    return outputStream != null ? outputStream.toByteArray() : null;
}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

public Document extract(int begin, int end) throws DocumentException {

    this.assertValidPageRange(begin, end);

    PDFDocument result = new PDFDocument();

    ByteArrayInputStream bais = null;
    ByteArrayOutputStream baos = null;

    if (content != null) {

        com.lowagie.text.Document document = new com.lowagie.text.Document();

        try {// w  w w.ja  v  a  2  s .  c o m

            bais = new ByteArrayInputStream(content);
            baos = new ByteArrayOutputStream();

            PdfReader inputPDF = new PdfReader(bais);

            // create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, baos);

            document.open();
            PdfContentByte cb = writer.getDirectContent();

            PdfImportedPage page;

            while (begin <= end) {
                document.newPage();
                page = writer.getImportedPage(inputPDF, begin);
                cb.addTemplate(page, 0, 0);
                begin++;
            }

            document.close();

            result.load(new ByteArrayInputStream(baos.toByteArray()));

        } catch (Exception e) {
            throw new DocumentException(e);
        } finally {
            if (document.isOpen())
                document.close();
            IOUtils.closeQuietly(bais);
            IOUtils.closeQuietly(baos);
        }

    }

    return result;
}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

@Override
public void append(Document document) throws DocumentException {

    super.append(document);

    ByteArrayOutputStream baos = null;
    com.lowagie.text.Document mergedDocument = new com.lowagie.text.Document();

    try {//from  w ww .  java  2  s . c  o m

        baos = new ByteArrayOutputStream();
        PdfCopy copy = new PdfCopy(mergedDocument, baos);

        mergedDocument.open();

        // copy current document
        PdfReader reader = new PdfReader(content);
        int pageCount = reader.getNumberOfPages();
        for (int i = 0; i < pageCount;) {
            copy.addPage(copy.getImportedPage(reader, ++i));
        }

        // copy new document
        reader = new PdfReader(document.getContent());
        pageCount = reader.getNumberOfPages();
        for (int i = 0; i < pageCount;) {
            copy.addPage(copy.getImportedPage(reader, ++i));
        }

        mergedDocument.close();

        // replace content with new content
        content = baos.toByteArray();

    } catch (Exception e) {
        throw new DocumentException(e);
    } finally {
        if (mergedDocument.isOpen())
            mergedDocument.close();
        IOUtils.closeQuietly(baos);
    }

}

From source file:org.kuali.kfs.gl.report.TransactionReport.java

License:Open Source License

/**
 * Generates transaction report// ww w.ja va  2  s . com
 * 
 * @param errorSortedList list of error'd transactions
 * @param reportErrors map containing transactions and the errors associated with each transaction
 * @param reportSummary list of summary objects
 * @param runDate date report is run
 * @param title title of report
 * @param fileprefix file prefix of report file
 * @param destinationDirectory destination of where report file will reside
 */
public void generateReport(List<Transaction> errorSortedList, Map<Transaction, List<Message>> reportErrors,
        List<Summary> reportSummary, Date runDate, String title, String fileprefix,
        String destinationDirectory) {
    LOG.debug("generateReport() started");

    Font headerFont = FontFactory.getFont(FontFactory.COURIER, 8, Font.BOLD);
    Font textFont = FontFactory.getFont(FontFactory.COURIER, 8, Font.NORMAL);

    Document document = new Document(PageSize.A4.rotate());

    PageHelper helper = new PageHelper();
    helper.runDate = runDate;
    helper.headerFont = headerFont;
    helper.title = title;

    try {
        DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);

        String filename = destinationDirectory + "/" + fileprefix + "_";
        filename = filename + dateTimeService.toDateTimeStringForFilename(runDate);
        filename = filename + ".pdf";
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        writer.setPageEvent(helper);

        document.open();
        appendReport(document, headerFont, textFont, errorSortedList, reportErrors, reportSummary, runDate);
    } catch (DocumentException de) {
        LOG.error("generateReport() Error creating PDF report", de);
        throw new RuntimeException("Report Generation Failed: " + de.getMessage());
    } catch (FileNotFoundException fnfe) {
        LOG.error("generateReport() Error writing PDF report", fnfe);
        throw new RuntimeException("Report Generation Failed: Error writing to file " + fnfe.getMessage());
    } finally {
        if ((document != null) && document.isOpen()) {
            document.close();
        }
    }
}

From source file:org.kuali.kfs.module.purap.pdf.BulkReceivingPdf.java

License:Open Source License

/**
 * Generates the pdf document based on the data in the given BulkReceivingDocument
 * //from  www  .  j a  v a2 s. c  o  m
 * @param blkRecDoc   The BulkReceivingDocument to be used to generate the pdf.
 * @param byteStream  The ByteArrayOutputStream where the pdf document will be written to.
 */
public void generatePdf(BulkReceivingDocument blkRecDoc, ByteArrayOutputStream byteStream, String logoImage,
        String environment) {

    if (LOG.isDebugEnabled()) {
        LOG.debug("generatePdf() started for bulk receiving - " + blkRecDoc.getDocumentNumber());
    }

    Document document = null;

    try {

        document = this.getDocument(9, 9, 70, 36);
        PdfWriter writer = PdfWriter.getInstance(document, byteStream);

        //These have to be set because they are used by the onOpenDocument() and onStartPage() methods.
        this.logoImage = logoImage;
        this.blkRecDoc = blkRecDoc;
        this.environment = environment;

        // This turns on the page events that handle the header and page numbers.
        BulkReceivingPdf events = new BulkReceivingPdf().getPageEvents();
        writer.setPageEvent(this);

        document.open();

        document.add(createVendorAndDeliveryDetailsTable());
        document.add(new Paragraph("\nAdditional Details\n  ", ver_8_bold));
        document.add(createAdditionalDetailsTable());

        document.close();

    } catch (Exception de) {
        throw new RuntimeException("Document Exception when trying to save a Bulk Receiving PDF", de);
    } finally {
        if (document != null && document.isOpen()) {
            document.close();
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("generatePdf() completed for bulk receiving - " + blkRecDoc.getDocumentNumber());
    }
}

From source file:org.mapfish.print.config.layout.Page.java

License:Open Source License

public void render(PJsonObject params, RenderingContext context) throws DocumentException {
    final Document doc = context.getDocument();
    doc.setPageSize(getPageSizeRect(context, params));
    doc.setMargins(getMarginLeft(context, params), getMarginRight(context, params),
            getMarginTop(context, params) + (header != null ? header.getHeight() : 0),
            getMarginBottom(context, params) + (footer != null ? footer.getHeight() : 0));

    context.getCustomBlocks().setBackgroundPdf(PDFUtils.evalString(context, params, backgroundPdf));
    if (doc.isOpen()) {
        doc.newPage();/*www . j a  va  2s .  c  o m*/
    } else {
        doc.open();
    }
    context.getCustomBlocks().setHeader(header, params);
    context.getCustomBlocks().setFooter(footer, params);

    for (int i = 0; i < items.size(); i++) {
        Block block = items.get(i);
        if (block.isVisible(context, params)) {
            block.render(params, new Block.PdfElement() {
                public void add(Element element) throws DocumentException {
                    doc.add(element);
                }
            }, context);
        }
    }
}