Example usage for org.apache.pdfbox.cos COSName getName

List of usage examples for org.apache.pdfbox.cos COSName getName

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName getName.

Prototype

public String getName() 

Source Link

Document

This will get the name of this COSName object.

Usage

From source file:pdfimport.pdfbox.operators.Invoke.java

License:Apache License

/**
 * process : Do : Paint the specified XObject (section 4.7).
 * @param operator The operator that is being executed.
 * @param arguments List/*from ww  w  .j  a  v  a2 s  .c om*/
 * @throws IOException If there is an error invoking the sub object.
 */
@Override
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    PageDrawer drawer = (PageDrawer) context;
    PDPage page = drawer.getPage();
    COSName objectName = (COSName) arguments.get(0);
    Map xobjects = drawer.getResources().getXObjects();
    PDXObject xobject = (PDXObject) xobjects.get(objectName.getName());
    if (xobject instanceof PDXObjectImage) {
        drawer.drawImage();
    } else if (xobject instanceof PDXObjectForm) {
        PDXObjectForm form = (PDXObjectForm) xobject;
        COSStream invoke = (COSStream) form.getCOSObject();
        PDResources pdResources = form.getResources();
        if (pdResources == null) {
            pdResources = page.findResources();
        }
        // if there is an optional form matrix, we have to
        // map the form space to the user space
        Matrix matrix = form.getMatrix();
        if (matrix != null) {
            Matrix xobjectCTM = matrix.multiply(context.getGraphicsState().getCurrentTransformationMatrix());
            context.getGraphicsState().setCurrentTransformationMatrix(xobjectCTM);
        }
        getContext().processSubStream(page, pdResources, invoke);
    } else {
        //unknown xobject type
    }

    //invoke named object.
}

From source file:pdfpicmangler.ResolutionAnalyzer.java

License:Open Source License

protected void processOperator(PDFOperator operator, List<COSBase> arguments) throws IOException {
    String operation = operator.getOperation();
    if (INVOKE_OPERATOR.equals(operation)) {
        COSName objectName = (COSName) arguments.get(0);
        Map<String, PDXObject> xobjects = getResources().getXObjects();
        PDXObject xobject = (PDXObject) xobjects.get(objectName.getName());
        if (xobject instanceof PDXObjectImage) {
            PDXObjectImage image = (PDXObjectImage) xobject;
            PDPage page = getCurrentPage();
            int imageWidth = image.getWidth();
            int imageHeight = image.getHeight();
            System.out.println("***************************************************************");

            String imageName = currentPage + objectName.getName();

            System.out.println("Found image [" + imageName + "]");

            Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();

            float imageXScale = ctmNew.getXScale() / 72;
            float imageYScale = ctmNew.getYScale() / 72;
            // size in pixel
            System.out.println("  size: " + imageWidth + " x " + imageHeight + " px");
            // size in page units
            System.out.print("  size_on_page: " + imageXScale + " x " + imageYScale + " in ");
            System.out.println("( = " + (imageXScale * 25.4) + " x " + (imageYScale * 25.4) + " mm)");
            System.out.println(//from  w  w w  .  j a  va  2s.  c o m
                    "  dpi: " + (imageWidth / imageXScale) + " x " + (imageHeight / imageYScale) + " dpi");

            System.out.println("  filters: " + image.getPDStream().getFilters());
            System.out.println("  compressed_size: " + image.getPDStream().getLength());

            COSBase userUnit = page.getCOSDictionary().getDictionaryObject("UserUnit");
            System.out.println("  userunit: " + userUnit);
            System.out.println("***************************************************************");

            float dpiX = imageWidth / imageXScale;
            float dpiY = imageHeight / imageYScale;
            float dpi = 0.5f * (dpiX + dpiY);

            if (Math.abs((dpiX - dpiY) / dpi) > 0.05) {
                System.out.println("warning: resolution of image " + imageName + "is not square: dpiX=" + dpiX
                        + " dpiY=" + dpiY);
            }

            if (resolutions.containsKey(imageName)) {
                float dpiOld = resolutions.get(imageName);
                System.out.println("re-used image name=" + imageName + " dpi=" + dpi + " dpiOld=" + dpiOld);

                if (dpiOld > dpi) {
                    resolutions.put(imageName, dpi);
                }
            } else {
                resolutions.put(imageName, dpi);
            }
        } else if (xobject instanceof PDXObjectForm) {
            // save the graphics state
            getGraphicsStack().push((PDGraphicsState) getGraphicsState().clone());
            PDPage page = getCurrentPage();

            PDXObjectForm form = (PDXObjectForm) xobject;
            PDResources pdResources = form.getResources();
            if (pdResources == null) {
                pdResources = page.findResources();
            }
            // if there is an optional form matrix, we have to
            // map the form space to the user space
            Matrix matrix = form.getMatrix();
            if (matrix != null) {
                Matrix xobjectCTM = matrix.multiply(getGraphicsState().getCurrentTransformationMatrix());
                getGraphicsState().setCurrentTransformationMatrix(xobjectCTM);
            }
            processSubStream(page, pdResources, (COSStream) form.getCOSObject());

            // restore the graphics state
            setGraphicsState((PDGraphicsState) getGraphicsStack().pop());
        }

    } else {
        super.processOperator(operator, arguments);
    }
}

