Search in sources :

Example 11 with PDStream

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

the class PDDocument method importPage.

/**
 * This will import and copy the contents from another location. Currently the content stream is
 * stored in a scratch file. The scratch file is associated with the document. If you are adding
 * a page to this document from another document and want to copy the contents to this
 * document's scratch file then use this method otherwise just use the {@link #addPage addPage()}
 * method.
 * <p>
 * Unlike {@link #addPage addPage()}, this method creates a new PDPage object. If your page has
 * annotations, and if these link to pages not in the target document, then the target document
 * might become huge. What you need to do is to delete page references of such annotations. See
 * <a href="http://stackoverflow.com/a/35477351/535646">here</a> for how to do this.
 * <p>
 * Inherited (global) resources are ignored because these can contain resources not needed for
 * this page which could bloat your document, see
 * <a href="https://issues.apache.org/jira/browse/PDFBOX-28">PDFBOX-28</a> and related issues.
 * If you need them, call <code>importedPage.setResources(page.getResources());</code>
 * <p>
 * This method should only be used to import a page from a loaded document, not from a generated
 * document because these can contain unfinished parts, e.g. font subsetting information.
 *
 * @param page The page to import.
 * @return The page that was imported.
 *
 * @throws IOException If there is an error copying the page.
 */
public PDPage importPage(PDPage page) throws IOException {
    PDPage importedPage = new PDPage(new COSDictionary(page.getCOSObject()), resourceCache);
    PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE);
    importedPage.setContents(dest);
    addPage(importedPage);
    importedPage.setCropBox(page.getCropBox());
    importedPage.setMediaBox(page.getMediaBox());
    importedPage.setRotation(page.getRotation());
    if (page.getResources() != null && !page.getCOSObject().containsKey(COSName.RESOURCES)) {
        Log.w("PdfBox-Android", "inherited resources of source document are not imported to destination page");
        Log.w("PdfBox-Android", "call importedPage.setResources(page.getResources()) to do this");
    }
    return importedPage;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream)

Example 12 with PDStream

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

the class PDXObject method createXObject.

/**
 * Creates a new XObject instance of the appropriate type for the COS stream.
 *
 * @param base The stream which is wrapped by this XObject.
 * @param resources
 * @return A new XObject instance.
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public static PDXObject createXObject(COSBase base, PDResources resources) throws IOException {
    if (base == null) {
        // TODO throw an exception?
        return null;
    }
    if (!(base instanceof COSStream)) {
        throw new IOException("Unexpected object type: " + base.getClass().getName());
    }
    COSStream stream = (COSStream) base;
    String subtype = stream.getNameAsString(COSName.SUBTYPE);
    if (COSName.IMAGE.getName().equals(subtype)) {
        return new PDImageXObject(new PDStream(stream), resources);
    } else if (COSName.FORM.getName().equals(subtype)) {
        ResourceCache cache = resources != null ? resources.getResourceCache() : null;
        COSDictionary group = (COSDictionary) stream.getDictionaryObject(COSName.GROUP);
        if (group != null && COSName.TRANSPARENCY.equals(group.getCOSName(COSName.S))) {
            return new PDTransparencyGroup(stream, cache);
        }
        return new PDFormXObject(stream, cache);
    } else if (COSName.PS.getName().equals(subtype)) {
        return new PDPostScriptXObject(stream);
    } else {
        throw new IOException("Invalid XObject Subtype: " + subtype);
    }
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSStream(com.tom_roush.pdfbox.cos.COSStream) PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) IOException(java.io.IOException) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream) ResourceCache(com.tom_roush.pdfbox.pdmodel.ResourceCache)

Example 13 with PDStream

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

the class PDOutputIntent method configureOutputProfile.

private PDStream configureOutputProfile(PDDocument doc, InputStream colorProfile) throws IOException {
    // ICC_Profile icc = ICC_Profile.getInstance(colorProfile);
    // PDStream stream = new PDStream(doc, new ByteArrayInputStream(icc.getData()), COSName.FLATE_DECODE);
    // stream.getCOSObject().setInt(COSName.N, icc.getNumComponents()); TODO: PdfBox-Android
    PDStream stream = new PDStream(doc, colorProfile, COSName.FLATE_DECODE);
    stream.getStream().setInt(COSName.N, 3);
    return stream;
}
Also used : PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream)

Example 14 with PDStream

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

the class LayerUtility method importPageAsForm.

/**
 * Imports a page from some PDF file as a Form XObject so it can be placed on another page
 * in the target document.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before invoking the Form XObject to
 * make sure that the graphics state is reset.
 *
 * @param sourceDoc the source PDF document that contains the page to be copied
 * @param page the page in the source PDF document to be copied
 * @return a Form XObject containing the original page's content
 * @throws IOException if an I/O error occurs
 */
