Example usage for org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline PDOutlineItem getAction

List of usage examples for org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline PDOutlineItem getAction

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline PDOutlineItem getAction.

Prototype

public PDAction getAction() 

Source Link

Document

Get the action of this node.

Usage

From source file:com.quanticate.opensource.pdftkbox.PDFBookmark.java

License:Apache License

/**
 * Creates our Bookmark Wrapper from the outline item.
 * Handling Children (and tracking of levels) is up to
 *  the calling class to manage//w  w  w .  java  2  s  . c  o m
 */
public PDFBookmark(PDOutlineItem current, int level) throws IOException {
    this.title = current.getTitle();
    this.outlineItem = current;
    this.level = level;

    // Set defaults
    this.pageNumber = -1;
    this.yOffset = 0;

    // Find where the bookmark points to and record
    PDDestination dest = null;

    // Check for a bookmark via an action
    if (current.getAction() != null) {
        PDAction action = current.getAction();
        if (action instanceof PDActionGoTo) {
            dest = ((PDActionGoTo) action).getDestination();
        }
    }
    if (dest == null) {
        dest = current.getDestination();
    }

    if (dest != null) {
        if (dest instanceof PDPageDestination) {
            PDPageDestination pdest = (PDPageDestination) dest;
            int pageNum = pdest.retrievePageNumber();
            if (pageNum != -1) {
                this.pageNumber = pageNum + 1;
            }
        }

        if (dest instanceof PDPageXYZDestination) {
            PDPageXYZDestination xyz = (PDPageXYZDestination) dest;
            yOffset = xyz.getTop();

            if (xyz.getZoom() > 0) {
                zoomType = ZoomType.ZoomPercent;
                zoom = Integer.toString((int) (xyz.getZoom() * 100)) + "%";
            } else {
                zoomType = ZoomType.Inherit;
            }
        } else if (dest instanceof PDPageFitWidthDestination) {
            PDPageFitWidthDestination width = (PDPageFitWidthDestination) dest;
            yOffset = width.getTop();

            zoomType = ZoomType.FitWidth;
        } else if (dest instanceof PDPageFitDestination) {
            zoomType = ZoomType.FitPage;
        } else if (dest instanceof PDPageFitHeightDestination) {
            zoomType = ZoomType.FitHeight;
        } else {
            System.err.println("TODO: Support destination of type " + dest);
        }

        // Set a zoom description from the type if needed
        if (zoomType != null && zoom == null) {
            zoom = zoomType.name();
        }
    } else {
        System.err.println(
                "Warning - Non-destination bookmark " + current + " with action " + current.getAction());
    }
}

From source file:com.vns.pdf.impl.PdfDocument.java

License:Apache License

public void fillBookmark(PDOutlineNode bookmark, String indentation) throws IOException {
    PDOutlineItem current = bookmark.getFirstChild();
    while (current != null) {
        ActionData actionData = parsePDAction(current.getAction());
        if (actionData == null) {
            actionData = parsePDDestination(current.getDestination());
        }//  w  ww .ja v  a 2 s  .c  o  m
        Annotation annotation;
        if (actionData != null) {
            annotation = new Annotation(-1, -1, -1, -1, actionData.destX, actionData.destY, actionData.destPage,
                    actionData.destZoom, indentation + current.getTitle());
        } else {
            annotation = new Annotation(indentation + current.getTitle());
        }
        this.doc.getBookmarks().add(annotation);
        fillBookmark(current, indentation + "    ");
        current = current.getNextSibling();
    }
}

From source file:org.apache.tika.parser.pdf.AbstractPDF2XHTML.java

License:Apache License

void extractBookmarkText(PDOutlineNode bookmark) throws SAXException, IOException, TikaException {
    PDOutlineItem current = bookmark.getFirstChild();

    if (current != null) {
        xhtml.startElement("ul");
        while (current != null) {
            xhtml.startElement("li");
            xhtml.characters(current.getTitle());
            xhtml.endElement("li");
            handleDestinationOrAction(current.getAction(), ActionTrigger.BOOKMARK);
            // Recurse:
            extractBookmarkText(current);
            current = current.getNextSibling();
        }/*from w  w w . j  a  va  2 s  . c  om*/
        xhtml.endElement("ul");
    }
}

From source file:org.pdfmetamodifier.OutlineHelper.java

License:Apache License

private static Integer getOutlinePageNumber(final PDOutlineItem outlineItem, final PDPageTree pages,
        final PDDestinationNameTreeNode destinations) throws IOException {
    final PDAction action = outlineItem.getAction();
    if (action != null) {
        if (action instanceof PDActionGoTo) {
            final PDActionGoTo actionGoTo = (PDActionGoTo) action;

            return getDestinationPageNumber(actionGoTo.getDestination(), pages, destinations);
        }//from w w  w .j av a 2 s  . co  m

        // Ignore other actions.
    }

    return getDestinationPageNumber(outlineItem.getDestination(), pages, destinations);
}

From source file:org.pdfsam.pdfbox.component.PDFBoxOutlineUtils.java

License:Open Source License

/**
 * @param current/*from   ww  w  . j ava 2 s  .  com*/
 *            the outline item
 * @param destinations
 *            the named destinations tree to look for in case of {@link PDNamedDestination}
 * @return the {@link PDPageDestination} for the given {@link PDOutlineItem} or null if the destination is not a page. In case the outline item has a named destination, it is
 *         resolved against the given names tree.
 */
public static Optional<PDPageDestination> toPageDestination(PDOutlineItem current,
        PDDestinationNameTreeNode destinations) {
    try {
        PDDestination dest = current.getDestination();
        if (dest == null) {
            PDAction outlineAction = current.getAction();
            if (outlineAction instanceof PDActionGoTo) {
                dest = ((PDActionGoTo) outlineAction).getDestination();
            }
        }
        if (dest instanceof PDNamedDestination && destinations != null) {
            dest = (PDDestination) destinations.getValue(((PDNamedDestination) dest).getNamedDestination());
        }
        if (dest instanceof PDPageDestination) {
            return Optional.of((PDPageDestination) dest);
        }
    } catch (IOException e) {
        LOG.warn("Unable to get outline item destination ", e);
    }
    return Optional.empty();
}