/** * Returns the page labels descriptor of the document. * * @return the page labels descriptor of the document. * @throws IOException If there is a problem retrieving the page labels. */ public PDPageLabels getPageLabels() throws IOException { COSDictionary dict = (COSDictionary) root.getDictionaryObject(COSName.PAGE_LABELS); return dict == null ? null : new PDPageLabels(document, dict); }
/** * Returns a mapping with 0-based page indices as keys and corresponding * page labels as values as an array. The array will have exactly as much * entries as the document has pages. * * @return an array mapping from 0-based page indices to labels. */ public String[] getLabelsByPageIndices() { final String[] map = new String[doc.getNumberOfPages()]; computeLabels(new LabelHandler() { @Override public void newLabel(int pageIndex, String label) { if(pageIndex < doc.getNumberOfPages()) { map[pageIndex] = label; } } }); return map; }
/** * Creates an page label dictionary for a document using the information in * the given COS dictionary. * * <p> * Note that the page label dictionary won't be automatically added to the * document; you will still need to do it manually (see * {@link org.apache.pdfbox.pdmodel.PDDocumentCatalog#setPageLabels(PDPageLabels)}. * </p> * * @param document * The document the page label dictionary is created for. * @param dict * an existing page label dictionary * @see org.apache.pdfbox.pdmodel.PDDocumentCatalog#setPageLabels(PDPageLabels) * @throws IOException * If something goes wrong during the number tree conversion. */ public PDPageLabels(PDDocument document, COSDictionary dict) throws IOException { this(document); if (dict == null) { return; } PDNumberTreeNode root = new PDNumberTreeNode(dict, PDPageLabelRange.class); findLabels(root); }
/** * Convenience method to get the page label if available. * * @param document * @param pageIndex 0-based page number. * @return a page label or null if not available. */ public static String getPageLabel(PDDocument document, int pageIndex) { PDPageLabels pageLabels; try { pageLabels = document.getDocumentCatalog().getPageLabels(); } catch (IOException ex) { return ex.getMessage(); } if (pageLabels != null) { String[] labels = pageLabels.getLabelsByPageIndices(); if (labels[pageIndex] != null) { return labels[pageIndex]; } } return null; } }
private void findLabels(PDNumberTreeNode node) throws IOException { if (node.getKids() != null) { List<PDNumberTreeNode> kids = node.getKids(); for (PDNumberTreeNode kid : kids) { findLabels(kid); } } else if (node.getNumbers() != null) { Map<Integer, COSObjectable> numbers = node.getNumbers(); for (Entry<Integer, COSObjectable> i : numbers.entrySet()) { if(i.getKey() >= 0) { labels.put(i.getKey(), (PDPageLabelRange) i.getValue()); } } } }
/** * Returns a mapping with computed page labels as keys and corresponding * 0-based page indices as values. The returned map will contain at most as * much entries as the document has pages. * * <p> * <strong>NOTE:</strong> If the document contains duplicate page labels, * the returned map will contain <em>less</em> entries than the document has * pages. The page index returned in this case is the <em>highest</em> index * among all pages sharing the same label. * </p> * * @return a mapping from labels to 0-based page indices. */ public Map<String, Integer> getPageIndicesByLabels() { final Map<String, Integer> labelMap = new HashMap<>(doc.getNumberOfPages()); computeLabels(new LabelHandler() { @Override public void newLabel(int pageIndex, String label) { labelMap.put(label, pageIndex); } }); return labelMap; }
/** * Returns the page labels descriptor of the document. * * @return the page labels descriptor of the document. * @throws IOException If there is a problem retrieving the page labels. */ public PDPageLabels getPageLabels() throws IOException { COSDictionary dict = (COSDictionary) root.getDictionaryObject(COSName.PAGE_LABELS); return dict == null ? null : new PDPageLabels(document, dict); }
/** * Creates an page label dictionary for a document using the information in * the given COS dictionary. * * <p> * Note that the page label dictionary won't be automatically added to the * document; you will still need to do it manually (see * {@link org.apache.pdfbox.pdmodel.PDDocumentCatalog#setPageLabels(PDPageLabels)}. * </p> * * @param document * The document the page label dictionary is created for. * @param dict * an existing page label dictionary * @see org.apache.pdfbox.pdmodel.PDDocumentCatalog#setPageLabels(PDPageLabels) * @throws IOException * If something goes wrong during the number tree conversion. */ public PDPageLabels(PDDocument document, COSDictionary dict) throws IOException { this(document); if (dict == null) { return; } PDNumberTreeNode root = new PDNumberTreeNode(dict, PDPageLabelRange.class); findLabels(root); }
/** * Returns a mapping with 0-based page indices as keys and corresponding * page labels as values as an array. The array will have exactly as much * entries as the document has pages. * * @return an array mapping from 0-based page indices to labels. */ public String[] getLabelsByPageIndices() { final String[] map = new String[doc.getNumberOfPages()]; computeLabels(new LabelHandler() { @Override public void newLabel(int pageIndex, String label) { if(pageIndex < doc.getNumberOfPages()) { map[pageIndex] = label; } } }); return map; }
/** * Returns the page labels descriptor of the document. * * @return the page labels descriptor of the document. * @throws IOException If there is a problem retrieving the page labels. */ public PDPageLabels getPageLabels() throws IOException { COSDictionary dict = (COSDictionary) root.getDictionaryObject(COSName.PAGE_LABELS); return dict == null ? null : new PDPageLabels(document, dict); }
/** * Creates an page label dictionary for a document using the information in * the given COS dictionary. * * <p> * Note that the page label dictionary won't be automatically added to the * document; you will still need to do it manually (see * {@link org.apache.pdfbox.pdmodel.PDDocumentCatalog#setPageLabels(PDPageLabels)}. * </p> * * @param document * The document the page label dictionary is created for. * @param dict * an existing page label dictionary * @see org.apache.pdfbox.pdmodel.PDDocumentCatalog#setPageLabels(PDPageLabels) * @throws IOException * If something goes wrong during the number tree conversion. */ public PDPageLabels(PDDocument document, COSDictionary dict) throws IOException { this(document); if (dict == null) { return; } PDNumberTreeNode root = new PDNumberTreeNode(dict, PDPageLabelRange.class); findLabels(root); }
/** * Returns a mapping with 0-based page indices as keys and corresponding * page labels as values as an array. The array will have exactly as much * entries as the document has pages. * * @return an array mapping from 0-based page indices to labels. */ public String[] getLabelsByPageIndices() { final String[] map = new String[doc.getNumberOfPages()]; computeLabels(new LabelHandler() { @Override public void newLabel(int pageIndex, String label) { if(pageIndex < doc.getNumberOfPages()) { map[pageIndex] = label; } } }); return map; }
private void findLabels(PDNumberTreeNode node) throws IOException { if (node.getKids() != null) { List<PDNumberTreeNode> kids = node.getKids(); for (PDNumberTreeNode kid : kids) { findLabels(kid); } } else if (node.getNumbers() != null) { Map<Integer, COSObjectable> numbers = node.getNumbers(); for (Entry<Integer, COSObjectable> i : numbers.entrySet()) { if(i.getKey() >= 0) { labels.put(i.getKey(), (PDPageLabelRange) i.getValue()); } } } }
/** * Returns a mapping with computed page labels as keys and corresponding * 0-based page indices as values. The returned map will contain at most as * much entries as the document has pages. * * <p> * <strong>NOTE:</strong> If the document contains duplicate page labels, * the returned map will contain <em>less</em> entries than the document has * pages. The page index returned in this case is the <em>highest</em> index * among all pages sharing the same label. * </p> * * @return a mapping from labels to 0-based page indices. */ public Map<String, Integer> getPageIndicesByLabels() { final Map<String, Integer> labelMap = new HashMap<String, Integer>(doc.getNumberOfPages()); computeLabels(new LabelHandler() { @Override public void newLabel(int pageIndex, String label) { labelMap.put(label, pageIndex); } }); return labelMap; }
private void findLabels(PDNumberTreeNode node) throws IOException { if (node.getKids() != null) { List<PDNumberTreeNode> kids = node.getKids(); for (PDNumberTreeNode kid : kids) { findLabels(kid); } } else if (node.getNumbers() != null) { Map<Integer, COSObjectable> numbers = node.getNumbers(); for (Entry<Integer, COSObjectable> i : numbers.entrySet()) { if(i.getKey() >= 0) { labels.put(i.getKey(), (PDPageLabelRange) i.getValue()); } } } }
/** * Returns a mapping with computed page labels as keys and corresponding * 0-based page indices as values. The returned map will contain at most as * much entries as the document has pages. * * <p> * <strong>NOTE:</strong> If the document contains duplicate page labels, * the returned map will contain <em>less</em> entries than the document has * pages. The page index returned in this case is the <em>highest</em> index * among all pages sharing the same label. * </p> * * @return a mapping from labels to 0-based page indices. */ public Map<String, Integer> getPageIndicesByLabels() { final Map<String, Integer> labelMap = new HashMap<>(doc.getNumberOfPages()); computeLabels(new LabelHandler() { @Override public void newLabel(int pageIndex, String label) { labelMap.put(label, pageIndex); } }); return labelMap; }