Search in sources :

Example 1 with PDNamedDestination

use of com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination in project PdfBox-Android by TomRoush.

the class PDOutlineItem method findDestinationPage.

/**
 * This method will attempt to find the page in this PDF document that this outline points to.
 * If the outline does not point to anything then this method will return null. If the outline
 * is an action that is not a GoTo action then this method will also return null.
 *
 * @param doc The document to get the page from.
 *
 * @return The page that this outline will go to when activated or null if it does not point to
 * anything.
 * @throws IOException If there is an error when trying to find the page.
 */
public PDPage findDestinationPage(PDDocument doc) throws IOException {
    PDDestination dest = getDestination();
    if (dest == null) {
        PDAction outlineAction = getAction();
        if (outlineAction instanceof PDActionGoTo) {
            dest = ((PDActionGoTo) outlineAction).getDestination();
        }
    }
    if (dest == null) {
        return null;
    }
    PDPageDestination pageDestination = null;
    if (dest instanceof PDNamedDestination) {
        pageDestination = doc.getDocumentCatalog().findNamedDestinationPage((PDNamedDestination) dest);
        if (pageDestination == null) {
            return null;
        }
    } else if (dest instanceof PDPageDestination) {
        pageDestination = (PDPageDestination) dest;
    } else {
        throw new IOException("Error: Unknown destination type " + dest);
    }
    PDPage page = pageDestination.getPage();
    if (page == null) {
        // Malformed PDF: local destinations must have a page object,
        // not a page number, these are meant for remote destinations.
        int pageNumber = pageDestination.getPageNumber();
        if (pageNumber != -1) {
            page = doc.getPage(pageNumber);
        }
    }
    return page;
}
Also used : PDAction(com.tom_roush.pdfbox.pdmodel.interactive.action.PDAction) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDNamedDestination(com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination) PDPageDestination(com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination) PDDestination(com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination) IOException(java.io.IOException) PDActionGoTo(com.tom_roush.pdfbox.pdmodel.interactive.action.PDActionGoTo)

Aggregations

PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)1 PDAction (com.tom_roush.pdfbox.pdmodel.interactive.action.PDAction)1 PDActionGoTo (com.tom_roush.pdfbox.pdmodel.interactive.action.PDActionGoTo)1 PDDestination (com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination)1 PDNamedDestination (com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination)1 PDPageDestination (com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination)1 IOException (java.io.IOException)1