org.apache.pdfbox.io.MemoryUsageSetting Java Examples

The following examples show how to use org.apache.pdfbox.io.MemoryUsageSetting. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppVariables.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static MemoryUsageSetting setPdfMem(String value) {
    switch (value) {
        case "1GB":
            AppVariables.setUserConfigValue("PdfMemDefault", "1GB");
            AppVariables.pdfMemUsage = MemoryUsageSetting.setupMixed(1024 * 1024 * 1024L, -1);
            break;
        case "2GB":
            AppVariables.setUserConfigValue("PdfMemDefault", "2GB");
            AppVariables.pdfMemUsage = MemoryUsageSetting.setupMixed(2048 * 1024 * 1024L, -1);
            break;
        case "Unlimit":
            AppVariables.setUserConfigValue("PdfMemDefault", "Unlimit");
            AppVariables.pdfMemUsage = MemoryUsageSetting.setupMixed(-1, -1);
            break;
        case "500MB":
        default:
            AppVariables.setUserConfigValue("PdfMemDefault", "500MB");
            AppVariables.pdfMemUsage = MemoryUsageSetting.setupMixed(500 * 1024 * 1024L, -1);
    }
    return AppVariables.pdfMemUsage;
}
 
Example #2
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Parses a PDF.
 * 
 * @param file file to be loaded
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering PDF streams 
 * 
 * @return loaded document
 * 
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file, String password, InputStream keyStore, String alias,
                              MemoryUsageSetting memUsageSetting) throws IOException
{
    @SuppressWarnings({"squid:S2095"}) // raFile not closed here, may be needed for signing
    RandomAccessBufferedFileInputStream raFile = new RandomAccessBufferedFileInputStream(file);
    try
    {
        return load(raFile, password, keyStore, alias, memUsageSetting);
    }
    catch (IOException ioe)
    {
        IOUtils.closeQuietly(raFile);
        throw ioe;
    }
}
 
Example #3
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private static PDDocument load(RandomAccessBufferedFileInputStream raFile, String password,
                               InputStream keyStore, String alias,
                               MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    try
    {
        PDFParser parser = new PDFParser(raFile, password, keyStore, alias, scratchFile);
        parser.parse();
        return parser.getPDDocument();
    }
    catch (IOException ioe)
    {
        IOUtils.closeQuietly(scratchFile);
        throw ioe;
    }
}
 
Example #4
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Parses a PDF. Depending on the memory settings parameter the given input stream is either
 * copied to memory or to a temporary file to enable random access to the pdf.
 *
 * @param input stream that contains the document. Don't forget to close it after loading.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, String password, InputStream keyStore, 
                              String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    try
    {
        RandomAccessRead source = scratchFile.createBuffer(input);
        PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
        parser.parse();
        return parser.getPDDocument();
    }
    catch (IOException ioe)
    {
        IOUtils.closeQuietly(scratchFile);
        throw ioe;
    }
}
 
Example #5
Source File: TemplateProcessor.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
public static void renderToPdf(String page, OutputStream os, ExtensionManager extensionManager, Event event) throws IOException {

        if(extensionManager.handlePdfTransformation(page, event, os)) {
            return;
        }
        PdfRendererBuilder builder = new PdfRendererBuilder();
        PDDocument doc = new PDDocument(MemoryUsageSetting.setupTempFileOnly());
        builder.usePDDocument(doc);
        builder.toStream(os);
        builder.useProtocolsStreamImplementation(new AlfioInternalFSStreamFactory(), "alfio-internal");
        builder.useProtocolsStreamImplementation(new InvalidProtocolFSStreamFactory(), "http", "https", "file", "jar");
        builder.useFastMode();

        var parser = new Parser();

        builder.withW3cDocument(W3CDom.toW3CDocument(parser.parse(page)), "");
        try (PdfBoxRenderer renderer = builder.buildPdfRenderer()) {
            File defaultFont = FONT_CACHE.get(DEJA_VU_SANS, TemplateProcessor::loadDejaVuFont);
            if (defaultFont != null && !defaultFont.exists()) { // fallback, the cached font will not be shared though
                FONT_CACHE.invalidate(DEJA_VU_SANS);
                defaultFont = loadDejaVuFont(DEJA_VU_SANS);
            }
            if (defaultFont != null) {
                renderer.getFontResolver().addFont(defaultFont, "DejaVu Sans Mono", null, null, false);
            }
            renderer.layout();
            renderer.createPDF();
        }
    }
 
Example #6
Source File: PdfBoxUtilities.java    From tess4j with Apache License 2.0 5 votes vote down vote up
/**
 * Merges PDF files.
 *
 * @param inputPdfFiles array of input files
 * @param outputPdfFile output file
 */
