org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDAppearanceContentStream.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Convenience method for annotations: sets the line with and dash style.
 *
 * @param lineWidth The line width.
 * @param bs The border style, may be null.
 * @param border The border array, must have at least three entries. This is
 * only used if the border style is null.
 *
 * @throws IOException If there is an error writing to the content stream.
 */
public void setBorderLine(float lineWidth, PDBorderStyleDictionary bs,
                                           COSArray border) throws IOException
{
    // Can't use PDBorderStyleDictionary.getDashStyle() as
    // this will return a default dash style if non is existing
    if (bs != null && bs.getCOSObject().containsKey(COSName.D) && 
                      bs.getStyle().equals(PDBorderStyleDictionary.STYLE_DASHED))
    {
        setLineDashPattern(bs.getDashStyle().getDashArray(), 0);
    }
    else if (bs == null && border.size() > 3 && border.getObject(3) instanceof COSArray)
    {
        setLineDashPattern(((COSArray) border.getObject(3)).toFloatArray(), 0);
    }
    setLineWidthOnDemand(lineWidth);
}
 
Example #2
Source File: PdfManipulator.java    From estatio with Apache License 2.0 6 votes vote down vote up
private void addHyperlink(
        final float x, final float y,
        final String hyperlink,
        final PDPage pdPage) throws IOException {

    PDAnnotationLink txtLink = new PDAnnotationLink();

    PDRectangle position = new PDRectangle();
    PDBorderStyleDictionary underline = new PDBorderStyleDictionary();
    underline.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
    txtLink.setBorderStyle(underline);

    position.setLowerLeftX(x);
    position.setLowerLeftY(y);
    position.setUpperRightX(X_MARGIN_LEFT + BOX_WIDTH);
    position.setUpperRightY(y + TEXT_LINE_HEIGHT);
    txtLink.setRectangle(position);

    PDActionURI action = new PDActionURI();
    action.setURI(hyperlink);
    txtLink.setAction(action);
    pdPage.getAnnotations().add(txtLink);
}
 
Example #3
Source File: FDFAnnotation.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will retrieve the border style dictionary, specifying the width and dash pattern used in drawing the
 * annotation.
 *
 * @return the border style dictionary.
 */
public PDBorderStyleDictionary getBorderStyle()
{
    COSDictionary bs = (COSDictionary) annot.getDictionaryObject(COSName.BS);
    if (bs != null)
    {
        return new PDBorderStyleDictionary(bs);
    }
    else
    {
        return null;
    }
}
 
Example #4
Source File: PDCircleAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example #5
Source File: PDLinkAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationLink annotation = (PDAnnotationLink) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example #6
Source File: PDPolygonAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example #7
Source File: PDPolylineAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example #8
Source File: PDSquareAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example #9
Source File: ReportLink.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
private float addLink(PDDocument document, int pageNumber, float startX, float startY, PdfTextStyle textConfig) {
    PDAnnotationLink txtLink = new PDAnnotationLink();
    txtLink.setColor(textConfig.getColor());

    PDBorderStyleDictionary underline = getLinkUnderline();
    txtLink.setBorderStyle(underline);

    try {
        float textWidth = (textConfig.getFont().getStyle(textConfig.getStyle()).getStringWidth(text) / 1000) * textConfig.getFontSize();

        float startLinkY = startY + textConfig.getFontSize();
        float endLinkY = startY - underline.getWidth();

        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(startX);
        position.setLowerLeftY(startLinkY);
        position.setUpperRightX(startX + textWidth);
        position.setUpperRightY(endLinkY);
        txtLink.setRectangle(position);

        PDActionURI action = new PDActionURI();
        action.setURI(link);
        txtLink.setAction(action);

        PDPage page = document.getDocumentCatalog().getPages().get(pageNumber);
        page.getAnnotations().add(txtLink);

        return endLinkY;
    } catch (IOException e) {
        LOG.warn("Could not add link: " + e.getClass() + " - " + e.getMessage());
        return startY;
    }
}
 
Example #10
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
private static PDBorderStyleDictionary toBorderStyle(
    final LinkStyle linkStyle) {
if (linkStyle == LinkStyle.none) {
    return getNoBorder();
}
PDBorderStyleDictionary borderStyle = new PDBorderStyleDictionary();
borderStyle.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
return borderStyle;
   }
 
Example #11
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
private static PDBorderStyleDictionary toBorderStyle(
    final LinkStyle linkStyle) {
if (linkStyle == LinkStyle.none) {
    return getNoBorder();
}
PDBorderStyleDictionary borderStyle = new PDBorderStyleDictionary();
borderStyle.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
return borderStyle;
   }
 
Example #12
Source File: AnnotationBorder.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
static AnnotationBorder getAnnotationBorder(PDAnnotation annotation,
        PDBorderStyleDictionary borderStyle)
{
    AnnotationBorder ab = new AnnotationBorder();
    if (borderStyle == null)
    {
        COSArray border = annotation.getBorder();
        if (border.size() >= 3 && border.getObject(2) instanceof COSNumber)
        {
            ab.width = ((COSNumber) border.getObject(2)).floatValue();
        }
        if (border.size() > 3)
        {
            COSBase base3 = border.getObject(3);
            if (base3 instanceof COSArray)
            {
                ab.dashArray = ((COSArray) base3).toFloatArray();
            }
        }
    }
    else
    {
        ab.width = borderStyle.getWidth();
        if (borderStyle.getStyle().equals(PDBorderStyleDictionary.STYLE_DASHED))
        {
            ab.dashArray = borderStyle.getDashStyle().getDashArray();
        }
        if (borderStyle.getStyle().equals(PDBorderStyleDictionary.STYLE_UNDERLINE))
        {
            ab.underline = true;
        }
    }
    if (ab.dashArray != null)
    {
        boolean allZero = true;
        for (float f : ab.dashArray)
        {
            if (Float.compare(f, 0) != 0)
            {
                allZero = false;
                break;
            }
        }
        if (allZero)
        {
            ab.dashArray = null;
        }
    }
    return ab;
}
 
Example #13
Source File: ReportLink.java    From cat-boot with Apache License 2.0 4 votes vote down vote up
private PDBorderStyleDictionary getLinkUnderline() {
    PDBorderStyleDictionary underline = new PDBorderStyleDictionary();
    underline.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
    return underline;
}
 
Example #14
Source File: FDFAnnotation.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * This will set the border style dictionary, specifying the width and dash pattern used in drawing the annotation.
 *
 * @param bs the border style dictionary to set.
 *
 */
public final void setBorderStyle(PDBorderStyleDictionary bs)
{
    annot.setItem(COSName.BS, bs);
}