public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException {
    importOcProperties(sourceDoc);
    PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE);
    PDFormXObject form = new PDFormXObject(newStream);
    // Copy resources
    PDResources pageRes = page.getResources();
    PDResources formRes = new PDResources();
    cloner.cloneMerge(pageRes, formRes);
    form.setResources(formRes);
    // Transfer some values from page to form
    transferDict(page.getCOSObject(), form.getCOSObject(), PAGE_TO_FORM_FILTER, true);
    Matrix matrix = form.getMatrix();
    AffineTransform at = matrix.createAffineTransform();
    PDRectangle mediaBox = page.getMediaBox();
    PDRectangle cropBox = page.getCropBox();
    PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox);
    // Handle the /Rotation entry on the page dict
    int rotation = page.getRotation();
    // Transform to FOP's user space
    // at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
    at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(), mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
    switch(rotation) {
        case 90:
            at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
            at.translate(0, viewBox.getWidth());
            at.rotate(-Math.PI / 2.0);
            break;
        case 180:
            at.translate(viewBox.getWidth(), viewBox.getHeight());
            at.rotate(-Math.PI);
            break;
        case 270:
            at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
            at.translate(viewBox.getHeight(), 0);
            at.rotate(-Math.PI * 1.5);
            break;
        default:
    }
    // Compensate for Crop Boxes not starting at 0,0
    at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
    if (!at.isIdentity()) {
        form.setMatrix(at);
    }
    BoundingBox bbox = new BoundingBox();
    bbox.setLowerLeftX(viewBox.getLowerLeftX());
    bbox.setLowerLeftY(viewBox.getLowerLeftY());
    bbox.setUpperRightX(viewBox.getUpperRightX());
    bbox.setUpperRightY(viewBox.getUpperRightY());
    form.setBBox(new PDRectangle(bbox));
    return form;
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) BoundingBox(com.tom_roush.fontbox.util.BoundingBox) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream)

Example 15 with PDStream

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

the class PDCIDFontType2Embedder method buildCIDToGIDMap.

private void buildCIDToGIDMap(Map<Integer, Integer> cidToGid) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int cidMax = Collections.max(cidToGid.keySet());
    for (int i = 0; i <= cidMax; i++) {
        int gid;
        if (cidToGid.containsKey(i)) {
            gid = cidToGid.get(i);
        } else {
            gid = 0;
        }
        out.write(new byte[] { (byte) (gid >> 8 & 0xff), (byte) (gid & 0xff) });
    }
    InputStream input = new ByteArrayInputStream(out.toByteArray());
    PDStream stream = new PDStream(document, input, COSName.FLATE_DECODE);
    cidFont.setItem(COSName.CID_TO_GID_MAP, stream);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream)

Aggregations

PDStream (com.tom_roush.pdfbox.pdmodel.common.PDStream)18 COSStream (com.tom_roush.pdfbox.cos.COSStream)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 COSArray (com.tom_roush.pdfbox.cos.COSArray)3 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)3 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)3 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)3 IOException (java.io.IOException)3 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)2 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)2 COSName (com.tom_roush.pdfbox.cos.COSName)2 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)2 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TTFParser (com.tom_roush.fontbox.ttf.TTFParser)1 BoundingBox (com.tom_roush.fontbox.util.BoundingBox)1 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)1 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)1