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

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

Introduction

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

Prototype

public static List getBookmark(PdfReader reader) 

Source Link

Document

Gets a List with the bookmarks.

Usage

From source file:org.squale.welcom.outils.pdf.advanced.WPdfMerge.java

License:Open Source License

/**
 * merge// www.j av a2  s  .co  m
 * 
 * @return byte array rempli
 */
private byte[] merge() {
    final ByteArrayOutputStream tmpout = new ByteArrayOutputStream();
    int pageOffset = 0;
    int f = 0;
    Document document = null;
    final ArrayList master = new ArrayList();
    PdfCopy writer = null;

    final Iterator it = readers.iterator();

    while (it.hasNext()) {
        PdfReader reader = (PdfReader) it.next();

        try {
            // Renome tout les champs;
            reader = new PdfReader(renameFieldUnique(reader));

            reader.consolidateNamedDestinations();
            // we retrieve the total number of pages
            final int n = reader.getNumberOfPages();
            final List bookmarks = SimpleBookmark.getBookmark(reader);
            if (bookmarks != null) {
                if (pageOffset != 0) {
                    SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                }
                master.addAll(bookmarks);
            }
            pageOffset += n;

            if (f == 0) {
                // step 1: creation of a document-object
                document = new Document(reader.getPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                writer = new PdfCopy(document, tmpout);
                // step 3: we open the document
                document.open();
            }
            // step 4: we add content
            PdfImportedPage page;
            for (int i = 0; i < n;) {
                ++i;
                page = writer.getImportedPage(reader, i);
                writer.addPage(page);
            }
            final PRAcroForm form = reader.getAcroForm();
            if (form != null) {
                writer.copyAcroForm(reader);
            }
            f++;
            if (master.size() > 0) {
                writer.setOutlines(master);
            }
            // step 5: we close the document
            // document.close();

        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
    if (document != null) {
        document.close();
    }
    return tmpout.toByteArray();
}

From source file:org.squale.welcom.outils.pdf.advanced.WPdfSummary.java

License:Open Source License

/**
 * @return liste de favoris
 */
private List getBookmark() {
    return SimpleBookmark.getBookmark(pdfReader);
}

From source file:oscar.util.ConcatPDF.java

License:Open Source License

/**
 * This class can be used to concatenate existing PDF files.
 * (This was an example known as PdfCopy.java)
 * @param args the command line arguments
 *//*www. ja v a 2s .c o m*/
public static void concat(List<Object> alist, OutputStream out) {

    try {
        int pageOffset = 0;
        ArrayList master = new ArrayList();
        int f = 0;
        Document document = null;
        PdfCopy writer = null;
        boolean fileAsStream = false;
        PdfReader reader = null;
        String name = "";

        MiscUtils.getLogger().debug("Size of list = " + alist.size());

        while (f < alist.size()) {
            // we create a reader for a certain document
            Object o = alist.get(f);

            if (o instanceof InputStream) {
                name = "";
                fileAsStream = true;
            } else {
                name = (String) alist.get(f);
                fileAsStream = false;
            }

            if (fileAsStream) {
                reader = new PdfReader((InputStream) alist.get(f));
            } else {
                reader = new PdfReader(name);
            }

            reader.consolidateNamedDestinations();
            // we retrieve the total number of pages
            int n = reader.getNumberOfPages();
            List bookmarks = SimpleBookmark.getBookmark(reader);
            if (bookmarks != null) {
                if (pageOffset != 0)
                    SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                master.addAll(bookmarks);
            }
            pageOffset += n;
            MiscUtils.getLogger().debug("There are " + n + " pages in " + name);

            if (f == 0) {
                // step 1: creation of a document-object
                document = new Document(reader.getPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                writer = new PdfCopy(document, out);
                // step 3: we open the document
                document.open();
            }
            // step 4: we add content
            PdfImportedPage page;
            for (int i = 0; i < n;) {
                ++i;
                page = writer.getImportedPage(reader, i);
                writer.addPage(page);
                MiscUtils.getLogger().debug("Processed page " + i);
            }
            PRAcroForm form = reader.getAcroForm();
            if (form != null)
                writer.copyAcroForm(reader);
            f++;
        }
        if (master.size() > 0)
            writer.setOutlines(master);
        // step 5: we close the document
        document.close();
    } catch (Exception e) {
        MiscUtils.getLogger().error("Error", e);
    }
}

From source file:questions.stamppages.BookmarksToTOC1.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*w  w  w.ja v  a2 s.co  m*/
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        writer.setPageEvent(new ParagraphBookmarkEvents(false));
        document.open();
        BufferedReader reader = new BufferedReader(new FileReader(RESOURCE));
        String line;
        Paragraph p;
        while ((line = reader.readLine()) != null) {
            p = new Paragraph(line);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(p);
            document.add(Chunk.NEWLINE);
        }
        reader.close();
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    try {
        PdfReader reader = new PdfReader(baos.toByteArray());
        Rectangle rect = reader.getPageSizeWithRotation(1);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.insertPage(1, rect);
        ColumnText column = new ColumnText(stamper.getOverContent(1));
        column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36), rect.getRight(36), rect.getTop(36));
        column.addElement(new Paragraph("TABLE OF CONTENTS"));
        List<Map> list = SimpleBookmark.getBookmark(reader);
        Chunk link;
        PdfAction action;
        String info;
        int p = 1;
        float y = 10;
        for (Map<String, String> bookmark : list) {
            link = new Chunk(bookmark.get("Title"));
            info = bookmark.get("Page");
            p = Integer.parseInt(info.substring(0, info.indexOf(' ')));
            y = Float.parseFloat(info.substring(info.lastIndexOf(' ') + 1) + "f");
            action = PdfAction.gotoLocalPage(p, new PdfDestination(PdfDestination.FITH, y),
                    stamper.getWriter());
            link.setAction(action);
            column.addElement(new Paragraph(link));
        }
        column.go();
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}

From source file:questions.stamppages.BookmarksToTOC2.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from ww  w .j ava  2 s  . c  o  m*/
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        writer.setPageEvent(new ParagraphBookmarkEvents(true));
        document.open();
        BufferedReader reader = new BufferedReader(new FileReader(RESOURCE));
        String line;
        Paragraph p;
        while ((line = reader.readLine()) != null) {
            p = new Paragraph(line);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(p);
            document.add(Chunk.NEWLINE);
        }
        reader.close();
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    try {
        PdfReader reader = new PdfReader(baos.toByteArray());
        Rectangle rect = reader.getPageSizeWithRotation(1);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.insertPage(1, rect);
        ColumnText column = new ColumnText(stamper.getOverContent(1));
        column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36), rect.getRight(36), rect.getTop(36));
        column.addElement(new Paragraph("TABLE OF CONTENTS"));
        List<Map> list = SimpleBookmark.getBookmark(reader);
        Chunk link;
        String dest;
        for (Map<String, String> bookmark : list) {
            link = new Chunk(bookmark.get("Title"));
            dest = bookmark.get("Named");
            link.setAction(PdfAction.gotoLocalPage(dest, false));
            column.addElement(new Paragraph(link));
        }
        column.go();
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}