public static void mergePdf(File[] inputPdfFiles, File outputPdfFile) {
    try {
        PDFMergerUtility mergerUtility = new PDFMergerUtility();
        mergerUtility.setDestinationFileName(outputPdfFile.getPath());
        for (File inputPdfFile : inputPdfFiles) {
            mergerUtility.addSource(inputPdfFile);
        }
        mergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
    } catch (IOException ioe) {
        logger.error("Error counting PDF pages => " + ioe);
    }
}
 
Example #7
Source File: TitleBlockGenerator.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
public static InputStream merge(InputStream originalPDF, byte[] titleBlock) throws IOException {

        ByteArrayOutputStream tempOutStream = new ByteArrayOutputStream();
        PDFMergerUtility mergedDoc = new PDFMergerUtility();

        InputStream titleBlockStream = new ByteArrayInputStream(titleBlock);

        mergedDoc.addSource(titleBlockStream);
        mergedDoc.addSource(originalPDF);

        mergedDoc.setDestinationStream(tempOutStream);
        mergedDoc.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

        return new ByteArrayInputStream(tempOutStream.toByteArray());


    }
 
Example #8
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Merge the list of source documents, saving the result in the destination
 * file.
 *
 * @param memUsageSetting defines how memory is used for buffering PDF streams;
 *                        in case of <code>null</code> unrestricted main memory is used 
 * 
 * @throws IOException If there is an error saving the document.
 */
public void mergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException
{
    if (documentMergeMode == DocumentMergeMode.PDFBOX_LEGACY_MODE)
    {
        legacyMergeDocuments(memUsageSetting);
    }
    else if (documentMergeMode == DocumentMergeMode.OPTIMIZE_RESOURCES_MODE)
    {
        optimizedMergeDocuments(memUsageSetting);
    }
}
 
Example #9
Source File: AppVariables.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static MemoryUsageSetting getPdfMem() {
    return setPdfMem(getUserConfigValue("PdfMemDefault", "1GB"));
}
 
Example #10
Source File: Utilities.java    From pdfcompare with Apache License 2.0 4 votes vote down vote up
public static MemoryUsageSetting getMemorySettings(final int bytes) throws IOException {
    return MemoryUsageSetting.setupMixed(bytes).setTempDir(FileUtils.createTempDir("PdfBox").toFile());
}
 
Example #11
Source File: PDFMerge.java    From nju-lib-downloader with GNU General Public License v3.0 4 votes vote down vote up
public static void mergePDFs(File[] pdfs, Path outFilePath) throws IOException {
    PDFMergerUtility PDFmerger = new PDFMergerUtility();
    PDFmerger.setDestinationFileName(outFilePath.toString());
    for (File file : pdfs) PDFmerger.addSource(file);
    PDFmerger.mergeDocuments(MemoryUsageSetting.setupMixed(1024 * 1024 * 500));
}
 
Example #12
Source File: Splitter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * @return the current memory setting.
 */
public MemoryUsageSetting getMemoryUsageSetting()
{
    return memoryUsageSetting;
}
 
Example #13
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates an empty PDF document.
 * You need to add at least one page for the document to be valid.
 */