From source file:Project.data.preparation.PrintImageLocation.java

public void processOperator(PDFOperator operator, List arguments) throws IOException {

    String operation = operator.getOperation();
    if (operation.equals("Do")) {
        COSName objectName = (COSName) arguments.get(0);
        Map xobjects = getResources().getXObjects();
        PDXObject xobject = (PDXObject) xobjects.get(objectName.getName());
        if (xobject instanceof PDXObjectImage) {
            try {
                PDXObjectImage image = (PDXObjectImage) xobject;
                PDPage page = getCurrentPage();
                Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
                double rotationInRadians = (page.findRotation() * Math.PI) / 180;

                AffineTransform rotation = new AffineTransform();
                rotation.setToRotation(rotationInRadians);
                AffineTransform rotationInverse = rotation.createInverse();
                Matrix rotationInverseMatrix = new Matrix();
                rotationInverseMatrix.setFromAffineTransform(rotationInverse);
                Matrix rotationMatrix = new Matrix();
                rotationMatrix.setFromAffineTransform(rotation);

                Matrix unrotatedCTM = ctm.multiply(rotationInverseMatrix);
                float xScale = unrotatedCTM.getXScale();
                float yScale = unrotatedCTM.getYScale();
                double[] location = { unrotatedCTM.getXPosition(), unrotatedCTM.getYPosition() };
                double[] size = { image.getWidth(), image.getHeight() };
                location_xy.add(location);
                size_xy.add(size);//from w  ww  .java 2s. c  o m
                setLocation_xy(location_xy);
                setSize_xy(size_xy);
                //                    System.out.println("Found image[" + objectName.getName() + "] "
                //                            + "at " + unrotatedCTM.getXPosition() + "," + unrotatedCTM.getYPosition()
                //                           // + " \t\tsize=" + (xScale / 100f * image.getWidth()) + "," + (yScale / 100f * image.getHeight())
                //                            + " \t\tactual size=" + image.getWidth() + "," + image.getHeight());
            } catch (NoninvertibleTransformException e) {
                throw new WrappedIOException(e);
            }
        }
    } else {
        super.processOperator(operator, arguments);
    }
}

From source file:uk.bl.wa.tika.parser.pdf.pdfbox.PDFParser.java

License:Apache License

