Search in sources :

Example 1 with PDDocument

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

the class Overlay method overlayDocuments.

/**
 * This will add overlays documents to a document.
 *
 * @param specificPageOverlayDocuments Optional map of overlay documents for specific pages. The
 * page numbers are 1-based. The map must be empty (but not null) if no specific mappings are
 * used.
 *
 * @return The modified input PDF document, which has to be saved and closed by the caller. If
 * the input document was passed by {@link #setInputPDF(PDDocument) setInputPDF(PDDocument)}
 * then it is that object that is returned.
 *
 * @throws IOException if something went wrong
 */
public PDDocument overlayDocuments(Map<Integer, PDDocument> specificPageOverlayDocuments) throws IOException {
    loadPDFs();
    for (Map.Entry<Integer, PDDocument> e : specificPageOverlayDocuments.entrySet()) {
        PDDocument doc = e.getValue();
        if (doc != null) {
            specificPageOverlayPage.put(e.getKey(), getLayoutPage(doc));
        }
    }
    processPages(inputPDFDocument);
    return inputPDFDocument;
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with PDDocument

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

the class Overlay method overlay.

/**
 * This will add overlays to a document.
 *
 * @param specificPageOverlayFile Optional map of overlay files for specific pages. The page
 * numbers are 1-based. The map must be empty (but not null) if no specific mappings are used.
 *
 * @return The modified input PDF document, which has to be saved and closed by the caller. If
 * the input document was passed by {@link #setInputPDF(PDDocument) setInputPDF(PDDocument)}
 * then it is that object that is returned.
 *
 * @throws IOException if something went wrong
 */
public PDDocument overlay(Map<Integer, String> specificPageOverlayFile) throws IOException {
    Map<String, PDDocument> loadedDocuments = new HashMap<String, PDDocument>();
    Map<PDDocument, LayoutPage> layouts = new HashMap<PDDocument, LayoutPage>();
    loadPDFs();
    for (Map.Entry<Integer, String> e : specificPageOverlayFile.entrySet()) {
        PDDocument doc = loadedDocuments.get(e.getValue());
        if (doc == null) {
            doc = loadPDF(e.getValue());
            loadedDocuments.put(e.getValue(), doc);
            layouts.put(doc, getLayoutPage(doc));
        }
        openDocuments.add(doc);
        specificPageOverlayPage.put(e.getKey(), layouts.get(doc));
    }
    processPages(inputPDFDocument);
    return inputPDFDocument;
}
Also used : HashMap(java.util.HashMap) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with PDDocument

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

the class PDFMergerUtility method optimizedMergeDocuments.

private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException {
    PDDocument destination = null;
    try {
        destination = new PDDocument(memUsageSetting);
        PDFCloneUtility cloner = new PDFCloneUtility(destination);
        for (Object sourceObject : sources) {
            PDDocument sourceDoc = null;
            try {
                if (sourceObject instanceof File) {
                    sourceDoc = PDDocument.load((File) sourceObject, memUsageSetting);
                } else {
                    sourceDoc = PDDocument.load((InputStream) sourceObject, memUsageSetting);
                }
                for (PDPage page : sourceDoc.getPages()) {
                    PDPage newPage = new PDPage((COSDictionary) cloner.cloneForNewDocument(page.getCOSObject()));
                    newPage.setCropBox(page.getCropBox());
                    newPage.setMediaBox(page.getMediaBox());
                    newPage.setRotation(page.getRotation());
                    PDResources resources = page.getResources();
                    if (resources != null) {
                        // this is smart enough to just create references for resources that are used on multiple pages
                        newPage.setResources(new PDResources((COSDictionary) cloner.cloneForNewDocument(resources)));
                    } else {
                        newPage.setResources(new PDResources());
                    }
                    destination.addPage(newPage);
                }
            } finally {
                IOUtils.closeQuietly(sourceDoc);
            }
        }
        if (destinationStream == null) {
            destination.save(destinationFileName);
        } else {
            destination.save(destinationStream);
        }
    } finally {
        IOUtils.closeQuietly(destination);
    }
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) COSObject(com.tom_roush.pdfbox.cos.COSObject) File(java.io.File)

Example 4 with PDDocument

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

the class PDFParser method getPDDocument.

/**
 * This will get the PD document that was parsed.  When you are done with
 * this document you must call close() on it to release resources.
 *
 * @return The document at the PD layer.
 *
 * @throws IOException If there is an error getting the document.
 */
public PDDocument getPDDocument() throws IOException {
    PDDocument doc = new PDDocument(getDocument(), source, getAccessPermission());
    doc.setEncryptionDictionary(getEncryption());
    return doc;
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument)

Example 5 with PDDocument

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

the class PDAcroFormTest method setUp.

@Before
public void setUp() {
    document = new PDDocument();
    acroForm = new PDAcroForm(document);
    document.getDocumentCatalog().setAcroForm(acroForm);
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Before(org.junit.Before)

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