Search in sources :

Example 26 with COSName

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

the class PDStructureElement method attributeChanged.

/**
 * Updates the revision number for the given attribute object.
 *
 * @param attributeObject the attribute object
 */
public void attributeChanged(PDAttributeObject attributeObject) {
    COSName key = COSName.A;
    COSBase a = this.getCOSObject().getDictionaryObject(key);
    if (a instanceof COSArray) {
        COSArray array = (COSArray) a;
        for (int i = 0; i < array.size(); i++) {
            COSBase entry = array.getObject(i);
            if (entry.equals(attributeObject.getCOSObject())) {
                COSBase next = array.get(i + 1);
                if (next instanceof COSInteger) {
                    array.set(i + 1, COSInteger.get(this.getRevisionNumber()));
                }
            }
        }
    } else {
        COSArray array = new COSArray();
        array.add(a);
        array.add(COSInteger.get(this.getRevisionNumber()));
        this.getCOSObject().setItem(key, array);
    }
}
Also used : COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 27 with COSName

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

the class PDOptionalContentGroup method getRenderState.

// TODO Add support for "Intent"
/**
 * @param destination to be rendered
 * @return state or null if undefined
 */
public RenderState getRenderState(RenderDestination destination) {
    COSName state = null;
    COSDictionary usage = (COSDictionary) dict.getDictionaryObject("Usage");
    if (usage != null) {
        if (RenderDestination.PRINT.equals(destination)) {
            COSDictionary print = (COSDictionary) usage.getDictionaryObject("Print");
            state = print == null ? null : (COSName) print.getDictionaryObject("PrintState");
        } else if (RenderDestination.VIEW.equals(destination)) {
            COSDictionary view = (COSDictionary) usage.getDictionaryObject("View");
            state = view == null ? null : (COSName) view.getDictionaryObject("ViewState");
        }
        // Fallback to export
        if (state == null) {
            COSDictionary export = (COSDictionary) usage.getDictionaryObject("Export");
            state = export == null ? null : (COSName) export.getDictionaryObject("ExportState");
        }
    }
    return state == null ? null : RenderState.valueOf(state);
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary)

Example 28 with COSName

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

the class PDCheckBox method getOnValue.

/**
 * Get the value which sets the check box to the On state.
 *
 * <p>The On value should be 'Yes' but other values are possible
 * so we need to look for that. On the other hand the Off value shall
 * always be 'Off'. If not set or not part of the normal appearance keys
 * 'Off' is the default</p>
 *
 * @return the value setting the check box to the On state.
 *          If an empty string is returned there is no appearance definition.
 */
public String getOnValue() {
    PDAnnotationWidget widget = this.getWidgets().get(0);
    PDAppearanceDictionary apDictionary = widget.getAppearance();
    String onValue = "";
    if (apDictionary != null) {
        PDAppearanceEntry normalAppearance = apDictionary.getNormalAppearance();
        if (normalAppearance != null) {
            Set<COSName> entries = normalAppearance.getSubDictionary().keySet();
            for (COSName entry : entries) {
                if (COSName.Off.compareTo(entry) != 0) {
                    onValue = entry.getName();
                }
            }
        }
    }
    return onValue;
}
Also used : PDAppearanceEntry(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry) PDAppearanceDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)

Example 29 with COSName

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

the class PDDefaultAppearanceString method processSetFont.

/**
 * Process the set font and font size operator.
 *
 * @param operands the font name and size
 * @throws IOException in case there are missing operators or the font is not within the resources
 */
private void processSetFont(List<COSBase> operands) throws IOException {
    if (operands.size() < 2) {
        throw new IOException("Missing operands for set font operator " + Arrays.toString(operands.toArray()));
    }
    COSBase base0 = operands.get(0);
    COSBase base1 = operands.get(1);
    if (!(base0 instanceof COSName)) {
        return;
    }
    if (!(base1 instanceof COSNumber)) {
        return;
    }
    COSName fontName = (COSName) base0;
    PDFont font = defaultResources.getFont(fontName);
    float fontSize = ((COSNumber) base1).floatValue();
    // todo: handle cases where font == null with special mapping logic (see PDFBOX-2661)
    if (font == null) {
        throw new IOException("Could not find font: /" + fontName.getName());
    }
    setFontName(fontName);
    setFont(font);
    setFontSize(fontSize);
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) COSName(com.tom_roush.pdfbox.cos.COSName) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 30 with COSName

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

the class PDFTemplateCreator method buildPDF.

/**
 * Build a PDF with a visible signature step by step, and return it as a stream.
 *
 * @param properties
 * @return InputStream
 * @throws IOException
 */
