Example usage for org.apache.pdfbox.io MemoryUsageSetting setupMixed

List of usage examples for org.apache.pdfbox.io MemoryUsageSetting setupMixed

Introduction

In this page you can find the example usage for org.apache.pdfbox.io MemoryUsageSetting setupMixed.

Prototype

public static MemoryUsageSetting setupMixed(long maxMainMemoryBytes) 

Source Link

Document

Setups buffering memory usage to use a portion of main-memory and additionally temporary file(s) in case the specified portion is exceeded.

Usage

From source file:com.trollworks.gcs.pdfview.PdfDockable.java

License:Open Source License

public PdfDockable(PdfRef pdfRef, int page, String highlight) {
    super(new BorderLayout());
    mFile = pdfRef.getFile();//from  w  w w  .j a  v a2  s . c om
    int pageCount = 9999;
    try {
        mPdf = PDDocument.load(pdfRef.getFile(), MemoryUsageSetting.setupMixed(50 * 1024 * 1024));
        pageCount = mPdf.getNumberOfPages();
    } catch (Exception exception) {
        Log.error(exception);
    }
    mToolbar = new Toolbar();

    mZoomInButton = new IconButton(StdImage.get("ZoomIn"), //$NON-NLS-1$
            formatWithKey(SCALE_DOC_UP, KeyStroke.getKeyStroke('=')), () -> mPanel.zoomIn());
    mToolbar.add(mZoomInButton);
    mZoomOutButton = new IconButton(StdImage.get("ZoomOut"), //$NON-NLS-1$
            formatWithKey(SCALE_DOC_DOWN, KeyStroke.getKeyStroke('-')), () -> mPanel.zoomOut());
    mToolbar.add(mZoomOutButton);
    mActualSizeButton = new IconButton(StdImage.get("ActualSize"), //$NON-NLS-1$
            formatWithKey(ACTUAL_SIZE, KeyStroke.getKeyStroke('1')), () -> mPanel.actualSize());
    mToolbar.add(mActualSizeButton);
    mZoomStatus = new JLabel("100%"); //$NON-NLS-1$
    mToolbar.add(mZoomStatus);

    mPageField = new EditorField(new DefaultFormatterFactory(new IntegerFormatter(1, pageCount, false)),
            event -> {
                if (mPanel != null) {
                    int pageIndex = ((Integer) mPageField.getValue()).intValue() - 1;
                    int newPageIndex = mPanel.goToPageIndex(pageIndex, null);
                    if (pageIndex != newPageIndex) {
                        mPageField.setValue(Integer.valueOf(newPageIndex + 1));
                    } else {
                        mPanel.requestFocusInWindow();
                    }
                }
            }, SwingConstants.RIGHT, Integer.valueOf(page), Integer.valueOf(9999), null);
    mToolbar.add(mPageField, Toolbar.LAYOUT_EXTRA_BEFORE);
    mPageStatus = new JLabel("/ -"); //$NON-NLS-1$
    mToolbar.add(mPageStatus);
    mPreviousPageButton = new IconButton(StdImage.get("PageUp"), //$NON-NLS-1$
            formatWithKey(PREVIOUS_PAGE, KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)),
            () -> mPanel.previousPage());
    mToolbar.add(mPreviousPageButton);
    mNextPageButton = new IconButton(StdImage.get("PageDown"), //$NON-NLS-1$
            formatWithKey(NEXT_PAGE, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)), () -> mPanel.nextPage());
    mToolbar.add(mNextPageButton);

    add(mToolbar, BorderLayout.NORTH);
    mPanel = new PdfPanel(this, mPdf, pdfRef, page, highlight);
    add(new JScrollPane(mPanel), BorderLayout.CENTER);

    setFocusCycleRoot(true);
    setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());
}

From source file:jgnash.report.pdf.Report.java

License:Open Source License

public Report() {
    this.pdfDocument = new PDDocument(MemoryUsageSetting.setupMixed(MAX_MEMORY_USAGE));

    setTableFont(loadFont(ReportFactory.getMonoFont(), pdfDocument));
    setHeaderFont(loadFont(ReportFactory.getHeaderFont(), pdfDocument));
    setFooterFont(loadFont(ReportFactory.getProportionalFont(), pdfDocument));

    // restore font size
    baseFontSize = getPreferences().getFloat(BASE_FONT_SIZE, DEFAULT_BASE_FONT_SIZE);

    // restore the page format
    setPageFormat(getPageFormat());//from  ww  w .j ava  2s  .c o  m
}

From source file:uk.bl.wa.tika.parser.pdf.pdfbox.PDFParser.java

License:Apache License

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    PDDocument pdfDocument = null;//from  w w w.j  ava2s.  c o  m
    TemporaryResources tmp = new TemporaryResources();

    try {
        // PDFBox can process entirely in memory, or can use a temp file
        //  for unpacked / processed resources
        // Decide which to do based on if we're reading from a file or not already
        TikaInputStream tstream = TikaInputStream.cast(stream);
        pdfDocument = PDDocument.load(new CloseShieldInputStream(stream),
                MemoryUsageSetting.setupMixed(100 * 1024 * 1024));

        if (pdfDocument.isEncrypted()) {
            String password = null;

            // Did they supply a new style Password Provider?
            PasswordProvider passwordProvider = context.get(PasswordProvider.class);
            if (passwordProvider != null) {
                password = passwordProvider.getPassword(metadata);
            }

            // Fall back on the old style metadata if set
            if (password == null && metadata.get(PASSWORD) != null) {
                password = metadata.get(PASSWORD);
            }

            // If no password is given, use an empty string as the default
            if (password == null) {
                password = "";
            }

        }
        metadata.set(Metadata.CONTENT_TYPE, "application/pdf");
        extractMetadata(pdfDocument, metadata);
        PDF2XHTML.process(pdfDocument, handler, metadata, extractAnnotationText, enableAutoSpace,
                suppressDuplicateOverlappingText, sortByPosition);
    } catch (Exception e) {
        log.error("Exception while parsing PDF: " + e);
    } finally {
        if (pdfDocument != null) {
            pdfDocument.close();
        }
        tmp.dispose();
    }
}