public PDDocument()
{
    this(MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #14
Source File: PdfDockable.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
public PdfDockable(PdfRef pdfRef, int page, String highlight) {
    super(new BorderLayout());
    mPath = pdfRef.getPath();
    int pageCount = 9999;
    try {
        mPdf = PDDocument.load(pdfRef.getPath().toFile(), MemoryUsageSetting.setupMixed(50 * 1024 * 1024));
        pageCount = mPdf.getNumberOfPages();
    } catch (Exception exception) {
        Log.error(exception);
    }
    mToolbar = new Toolbar();

    mZoomInButton = new IconButton(Images.ZOOM_IN, formatWithKey(I18n.Text("Scale Document Up"), KeyStroke.getKeyStroke('=')), () -> mPanel.zoomIn());
    mToolbar.add(mZoomInButton);
    mZoomOutButton = new IconButton(Images.ZOOM_OUT, formatWithKey(I18n.Text("Scale Document Down"), KeyStroke.getKeyStroke('-')), () -> mPanel.zoomOut());
    mToolbar.add(mZoomOutButton);
    mActualSizeButton = new IconButton(Images.ACTUAL_SIZE, formatWithKey(I18n.Text("Actual Size"), KeyStroke.getKeyStroke('1')), () -> mPanel.actualSize());
    mToolbar.add(mActualSizeButton);
    mZoomStatus = new JLabel("100%");
    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) {
                mPanel.requestFocusInWindow();
            } else {
                mPageField.setValue(Integer.valueOf(newPageIndex + 1));
            }
        }
    }, SwingConstants.RIGHT, Integer.valueOf(Math.max(page, 1)), Integer.valueOf(9999), null);
    mToolbar.add(mPageField, Toolbar.LAYOUT_EXTRA_BEFORE);
    mPageStatus = new JLabel("/ -");
    mToolbar.add(mPageStatus);
    mPreviousPageButton = new IconButton(Images.PAGE_UP, formatWithKey(I18n.Text("Previous Page"), KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)), () -> mPanel.previousPage());
    mToolbar.add(mPreviousPageButton);
    mNextPageButton = new IconButton(Images.PAGE_DOWN, formatWithKey(I18n.Text("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());
}
 
Example #15
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Parses a PDF.
 * 
 * @param input byte array that contains the document.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(byte[] input, String password, InputStream keyStore, 
        String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    RandomAccessRead source = new RandomAccessBuffer(input);
    PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
    parser.parse();
    return parser.getPDDocument();
}
 
Example #16
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. Depending on the memory settings parameter the given input stream is either
 * copied to main memory or to a temporary file to enable random access to the pdf.
 * 
 * @param input stream that contains the document. Don't forget to close it after loading.
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the PDF required a non-empty password.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, MemoryUsageSetting memUsageSetting)
        throws IOException
{
    return load(input, "", null, null, memUsageSetting);
}
 
Example #17
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. The given input stream is copied to the memory to enable random access to the
 * pdf. Unrestricted main memory will be used for buffering PDF streams.
 *
 * @param input stream that contains the document. Don't forget to close it after loading.
 * @param password password to be used for decryption
 *
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, String password)
        throws IOException
{
    return load(input, password, null, null, MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #18
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. The given input stream is copied to the memory to enable random access to the
 * pdf. Unrestricted main memory will be used for buffering PDF streams.
 *
 * @param input stream that contains the document. Don't forget to close it after loading.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * 
 * @return loaded document
 * 
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, String password, InputStream keyStore, String alias)
        throws IOException
{
    return load(input, password, keyStore, alias, MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #19
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. Depending on the memory settings parameter the given input stream is either
 * copied to main memory or to a temporary file to enable random access to the pdf.
 *
 * @param input stream that contains the document. Don't forget to close it after loading.
 * @param password password to be used for decryption
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, String password, MemoryUsageSetting memUsageSetting)
        throws IOException
{
    return load(input, password, null, null, memUsageSetting);
}
 
Example #20
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. The given input stream is copied to the memory to enable random access to the
 * pdf. Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param input stream that contains the document. Don't forget to close it after loading.
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the PDF required a non-empty password.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input) throws IOException
{
    return load(input, "", null, null, MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #21
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param input byte array that contains the document.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(byte[] input, String password, InputStream keyStore, 
        String alias) throws IOException
{
    return load(input, password, keyStore, alias, MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #22
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Merge the list of source documents, saving the result in the destination file.
 *
 * @throws IOException If there is an error saving the document.
 * @deprecated use {@link #mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting) }
 */
@Deprecated
public void mergeDocuments() throws IOException
{
    mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #23
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param file file to be loaded
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * 
 * @return loaded document
 * 
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file, String password, InputStream keyStore, String alias)
throws IOException
{
    return load(file, password, keyStore, alias, MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #24
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF.
 * 
 * @param file file to be loaded
 * @param password password to be used for decryption
 * @param memUsageSetting defines how memory is used for buffering PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file, String password, MemoryUsageSetting memUsageSetting)
        throws IOException
{
    return load(file, password, null, null, memUsageSetting);
}
 
Example #25
Source File: Splitter.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Set the memory setting.
 * 
 * @param memoryUsageSetting 
 */
public void setMemoryUsageSetting(MemoryUsageSetting memoryUsageSetting)
{
    this.memoryUsageSetting = memoryUsageSetting;
}
 
Example #26
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param file file to be loaded
 * @param password password to be used for decryption
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file, String password)
        throws IOException
{
    return load(file, password, null, null, MemoryUsageSetting.setupMainMemoryOnly());
}
 
Example #27
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF.
 * 
 * @param file file to be loaded
 * @param memUsageSetting defines how memory is used for buffering PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the file required a non-empty password.
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file, MemoryUsageSetting memUsageSetting)
        throws IOException
{
    return load(file, "", null, null, memUsageSetting);
}
 
Example #28
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param file file to be loaded
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the file required a non-empty password.
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file) throws IOException
{
    return load(file, "", MemoryUsageSetting.setupMainMemoryOnly());
}