public InputStream buildPDF(PDVisibleSignDesigner properties) throws IOException {
    Log.i("PdfBox-Android", "pdf building has been started");
    PDFTemplateStructure pdfStructure = pdfBuilder.getStructure();
    // we create array of [Text, ImageB, ImageC, ImageI]
    pdfBuilder.createProcSetArray();
    // create page
    pdfBuilder.createPage(properties);
    PDPage page = pdfStructure.getPage();
    // create template
    pdfBuilder.createTemplate(page);
    PDDocument template = pdfStructure.getTemplate();
    // create /AcroForm
    pdfBuilder.createAcroForm(template);
    PDAcroForm acroForm = pdfStructure.getAcroForm();
    // AcroForm contains signature fields
    pdfBuilder.createSignatureField(acroForm);
    PDSignatureField pdSignatureField = pdfStructure.getSignatureField();
    // create signature
    // TODO
    // The line below has no effect with the CreateVisibleSignature example.
    // The signature field is needed as a "holder" for the /AP tree,
    // but the /P and /V PDSignatureField entries are ignored by PDDocument.addSignature
    pdfBuilder.createSignature(pdSignatureField, page, "");
    // that is /AcroForm/DR entry
    pdfBuilder.createAcroFormDictionary(acroForm, pdSignatureField);
    // create AffineTransform
    pdfBuilder.createAffineTransform(properties.getTransform());
    AffineTransform transform = pdfStructure.getAffineTransform();
    // rectangle, formatter, image. /AcroForm/DR/XObject contains that form
    pdfBuilder.createSignatureRectangle(pdSignatureField, properties);
    pdfBuilder.createFormatterRectangle(properties.getFormatterRectangleParameters());
    PDRectangle bbox = pdfStructure.getFormatterRectangle();
    pdfBuilder.createSignatureImage(template, properties.getImage());
    // create form stream, form and  resource.
    pdfBuilder.createHolderFormStream(template);
    PDStream holderFormStream = pdfStructure.getHolderFormStream();
    pdfBuilder.createHolderFormResources();
    PDResources holderFormResources = pdfStructure.getHolderFormResources();
    pdfBuilder.createHolderForm(holderFormResources, holderFormStream, bbox);
    // that is /AP entry the appearance dictionary.
    pdfBuilder.createAppearanceDictionary(pdfStructure.getHolderForm(), pdSignatureField);
    // inner form stream, form and resource (holder form contains inner form)
    pdfBuilder.createInnerFormStream(template);
    pdfBuilder.createInnerFormResource();
    PDResources innerFormResource = pdfStructure.getInnerFormResources();
    pdfBuilder.createInnerForm(innerFormResource, pdfStructure.getInnerFormStream(), bbox);
    PDFormXObject innerForm = pdfStructure.getInnerForm();
    // inner form must be in the holder form as we wrote
    pdfBuilder.insertInnerFormToHolderResources(innerForm, holderFormResources);
    // Image form is in this structure: /AcroForm/DR/FRM/Resources/XObject/n2
    pdfBuilder.createImageFormStream(template);
    PDStream imageFormStream = pdfStructure.getImageFormStream();
    pdfBuilder.createImageFormResources();
    PDResources imageFormResources = pdfStructure.getImageFormResources();
    pdfBuilder.createImageForm(imageFormResources, innerFormResource, imageFormStream, bbox, transform, pdfStructure.getImage());
    pdfBuilder.createBackgroundLayerForm(innerFormResource, bbox);
    // now inject procSetArray
    pdfBuilder.injectProcSetArray(innerForm, page, innerFormResource, imageFormResources, holderFormResources, pdfStructure.getProcSet());
    COSName imageFormName = pdfStructure.getImageFormName();
    COSName imageName = pdfStructure.getImageName();
    COSName innerFormName = pdfStructure.getInnerFormName();
    // now create Streams of AP
    pdfBuilder.injectAppearanceStreams(holderFormStream, imageFormStream, imageFormStream, imageFormName, imageName, innerFormName, properties);
    pdfBuilder.createVisualSignature(template);
    pdfBuilder.createWidgetDictionary(pdSignatureField, holderFormResources);
    InputStream in = getVisualSignatureAsStream(pdfStructure.getVisualSignature());
    Log.i("PdfBox-Android", "stream returning started, size= " + in.available());
    // we must close the document
    template.close();
    // return result of the stream
    return in;
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSName(com.tom_roush.pdfbox.cos.COSName) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) PDSignatureField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDSignatureField) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream)

Aggregations

COSName (com.tom_roush.pdfbox.cos.COSName)83 COSBase (com.tom_roush.pdfbox.cos.COSBase)50 COSArray (com.tom_roush.pdfbox.cos.COSArray)27 IOException (java.io.IOException)24 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)11 COSString (com.tom_roush.pdfbox.cos.COSString)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)9 COSStream (com.tom_roush.pdfbox.cos.COSStream)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)5 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)5 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)5 PDXObject (com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)4 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)4 OutputStream (java.io.OutputStream)4 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)3