Search in sources :

Example 91 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class PageExtractor method extract.

/**
 * This will take a document and extract the desired pages into a new
 * document.  Both startPage and endPage are included in the extracted
 * document.  If the endPage is greater than the number of pages in the
 * source document, it will go to the end of the document.  If startPage is
 * less than 1, it'll start with page 1.  If startPage is greater than
 * endPage or greater than the number of pages in the source document, a
 * blank document will be returned.
 *
 * @return The extracted document
 * @throws IOException If there is an IOError
 */
public PDDocument extract() throws IOException {
    PDDocument extractedDocument = new PDDocument();
    extractedDocument.setDocumentInformation(sourceDocument.getDocumentInformation());
    extractedDocument.getDocumentCatalog().setViewerPreferences(sourceDocument.getDocumentCatalog().getViewerPreferences());
    for (int i = startPage; i <= endPage; i++) {
        PDPage page = sourceDocument.getPage(i - 1);
        PDPage imported = extractedDocument.importPage(page);
        imported.setCropBox(page.getCropBox());
        imported.setMediaBox(page.getMediaBox());
        imported.setResources(page.getResources());
        imported.setRotation(page.getRotation());
    }
    return extractedDocument;
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument)

Example 92 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class Splitter method createNewDocument.

/**
 * Create a new document to write the split contents to.
 *
 * @return the newly created PDDocument.
 * @throws IOException If there is an problem creating the new document.
 */
protected PDDocument createNewDocument() throws IOException {
    PDDocument document = memoryUsageSetting == null ? new PDDocument() : new PDDocument(memoryUsageSetting);
    document.getDocument().setVersion(getSourceDocument().getVersion());
    document.setDocumentInformation(getSourceDocument().getDocumentInformation());
    document.getDocumentCatalog().setViewerPreferences(getSourceDocument().getDocumentCatalog().getViewerPreferences());
    return document;
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument)

Example 93 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class Overlay method close.

/**
 * Close all input documents which were used for the overlay and opened by this class.
 *
 * @throws IOException if something went wrong
 */
@Override
public void close() throws IOException {
    if (defaultOverlay != null) {
        defaultOverlay.close();
    }
    if (firstPageOverlay != null) {
        firstPageOverlay.close();
    }
    if (lastPageOverlay != null) {
        lastPageOverlay.close();
    }
    if (allPagesOverlay != null) {
        allPagesOverlay.close();
    }
    if (oddPageOverlay != null) {
        oddPageOverlay.close();
    }
    if (evenPageOverlay != null) {
        evenPageOverlay.close();
    }
    for (PDDocument doc : openDocuments) {
        doc.close();
    }
    openDocuments.clear();
    specificPageOverlayPage.clear();
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument)

Example 94 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class PDFMergerUtility method legacyMergeDocuments.

/**
 * 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.
 */
private void legacyMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException {
    PDDocument destination = null;
    if (sources != null && sources.size() > 0) {
        // Make sure that:
        // - first Exception is kept
        // - destination is closed
        // - all PDDocuments are closed
        // - all FileInputStreams are closed
        // - there's a way to see which errors occurred
        List<PDDocument> tobeclosed = new ArrayList<PDDocument>();
        try {
            MemoryUsageSetting partitionedMemSetting = memUsageSetting != null ? memUsageSetting.getPartitionedCopy(sources.size() + 1) : MemoryUsageSetting.setupMainMemoryOnly();
            destination = new PDDocument(partitionedMemSetting);
            for (Object sourceObject : sources) {
                PDDocument sourceDoc = null;
                if (sourceObject instanceof File) {
                    sourceDoc = PDDocument.load((File) sourceObject, partitionedMemSetting);
                } else {
                    sourceDoc = PDDocument.load((InputStream) sourceObject, partitionedMemSetting);
                }
                tobeclosed.add(sourceDoc);
                appendDocument(destination, sourceDoc);
            }
            // optionally set meta data
            if (destinationDocumentInformation != null) {
                destination.setDocumentInformation(destinationDocumentInformation);
            }
            if (destinationMetadata != null) {
                destination.getDocumentCatalog().setMetadata(destinationMetadata);
            }
            if (destinationStream == null) {
                destination.save(destinationFileName);
            } else {
                destination.save(destinationStream);
            }
        } finally {
            if (destination != null) {
                IOUtils.closeAndLogException(destination, "PDDocument", null);
            }
            for (PDDocument doc : tobeclosed) {
                IOUtils.closeAndLogException(doc, "PDDocument", null);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ArrayList(java.util.ArrayList) MemoryUsageSetting(com.tom_roush.pdfbox.io.MemoryUsageSetting) COSObject(com.tom_roush.pdfbox.cos.COSObject) File(java.io.File)

Example 95 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class COSWriter method write.

/**
 * This will write the pdf document.
 *
 * @throws IOException If an error occurs while generating the data.
 * @param doc The document to write.
 */
public void write(COSDocument doc) throws IOException {
    PDDocument pdDoc = new PDDocument(doc);
    write(pdDoc);
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)137 File (java.io.File)80 Test (org.junit.Test)69 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)22 InputStream (java.io.InputStream)21 Bitmap (android.graphics.Bitmap)18 IOException (java.io.IOException)14 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)11 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)11 ArrayList (java.util.ArrayList)11 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)9 FileOutputStream (java.io.FileOutputStream)9 COSArray (com.tom_roush.pdfbox.cos.COSArray)8 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)7 Paint (android.graphics.Paint)6