private void extractMetadata(PDDocument document, Metadata metadata) throws TikaException {
    PDDocumentInformation info = document.getDocumentInformation();
    metadata.set(PagedText.N_PAGES, document.getNumberOfPages());
    addMetadata(metadata, Metadata.TITLE, info.getTitle());
    addMetadata(metadata, Metadata.AUTHOR, info.getAuthor());
    addMetadata(metadata, Metadata.KEYWORDS, info.getKeywords());
    addMetadata(metadata, "pdf:creator", info.getCreator());
    addMetadata(metadata, "pdf:producer", info.getProducer());
    addMetadata(metadata, Metadata.SUBJECT, info.getSubject());
    addMetadata(metadata, "trapped", info.getTrapped());
    addMetadata(metadata, "created", info.getCreationDate());
    addMetadata(metadata, Metadata.CREATION_DATE, info.getCreationDate());
    Calendar modified = info.getModificationDate();
    addMetadata(metadata, Metadata.LAST_MODIFIED, modified);

    // All remaining metadata is custom
    // Copy this over as-is
    List<String> handledMetadata = Arrays.asList(new String[] { "Author", "Creator", "CreationDate", "ModDate",
            "Keywords", "Producer", "Subject", "Title", "Trapped" });
    if (info.getCOSObject() != null && info.getCOSObject().keySet() != null) {
        for (COSName key : info.getCOSObject().keySet()) {
            String name = key.getName();
            if (!handledMetadata.contains(name)) {
                addMetadata(metadata, name, info.getCOSObject().getDictionaryObject(key));
            }// w ww . j  av  a  2s.c o  m
        }
    }
    // ANJ Extensions:
    //
    //
    // Add other data of interest:
    metadata.set("pdf:version", "" + document.getDocument().getVersion());
    metadata.set("pdf:numPages", "" + document.getNumberOfPages());
    //metadata.set("pdf:cryptoMode", ""+getCryptoModeAsString(reader));
    //metadata.set("pdf:openedWithFullPermissions", ""+reader.isOpenedWithFullPermissions());
    metadata.set("pdf:encrypted", "" + document.isEncrypted());
    //metadata.set("pdf:metadataEncrypted", ""+document.isMetadataEncrypted());
    //metadata.set("pdf:128key", ""+reader.is128Key());
    //metadata.set("pdf:tampered", ""+reader.isTampered());
    try {
        if (document.getDocumentCatalog().getMetadata() != null) {
            XMPMetadata xmp = XMPMetadata.load(document.getDocumentCatalog().getMetadata().exportXMPMetadata());
            // There is a special class for grabbing data in the PDF schema - not sure it will add much here:
            // Could parse xmp:CreatorTool and pdf:Producer etc. etc. out of here.
            XMPSchemaPDF pdfxmp = xmp.getPDFSchema();
            // Added a PDF/A schema class:
            xmp.addXMLNSMapping(XMPSchemaPDFA.NAMESPACE, XMPSchemaPDFA.class);
            XMPSchemaPDFA pdfaxmp = (XMPSchemaPDFA) xmp.getSchemaByClass(XMPSchemaPDFA.class);
            if (pdfaxmp != null) {
                metadata.set("pdfaid:part", pdfaxmp.getPart());
                metadata.set("pdfaid:conformance", pdfaxmp.getConformance());
                String version = "A-" + pdfaxmp.getPart() + pdfaxmp.getConformance().toLowerCase();
                //metadata.set("pdfa:version", version );                    
                metadata.set("pdf:version", version);
            }
            // TODO WARN if this XMP version is inconsistent with document header version?
        }
    } catch (IOException e) {
        log.error("XMP Parsing failed: " + e);
        metadata.set("pdf:metadata-xmp-parse-failed", "" + e);
    }

    // Attempt to determine Adobe extension level, if present:
    COSDictionary root = document.getDocumentCatalog().getCOSObject();
    COSDictionary extensions = (COSDictionary) root.getDictionaryObject(COSName.getPDFName("Extensions"));
    if (extensions != null) {
        for (COSName extName : extensions.keySet()) {
            // If it's an Adobe one, interpret it to determine the extension level:
            if (extName.equals(COSName.getPDFName("ADBE"))) {
                COSDictionary adobeExt = (COSDictionary) extensions.getDictionaryObject(extName);
                if (adobeExt != null) {
                    String baseVersion = adobeExt.getNameAsString(COSName.getPDFName("BaseVersion"));
                    int el = adobeExt.getInt(COSName.getPDFName("ExtensionLevel"));
                    metadata.set("pdf:version", baseVersion + " Adobe Extension Level " + el);
                }
                // TODO WARN if this embedded version is inconsistent with document header version?
            } else {
                // WARN that there is an Extension, but it's not Adobe's, and so is a 'new' format'.
                metadata.set("pdf:foundNonAdobeExtensionName", extName.getName());
            }
        }
    }
    // End Of ANJ Extensions.
}