Search in sources :

Example 1 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class PDAbstractContentStream method drawImage.

/**
 * Draw an inline image at the x,y coordinates and a certain width and height.
 *
 * @param inlineImage The inline image to draw.
 * @param x The x-coordinate to draw the inline image.
 * @param y The y-coordinate to draw the inline image.
 * @param width The width of the inline image to draw.
 * @param height The height of the inline image to draw.
 *
 * @throws IOException If there is an error writing to the stream.
 * @throws IllegalStateException If the method was called within a text block.
 */
public void drawImage(PDInlineImage inlineImage, float x, float y, float width, float height) throws IOException {
    if (inTextMode) {
        throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
    }
    saveGraphicsState();
    transform(new Matrix(width, 0, 0, height, x, y));
    // create the image dictionary
    StringBuilder sb = new StringBuilder();
    sb.append(OperatorName.BEGIN_INLINE_IMAGE);
    sb.append("\n /W ");
    sb.append(inlineImage.getWidth());
    sb.append("\n /H ");
    sb.append(inlineImage.getHeight());
    sb.append("\n /CS ");
    sb.append("/");
    sb.append(inlineImage.getColorSpace().getName());
    if (inlineImage.getDecode() != null && inlineImage.getDecode().size() > 0) {
        sb.append("\n /D ");
        sb.append("[");
        for (COSBase base : inlineImage.getDecode()) {
            sb.append(((COSNumber) base).intValue());
            sb.append(" ");
        }
        sb.append("]");
    }
    if (inlineImage.isStencil()) {
        sb.append("\n /IM true");
    }
    sb.append("\n /BPC ");
    sb.append(inlineImage.getBitsPerComponent());
    // image dictionary
    write(sb.toString());
    writeLine();
    // binary data
    writeOperator(OperatorName.BEGIN_INLINE_IMAGE_DATA);
    writeBytes(inlineImage.getData());
    writeLine();
    writeOperator(OperatorName.END_INLINE_IMAGE);
    restoreGraphicsState();
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 2 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class PDDocument method assignAcroFormDefaultResource.

private void assignAcroFormDefaultResource(PDAcroForm acroForm, COSDictionary newDict) {
    // read and set/update AcroForm default resource dictionary /DR if available
    COSBase newBase = newDict.getDictionaryObject(COSName.DR);
    if (newBase instanceof COSDictionary) {
        COSDictionary newDR = (COSDictionary) newBase;
        PDResources defaultResources = acroForm.getDefaultResources();
        if (defaultResources == null) {
            acroForm.getCOSObject().setItem(COSName.DR, newDR);
            newDR.setDirect(true);
            newDR.setNeedToBeUpdated(true);
        } else {
            COSDictionary oldDR = defaultResources.getCOSObject();
            COSBase newXObjectBase = newDR.getItem(COSName.XOBJECT);
            COSBase oldXObjectBase = oldDR.getItem(COSName.XOBJECT);
            if (newXObjectBase instanceof COSDictionary && oldXObjectBase instanceof COSDictionary) {
                ((COSDictionary) oldXObjectBase).addAll((COSDictionary) newXObjectBase);
                oldDR.setNeedToBeUpdated(true);
            }
        }
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 3 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class PDDocument method prepareVisibleSignature.

private void prepareVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm, COSDocument visualSignature) {
    // Obtain visual signature object
    boolean annotNotFound = true;
    boolean sigFieldNotFound = true;
    for (COSObject cosObject : visualSignature.getObjects()) {
        if (!annotNotFound && !sigFieldNotFound) {
            break;
        }
        COSBase base = cosObject.getObject();
        if (base instanceof COSDictionary) {
            COSDictionary cosBaseDict = (COSDictionary) base;
            // Search for signature annotation
            COSBase type = cosBaseDict.getDictionaryObject(COSName.TYPE);
            if (annotNotFound && COSName.ANNOT.equals(type)) {
                assignSignatureRectangle(signatureField, cosBaseDict);
                annotNotFound = false;
            }
            // Search for signature field
            COSBase fieldType = cosBaseDict.getDictionaryObject(COSName.FT);
            COSBase apDict = cosBaseDict.getDictionaryObject(COSName.AP);
            if (sigFieldNotFound && COSName.SIG.equals(fieldType) && apDict instanceof COSDictionary) {
                assignAppearanceDictionary(signatureField, (COSDictionary) apDict);
                assignAcroFormDefaultResource(acroForm, cosBaseDict);
                sigFieldNotFound = false;
            }
        }
    }
    if (annotNotFound || sigFieldNotFound) {
        throw new IllegalArgumentException("Template is missing required objects");
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 4 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class PDDocument method getDocumentCatalog.

/**
 * This will get the document CATALOG. This is guaranteed to not return null.
 *
 * @return The documents /Root dictionary
 */
public PDDocumentCatalog getDocumentCatalog() {
    if (documentCatalog == null) {
        COSDictionary trailer = document.getTrailer();
        COSBase dictionary = trailer.getDictionaryObject(COSName.ROOT);
        if (dictionary instanceof COSDictionary) {
            documentCatalog = new PDDocumentCatalog(this, (COSDictionary) dictionary);
        } else {
            documentCatalog = new PDDocumentCatalog(this);
        }
    }
    return documentCatalog;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 5 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class PDFMergerUtility method acroFormLegacyMode.

/*
     * Merge the contents of the source form into the destination form for the
     * destination file.
     *
     * @param cloner the object cloner for the destination document
     * @param destAcroForm the destination form
     * @param srcAcroForm the source form
     * @throws IOException If an error occurs while adding the field.
     */
private void acroFormLegacyMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException {
    List<PDField> srcFields = srcAcroForm.getFields();
    COSArray destFields;
    if (srcFields != null && !srcFields.isEmpty()) {
        // if a form is merged multiple times using PDFBox the newly generated
        // fields starting with dummyFieldName may already exist. We need to determine the last unique
        // number used and increment that.
        final String prefix = "dummyFieldName";
        final int prefixLength = prefix.length();
        for (PDField destField : destAcroForm.getFieldTree()) {
            String fieldName = destField.getPartialName();
            if (fieldName.startsWith(prefix)) {
                nextFieldNum = Math.max(nextFieldNum, Integer.parseInt(fieldName.substring(prefixLength, fieldName.length())) + 1);
            }
        }
        // get the destinations root fields. Could be that the entry doesn't exist
        // or is of wrong type
        COSBase base = destAcroForm.getCOSObject().getItem(COSName.FIELDS);
        if (base instanceof COSArray) {
            destFields = (COSArray) base;
        } else {
            destFields = new COSArray();
        }
        for (PDField srcField : srcAcroForm.getFields()) {
            COSDictionary dstField = (COSDictionary) cloner.cloneForNewDocument(srcField.getCOSObject());
            // to prevent merge conflicts.
            if (destAcroForm.getField(srcField.getFullyQualifiedName()) != null) {
                dstField.setString(COSName.T, prefix + nextFieldNum++);
            }
            destFields.add(dstField);
        }
        destAcroForm.getCOSObject().setItem(COSName.FIELDS, destFields);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Aggregations

COSBase (com.tom_roush.pdfbox.cos.COSBase)215 COSArray (com.tom_roush.pdfbox.cos.COSArray)108 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)68 COSName (com.tom_roush.pdfbox.cos.COSName)50 COSObject (com.tom_roush.pdfbox.cos.COSObject)42 IOException (java.io.IOException)34 COSString (com.tom_roush.pdfbox.cos.COSString)33 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)29 ArrayList (java.util.ArrayList)29 COSStream (com.tom_roush.pdfbox.cos.COSStream)20 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)14 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)11 HashMap (java.util.HashMap)11 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)10 Map (java.util.Map)10 List (java.util.List)9 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)8 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)7 COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)7 PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)6