Search in sources :

Example 1 with PDPage

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

the class Splitter method processPage.

/**
 * Interface to start processing a new page.
 *
 * @param page The page that is about to get processed.
 *
 * @throws IOException If there is an error creating the new document.
 */
protected void processPage(PDPage page) throws IOException {
    createNewDocumentIfNecessary();
    PDPage imported = getDestinationDocument().importPage(page);
    imported.setResources(page.getResources());
    // remove page links to avoid copying not needed resources
    processAnnotations(imported);
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage)

Example 2 with PDPage

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

the class Overlay method getLayoutPage.

private LayoutPage getLayoutPage(PDDocument doc) throws IOException {
    PDPage page = doc.getPage(0);
    COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS);
    PDResources resources = page.getResources();
    if (resources == null) {
        resources = new PDResources();
    }
    return new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject(), page.getRotation());
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

Example 3 with PDPage

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

the class Overlay method processPages.

private void processPages(PDDocument document) throws IOException {
    int pageCounter = 0;
    for (PDPage page : document.getPages()) {
        pageCounter++;
        COSDictionary pageDictionary = page.getCOSObject();
        COSBase originalContent = pageDictionary.getDictionaryObject(COSName.CONTENTS);
        COSArray newContentArray = new COSArray();
        LayoutPage layoutPage = getLayoutPage(pageCounter, document.getNumberOfPages());
        if (layoutPage == null) {
            continue;
        }
        switch(position) {
            case FOREGROUND:
                // save state
                newContentArray.add(createStream("q\n"));
                addOriginalContent(originalContent, newContentArray);
                // restore state
                newContentArray.add(createStream("Q\n"));
                // overlay content last
                overlayPage(page, layoutPage, newContentArray);
                break;
            case BACKGROUND:
                // overlay content first
                overlayPage(page, layoutPage, newContentArray);
                addOriginalContent(originalContent, newContentArray);
                break;
            default:
                throw new IOException("Unknown type of position:" + position);
        }
        pageDictionary.setItem(COSName.CONTENTS, newContentArray);
    }
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 4 with PDPage

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

the class Overlay method getLayoutPages.

private Map<Integer, LayoutPage> getLayoutPages(PDDocument doc) throws IOException {
    int numberOfPages = doc.getNumberOfPages();
    Map<Integer, LayoutPage> layoutPages = new HashMap<Integer, Overlay.LayoutPage>(numberOfPages);
    for (int i = 0; i < numberOfPages; i++) {
        PDPage page = doc.getPage(i);
        COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
        }
        layoutPages.put(i, new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject(), page.getRotation()));
    }
    return layoutPages;
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) HashMap(java.util.HashMap) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

Example 5 with PDPage

use of com.tom_roush.pdfbox.pdmodel.PDPage 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)

Aggregations

PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)55 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)24 File (java.io.File)23 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)13 Test (org.junit.Test)13 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)11 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)10 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 IOException (java.io.IOException)7 Bitmap (android.graphics.Bitmap)6 COSArray (com.tom_roush.pdfbox.cos.COSArray)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)5 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)5 COSBase (com.tom_roush.pdfbox.cos.COSBase)4 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)4 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)4 Paint (android.graphics.Paint)3 COSName (com.tom_roush.pdfbox.cos.COSName)3