Search in sources :

Example 36 with PDRectangle

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

the class AppearanceGeneratorHelper method setAppearanceValue.

/**
 * This is the public method for setting the appearance stream.
 *
 * @param apValue the String value which the appearance should represent
 * @throws IOException If there is an error creating the stream.
 */
public void setAppearanceValue(String apValue) throws IOException {
    value = apValue;
    // see PDFBOX-3911
    if (field instanceof PDTextField && !((PDTextField) field).isMultiline()) {
        value = apValue.replaceAll("\\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]", " ");
    }
    for (PDAnnotationWidget widget : field.getWidgets()) {
        // some fields have the /Da at the widget level if the
        // widgets differ in layout.
        PDDefaultAppearanceString acroFormAppearance = defaultAppearance;
        if (widget.getCOSObject().getDictionaryObject(COSName.DA) != null) {
            defaultAppearance = getWidgetDefaultAppearanceString(widget);
        }
        PDRectangle rect = widget.getRectangle();
        if (rect == null) {
            widget.getCOSObject().removeItem(COSName.AP);
            Log.w("PdfBox-Android", "widget of field " + field.getFullyQualifiedName() + " has no rectangle, no appearance stream created");
            continue;
        }
        PDFormFieldAdditionalActions actions = field.getActions();
        // when it is opened. See FreedomExpressions.pdf for an example of this.
        if (actions == null || actions.getF() == null || widget.getCOSObject().getDictionaryObject(COSName.AP) != null) {
            PDAppearanceDictionary appearanceDict = widget.getAppearance();
            if (appearanceDict == null) {
                appearanceDict = new PDAppearanceDictionary();
                widget.setAppearance(appearanceDict);
            }
            PDAppearanceEntry appearance = appearanceDict.getNormalAppearance();
            // TODO support appearances other than "normal"
            PDAppearanceStream appearanceStream;
            if (isValidAppearanceStream(appearance)) {
                appearanceStream = appearance.getAppearanceStream();
            } else {
                appearanceStream = prepareNormalAppearanceStream(widget);
                appearanceDict.setNormalAppearance(appearanceStream);
            // TODO support appearances other than "normal"
            }
            /*
                 * Adobe Acrobat always recreates the complete appearance stream if there is an appearance characteristics
                 * entry (the widget dictionaries MK entry). In addition if there is no content yet also create the appearance
                 * stream from the entries.
                 *
                 */
            if (widget.getAppearanceCharacteristics() != null || appearanceStream.getContentStream().getLength() == 0) {
                initializeAppearanceContent(widget, appearanceStream);
            }
            setAppearanceContent(widget, appearanceStream);
        }
        // restore the field level appearance
        defaultAppearance = acroFormAppearance;
    }
}
Also used : PDAppearanceEntry(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry) PDFormFieldAdditionalActions(com.tom_roush.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions) PDAppearanceDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 37 with PDRectangle

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

the class AppearanceGeneratorHelper method insertGeneratedListboxSelectionHighlight.

private void insertGeneratedListboxSelectionHighlight(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize) throws IOException {
    List<Integer> indexEntries = ((PDListBox) field).getSelectedOptionsIndex();
    List<String> values = ((PDListBox) field).getValue();
    List<String> options = ((PDListBox) field).getOptionsExportValues();
    if (!values.isEmpty() && !options.isEmpty() && indexEntries.isEmpty()) {
        // create indexEntries from options
        indexEntries = new ArrayList<Integer>();
        for (String v : values) {
            indexEntries.add(options.indexOf(v));
        }
    }
    // The first entry which shall be presented might be adjusted by the optional TI key
    // If this entry is present the first entry to be displayed is the keys value otherwise
    // display starts with the first entry in Opt.
    int topIndex = ((PDListBox) field).getTopIndex();
    float highlightBoxHeight = font.getBoundingBox().getHeight() * fontSize / FONTSCALE;
    // the padding area
    PDRectangle paddingEdge = applyPadding(appearanceStream.getBBox(), 1);
    for (int selectedIndex : indexEntries) {
        contents.setNonStrokingColor(HIGHLIGHT_COLOR[0], HIGHLIGHT_COLOR[1], HIGHLIGHT_COLOR[2]);
        contents.addRect(paddingEdge.getLowerLeftX(), paddingEdge.getUpperRightY() - highlightBoxHeight * (selectedIndex - topIndex + 1) + 2, paddingEdge.getWidth(), highlightBoxHeight);
        contents.fill();
    }
    contents.setNonStrokingColor(0f);
}
Also used : PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 38 with PDRectangle

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

