Example usage for com.lowagie.text.pdf SimpleBookmark exportToXML

List of usage examples for com.lowagie.text.pdf SimpleBookmark exportToXML

Introduction

In this page you can find the example usage for com.lowagie.text.pdf SimpleBookmark exportToXML.

Prototype

public static void exportToXML(List list, Writer wrt, String encoding, boolean onlyASCII) throws IOException 

Source Link

Document

Exports the bookmarks to XML.

Usage

From source file:de.unigoettingen.sub.commons.contentlib.pdflib.PDFCreator.java

License:Apache License

private List<PDFBookmark> extractBookmarksFromPDF(PdfReader pdfreader, List<PDFBookmark> pDFBookmarks,
        int documentpartnumber) {
    // add Bookmarks
    List<?> list = SimpleBookmark.getBookmark(pdfreader);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (list != null) {
        try {/*from w ww.j  a  v  a2s. com*/
            // TODO: GDZ: Do we really need this XML step here?
            SimpleBookmark.exportToXML(list, baos, "UTF8", false);
            String bms = baos.toString();
            LOGGER.debug(bms);
            // parse the xml, find Title elements
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(bms)));

            Element e = doc.getDocumentElement();
            NodeList nl = e.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().equals("Title"))) {
                    // found the right node, get title and pagenumber
                    PDFBookmark bm = parseTitleNode(n, documentpartnumber);
                    // this is the uppermost bookmark, add it to rootbookmark list
                    pDFBookmarks.add(bm);
                }
            }
        } catch (IOException e) {
            LOGGER.error("IOExeption occured", e);
        } catch (ParserConfigurationException e) {
            LOGGER.error("ParserConfigurationException occured", e);
        } catch (SAXException e) {
            LOGGER.error("SAXException occured", e);
        }
    }
    return pDFBookmarks;
}

From source file:net.sqs2.omr.master.pdfbookmark.PDFtoBookmarkTranslator.java

License:Apache License

@Override
public void execute(InputStream sourceInputStream, String systemId, OutputStream bookmarkOutputStream,
        URIResolver uriResolver) throws InvalidPageMasterException {
    try {/*from w  w w.  j  av a  2  s  .co m*/
        PdfReader reader = new PdfReader(sourceInputStream);
        this.numPages = reader.getNumberOfPages();
        SimpleBookmark.exportToXML(SimpleBookmark.getBookmark(reader), bookmarkOutputStream, "UTF-8", false);
        reader.close();
    } catch (IOException ex) {
        throw new InvalidPageMasterException();
    } finally {
    }
}

From source file:org.pdfsam.console.business.pdf.handlers.SplitCmdExecutor.java

License:Open Source License

/**
 * Execute the split of a pdf document when split type is S_BLEVEL
 * /*  www .j  a v  a  2s.  com*/
 * @param inputCommand
 * @throws Exception
 */
private void executeBookmarksSplit(SplitParsedCommand inputCommand) throws Exception {
    pdfReader = PdfUtility.readerFor(inputCommand.getInputFile());
    int bLevel = inputCommand.getBookmarksLevel().intValue();
    Hashtable bookmarksTable = new Hashtable();
    if (bLevel > 0) {
        pdfReader.removeUnusedObjects();
        pdfReader.consolidateNamedDestinations();
        List bookmarks = SimpleBookmark.getBookmark(pdfReader);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SimpleBookmark.exportToXML(bookmarks, out, "UTF-8", false);
        ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
        int maxDepth = PdfUtility.getMaxBookmarksDepth(input);
        input.reset();
        if (bLevel <= maxDepth) {
            SAXReader reader = new SAXReader();
            org.dom4j.Document document = reader.read(input);
            // head node
            String headBookmarkXQuery = "/Bookmark/Title[@Action=\"GoTo\"]";
            Node headNode = document.selectSingleNode(headBookmarkXQuery);
            if (headNode != null && headNode.getText() != null && headNode.getText().trim().length() > 0) {
                bookmarksTable.put(new Integer(1), headNode.getText().trim());
            }
            // bLevel nodes
            StringBuffer buffer = new StringBuffer("/Bookmark");
            for (int i = 0; i < bLevel; i++) {
                buffer.append("/Title[@Action=\"GoTo\"]");
            }
            String xQuery = buffer.toString();
            List nodes = document.selectNodes(xQuery);
            input.close();
            input = null;
            if (nodes != null && nodes.size() > 0) {
                LinkedHashSet pageSet = new LinkedHashSet(nodes.size());
                for (Iterator nodeIter = nodes.iterator(); nodeIter.hasNext();) {
                    Node currentNode = (Node) nodeIter.next();
                    Node pageAttribute = currentNode.selectSingleNode("@Page");
                    if (pageAttribute != null && pageAttribute.getText().length() > 0) {
                        String attribute = pageAttribute.getText();
                        int blankIndex = attribute.indexOf(' ');
                        if (blankIndex > 0) {
                            Integer currentNumber = new Integer(attribute.substring(0, blankIndex));
                            String bookmarkText = currentNode.getText().trim();
                            // fix #2789963
                            if (currentNumber.intValue() > 0) {
                                // bookmarks regexp matching if any
                                if (StringUtils.isBlank(inputCommand.getBookmarkRegexp())
                                        || bookmarkText.matches(inputCommand.getBookmarkRegexp())) {
                                    // to split just before the given page
                                    if ((currentNumber.intValue()) > 1) {
                                        pageSet.add(new Integer(currentNumber.intValue() - 1));
                                    }
                                    if (StringUtils.isNotBlank(bookmarkText)) {
                                        bookmarksTable.put(currentNumber, bookmarkText.trim());
                                    }
                                }
                            }
                        }
                    }
                }
                if (pageSet.size() > 0) {
                    if (StringUtils.isBlank(inputCommand.getBookmarkRegexp())) {
                        LOG.debug("Found " + pageSet.size() + " destination pages at level " + bLevel);
                    } else {
                        LOG.debug("Found " + pageSet.size() + " destination pages at level " + bLevel
                                + " matching '" + inputCommand.getBookmarkRegexp() + "'");
                    }
                    inputCommand.setSplitPageNumbers((Integer[]) pageSet.toArray(new Integer[pageSet.size()]));
                } else {
                    throw new SplitException(SplitException.ERR_BLEVEL_NO_DEST, new String[] { "" + bLevel });
                }
            } else {
                throw new SplitException(SplitException.ERR_BLEVEL, new String[] { "" + bLevel });
            }
        } else {
            input.close();
            pdfReader.close();
            throw new SplitException(SplitException.ERR_BLEVEL_OUTOFBOUNDS,
                    new String[] { "" + bLevel, "" + maxDepth });

        }
    } else {
        pdfReader.close();
        throw new SplitException(SplitException.ERR_NOT_VALID_BLEVEL, new String[] { "" + bLevel });
    }
    pdfReader.close();
    executeSplit(inputCommand, bookmarksTable);
}