Example usage for com.itextpdf.text.pdf PdfAWriter setOutlines

List of usage examples for com.itextpdf.text.pdf PdfAWriter setOutlines

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfAWriter setOutlines.

Prototype

public void setOutlines(final List<HashMap<String, Object>> outlines) 

Source Link

Document

Sets the bookmarks.

Usage

From source file:com.github.ossdevs.jhocr.converter.HocrToPdf.java

License:Open Source License

/**
 * @param pdfConformanceLevel determines into which format the PDF-A&/B will be converted.
 * @return true if the conversion was successful.
 *//*  www  . ja v  a 2 s. c  o m*/
private boolean convertToPDFA(PdfAConformanceLevel pdfConformanceLevel) {
    boolean result = false;
    Document document = new Document();

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    if (classLoader == null) {
        classLoader = Class.class.getClassLoader();
    }

    try {
        PdfAWriter writer = PdfAWriter.getInstance(document, getOutputStream(), pdfConformanceLevel);
        writer.createXmpMetadata();

        document.open();
        document.addHeader(KEY_JHOCR_INFO, KEY_JHOCR_INFO_VALUE);
        document.setMargins(0, 0, 0, 0);

        /**
         * TODO add documentation
         */
        for (HocrDocumentItem item : getItems()) {

            HocrParser parser = new HocrParser(item.getHocrInputStream());

            HocrDocument hocrDocument = parser.parse();

            /**
             * TODO add documentation
             * TODO add multipage image support
             */
            if (hocrDocument.getPages().size() > 1) {
                throw new UnsupportedOperationException(
                        "Multipage tif are not yet implemented, please report: http://code.google.com/p/jhocr/issues/list");
            }

            /**
             * TODO add documentation
             */
            for (HocrPage hocrPage : hocrDocument.getPages()) {
                HocrPageProcessor pageProcessor = new HocrPageProcessor(hocrPage, item.getImageInputStream(),
                        isUseImageDpi());

                if (pageProcessor.isInitialized()) {
                    pageProcessor.process(document, writer);
                }
            }
        }

        if (!outlines.isEmpty()) {
            writer.setOutlines(outlines);
        }

        InputStream is = this.getClass().getResourceAsStream("/sRGB.profile");

        ICC_Profile icc = ICC_Profile.getInstance(is);
        writer.setOutputIntents(KEY_JHOCR_INFO, KEY_JHOCR_INFO_VALUE, "http://www.color.org",
                "sRGB IEC61966-2.1", icc);

        /**
         * Closing the document body stream.
         */
        document.close();
        getOutputStream().close();
        result = true;

    } catch (UnsupportedOperationException e) {
        document.close();
        logger.error("This operation is not yet implemented.", e);
        result = false;
    } catch (DocumentException e) {
        document.close();
        logger.error("exception while genrating the PDF.", e);
        result = false;
    } catch (IOException e) {
        document.close();
        logger.error("FileSystem I/O Exception, please check the log and file system persmissions.", e);
        result = false;
    }

    return result;

}