the class AppearanceGeneratorHelper method insertGeneratedCombAppearance.

/**
 * Generate the appearance for comb fields.
 *
 * @param contents the content stream to write to
 * @param appearanceStream the appearance stream used
 * @param font the font to be used
 * @param fontSize the font size to be used
 * @throws IOException
 */
private void insertGeneratedCombAppearance(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize) throws IOException {
    // TODO:    Currently the quadding is not taken into account
    // so the comb is always filled from left to right.
    int maxLen = ((PDTextField) field).getMaxLen();
    int numChars = Math.min(value.length(), maxLen);
    PDRectangle paddingEdge = applyPadding(appearanceStream.getBBox(), 1);
    float combWidth = appearanceStream.getBBox().getWidth() / maxLen;
    float ascentAtFontSize = font.getFontDescriptor().getAscent() / FONTSCALE * fontSize;
    float baselineOffset = paddingEdge.getLowerLeftY() + (appearanceStream.getBBox().getHeight() - ascentAtFontSize) / 2;
    float prevCharWidth = 0f;
    float xOffset = combWidth / 2;
    for (int i = 0; i < numChars; i++) {
        String combString = value.substring(i, i + 1);
        float currCharWidth = font.getStringWidth(combString) / FONTSCALE * fontSize / 2;
        xOffset = xOffset + prevCharWidth / 2 - currCharWidth / 2;
        contents.newLineAtOffset(xOffset, baselineOffset);
        contents.showText(combString);
        baselineOffset = 0;
        prevCharWidth = currCharWidth;
        xOffset = combWidth;
    }
}
Also used : PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 39 with PDRectangle

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

the class PDAcroForm method resolveNeedsTranslation.

/**
 * Check if there is a translation needed to place the annotations content.
 *
 * @param appearanceStream
 * @return the need for a translation transformation.
 */
private boolean resolveNeedsTranslation(PDAppearanceStream appearanceStream) {
    boolean needsTranslation = true;
    PDResources resources = appearanceStream.getResources();
    if (resources != null && resources.getXObjectNames().iterator().hasNext()) {
        Iterator<COSName> xObjectNames = resources.getXObjectNames().iterator();
        while (xObjectNames.hasNext()) {
            try {
                // if the BBox of the PDFormXObject does not start at 0,0
                // there is no need do translate as this is done by the BBox definition.
                PDXObject xObject = resources.getXObject(xObjectNames.next());
                if (xObject instanceof PDFormXObject) {
                    PDRectangle bbox = ((PDFormXObject) xObject).getBBox();
                    float llX = bbox.getLowerLeftX();
                    float llY = bbox.getLowerLeftY();
                    if (Float.compare(llX, 0) != 0 && Float.compare(llY, 0) != 0) {
                        needsTranslation = false;
                    }
                }
            } catch (IOException e) {
            // we can safely ignore the exception here
            // as this might only cause a misplacement
            }
        }
        return needsTranslation;
    }
    return true;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 40 with PDRectangle

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

the class PDVisibleSigBuilder method createFormatterRectangle.

/**
 * {@inheritDoc }
 *
 * @deprecated use {@link #createFormatterRectangle(int[]) createFormatterRectangle(int[])}
 */
@Override
@Deprecated
public void createFormatterRectangle(byte[] params) {
    PDRectangle formatterRectangle = new PDRectangle();
    formatterRectangle.setLowerLeftX(Math.min(params[0], params[2]));
    formatterRectangle.setLowerLeftY(Math.min(params[1], params[3]));
    formatterRectangle.setUpperRightX(Math.max(params[0], params[2]));
    formatterRectangle.setUpperRightY(Math.max(params[1], params[3]));
    pdfStructure.setFormatterRectangle(formatterRectangle);
    Log.i("PdfBox-Android", "Formatter rectangle has been created");
}
Also used : PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Aggregations

PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)82 IOException (java.io.IOException)19 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)13 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)12 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)11 Matrix (com.tom_roush.pdfbox.util.Matrix)11 COSArray (com.tom_roush.pdfbox.cos.COSArray)10 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)10 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)9 Path (android.graphics.Path)8 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)5 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 COSName (com.tom_roush.pdfbox.cos.COSName)4 PDExtendedGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)4 PDAnnotationTextMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup)4 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)4