Search in sources :

Example 1 with PDResources

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

the class Overlay method overlayPage.

private void overlayPage(PDPage page, LayoutPage layoutPage, COSArray array) throws IOException {
    PDResources resources = page.getResources();
    if (resources == null) {
        resources = new PDResources();
        page.setResources(resources);
    }
    COSName xObjectId = createOverlayXObject(page, layoutPage);
    array.add(createOverlayStream(page, layoutPage, xObjectId));
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

Example 2 with PDResources

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

use of com.tom_roush.pdfbox.pdmodel.PDResources 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 4 with PDResources

use of com.tom_roush.pdfbox.pdmodel.PDResources 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 5 with PDResources

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

the class PDSquigglyAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) getAnnotation();
    PDRectangle rect = annotation.getRectangle();
    float[] pathsArray = annotation.getQuadPoints();
    if (pathsArray == null) {
        return;
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDColor color = annotation.getColor();
    if (color == null || color.getComponents().length == 0) {
        return;
    }
    if (Float.compare(ab.width, 0) == 0) {
        // value found in adobe reader
        ab.width = 1.5f;
    }
    // Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
    // TODO in a class structure this should be overridable
    // this is similar to polyline but different data type
    // all coordinates (unlike painting) are used because I'm lazy
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    for (int i = 0; i < pathsArray.length / 2; ++i) {
        float x = pathsArray[i * 2];
        float y = pathsArray[i * 2 + 1];
        minX = Math.min(minX, x);
        minY = Math.min(minY, y);
        maxX = Math.max(maxX, x);
        maxY = Math.max(maxY, y);
    }
    rect.setLowerLeftX(Math.min(minX - ab.width / 2, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - ab.width / 2, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + ab.width / 2, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width / 2, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        setOpacity(cs, annotation.getConstantOpacity());
        cs.setStrokingColor(color);
        // https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
        for (int i = 0; i < pathsArray.length / 8; ++i) {
            // Adobe uses a fixed pattern that assumes a height of 40, and it transforms to that height
            // horizontally and the same / 1.8 vertically.
            // translation apparently based on bottom left, but slightly different in Adobe
            // TODO what if the annotation is not horizontal?
            float height = pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5];
            cs.transform(new Matrix(height / 40f, 0, 0, height / 40f / 1.8f, pathsArray[i * 8 + 4], pathsArray[i * 8 + 5]));
            // Create form, BBox is mostly fixed, except for the horizontal size which is
            // horizontal size divided by the horizontal transform factor from above
            // (almost)
            PDFormXObject form = new PDFormXObject(createCOSStream());
            form.setBBox(new PDRectangle(-0.5f, -0.5f, (pathsArray[i * 8 + 2] - pathsArray[i * 8]) / height * 40f + 0.5f, 13));
            form.setResources(new PDResources());
            form.setMatrix(AffineTransform.getTranslateInstance(0.5f, 0.5f));
            cs.drawForm(form);
            PDFormContentStream formCS = null;
            try {
                formCS = new PDFormContentStream(form);
                PDTilingPattern pattern = new PDTilingPattern();
                pattern.setBBox(new PDRectangle(0, 0, 10, 12));
                pattern.setXStep(10);
                pattern.setYStep(13);
                pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);
                pattern.setPaintType(PDTilingPattern.PAINT_UNCOLORED);
                PDPatternContentStream patternCS = null;
                try {
                    patternCS = new PDPatternContentStream(pattern);
                    // from Adobe
                    patternCS.setLineCapStyle(1);
                    patternCS.setLineJoinStyle(1);
                    patternCS.setLineWidth(1);
                    patternCS.setMiterLimit(10);
                    patternCS.moveTo(0, 1);
                    patternCS.lineTo(5, 11);
                    patternCS.lineTo(10, 1);
                    patternCS.stroke();
                } finally {
                    IOUtils.closeQuietly(patternCS);
                }
                COSName patternName = form.getResources().add(pattern);
                // PDColorSpace patternColorSpace = new PDPattern(null, PDDeviceRGB.INSTANCE);
                // PDColor patternColor = new PDColor(color.getComponents(), patternName, patternColorSpace);
                // formCS.setNonStrokingColor(patternColor); TODO: PdfBox-Android
                // With Adobe, the horizontal size is slightly different, don't know why
                formCS.addRect(0, 0, (pathsArray[i * 8 + 2] - pathsArray[i * 8]) / height * 40f, 12);
                formCS.fill();
            } finally {
                IOUtils.closeQuietly(formCS);
            }
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDFormContentStream(com.tom_roush.pdfbox.pdmodel.PDFormContentStream) PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDPatternContentStream(com.tom_roush.pdfbox.pdmodel.PDPatternContentStream) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) Matrix(com.tom_roush.pdfbox.util.Matrix) PDTilingPattern(com.tom_roush.pdfbox.pdmodel.graphics.pattern.PDTilingPattern) COSName(com.tom_roush.pdfbox.cos.COSName) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Aggregations

PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)44 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)13 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)13 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)11 COSName (com.tom_roush.pdfbox.cos.COSName)9 Matrix (com.tom_roush.pdfbox.util.Matrix)9 IOException (java.io.IOException)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 File (java.io.File)7 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)5 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)4 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)4 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)4 PDGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState)4 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)4 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)3 PDStream (com.tom_roush.pdfbox.pdmodel.common.PDStream)3