Example usage for com.lowagie.text.pdf PdfPageLabels addPageLabel

List of usage examples for com.lowagie.text.pdf PdfPageLabels addPageLabel

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPageLabels addPageLabel.

Prototype

public void addPageLabel(int page, int numberStyle, String text, int firstPage) 

Source Link

Document

Adds or replaces a page label.

Usage

From source file:ErrMsgException.java

License:Open Source License

public static void main(String args[]) {
    // ?//from  ww  w .j  av a 2  s . co  m
    if (args.length < 3 || args.length > 5) {
        usage();
        return;
    }

    // ??
    ArrayList<HashMap> bookmark = new ArrayList<HashMap>();
    HashMap<String, Object> lastmap = null;
    ArrayDeque<ArrayList> queue = new ArrayDeque<ArrayList>();
    queue.push(bookmark);

    // ?????
    try {
        File file = new File(args[2]);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        int previouslevel = 0;
        int lno = 0;

        while ((line = br.readLine()) != null) {
            lno++;
            if (line.startsWith("#"))
                continue; // ?#

            int level = 0;
            if (line.startsWith("\t")) {
                // 
                do {
                    line = line.substring(1);
                    level++;
                } while (line.startsWith("\t"));
            }

            String lines[] = line.split("\t");
            line = "";
            if (lines.length > 2) {
                for (int i = 0; i < lines.length - 1; i++) {
                    line += lines[i];
                    if (i < lines.length - 1)
                        line += "";
                }
            } else {
                line = lines[0];
            }
            if (lines.length < 2)
                continue;

            int preample = 0;
            if (args.length >= 4) {
                preample = Integer.parseInt(args[3]);
            }
            int page = roman2arabic(lines[lines.length - 1], preample);

            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("Title", line);
            map.put("Action", "GoTo");
            map.put("Page", page + " Fit");

            if (args.length >= 5) {
                // ??
                int foldlevel = Integer.parseInt(args[4]);
                if (foldlevel <= level)
                    map.put("Open", "false");
            }

            if (level > previouslevel) {
                if (level - previouslevel > 1) {
                    // ???
                    throw new ErrMsgException(
                            lno + ": ??2?????????");
                }
                // 
                ArrayList<HashMap> kids = new ArrayList<HashMap>();
                kids.add(map);
                if (lastmap != null) {
                    lastmap.put("Kids", kids);
                } else {
                    throw new ErrMsgException(
                            lno + ": ??????????");
                }
                queue.push(kids);

            } else if (level < previouslevel) {
                // ?
                for (int i = previouslevel; i > level; i--) {
                    queue.pop();
                }
                queue.peek().add(map);
            } else {
                // ????????
                queue.peek().add(map);
            }
            lastmap = map;
            previouslevel = level;
        }

        br.close();
    } catch (IOException e) {
        System.out.println(e);
        return;
    } catch (ErrMsgException e) {
        System.out.println("?????\n" + e.getMessage());
        return;
    }

    // PDF????
    try {
        PdfReader reader = new PdfReader(args[0]);
        int maxpage = reader.getNumberOfPages();

        OutputStream os = new FileOutputStream(args[1]);
        PdfCopyFields copyFields = new PdfCopyFields(os);
        copyFields.addDocument(reader);

        copyFields.setOutlines(bookmark);

        if (args.length > 3) { // ?
            PdfPageLabels labels = new PdfPageLabels();
            if (maxpage > 0)
                labels.addPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS, null, 1);
            if (maxpage > Integer.parseInt(args[3]))
                labels.addPageLabel(Integer.parseInt(args[3]) + 1, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, null,
                        1);

            copyFields.getWriter().setPageLabels(labels);
        }

        copyFields.close();
        os.close();
    } catch (DocumentException e) {
        System.out.println(e);
        return;
    } catch (IOException e) {
        System.out.println(e);
        return;
    }
    System.out.println(args[1] + " ?????");
}

From source file:RomanNmbl.java

License:Open Source License

public static void main(String args[]) {
    if (args.length != 3) {
        usage();/*from w ww .j  ava2s.  c o  m*/
        return;
    }

    try {
        PdfReader reader;
        reader = new PdfReader(args[0]);
        int maxpage = reader.getNumberOfPages();

        PdfStamper stamper;
        stamper = new PdfStamper(reader, new FileOutputStream(args[1]));

        PdfPageLabels labels = new PdfPageLabels();
        if (maxpage > 0)
            labels.addPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS, null, 1);
        if (maxpage > Integer.parseInt(args[2]))
            labels.addPageLabel(Integer.parseInt(args[2]) + 1, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, null, 1);

        stamper.getWriter().setPageLabels(labels);
        stamper.close();
    } catch (IOException e) {
        // FIXME: Better error handling.
        System.out.println(e);
        return;
    } catch (DocumentException e) {
        System.out.println(e);
        return;
    }
}

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

License:Open Source License

public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {
    if ((parsedCommand != null) && (parsedCommand instanceof PageLabelsParsedCommand)) {

        PageLabelsParsedCommand inputCommand = (PageLabelsParsedCommand) parsedCommand;
        setPercentageOfWorkDone(0);/*w w  w . java2 s  . c  o  m*/
        Document currentDocument;
        try {
            File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile());
            LOG.debug("Opening " + inputCommand.getInputFile().getFile().getAbsolutePath());
            pdfReader = PdfUtility.readerFor(inputCommand.getInputFile());
            pdfReader.removeUnusedObjects();
            pdfReader.consolidateNamedDestinations();
            int n = pdfReader.getNumberOfPages();
            currentDocument = new Document(pdfReader.getPageSizeWithRotation(1));

            pdfWriter = new PdfCopy(currentDocument, new FileOutputStream(tmpFile));

            // set compressed
            setCompressionSettingOnWriter(inputCommand, pdfWriter);
            // set pdf version
            setPdfVersionSettingOnWriter(inputCommand, pdfWriter, Character.valueOf(pdfReader.getPdfVersion()));

            // set creator
            currentDocument.addCreator(ConsoleServicesFacade.CREATOR);
            currentDocument.open();

            for (int count = 1; count <= n; count++) {
                pdfWriter.addPage(pdfWriter.getImportedPage(pdfReader, count));
            }
            pdfReader.close();
            pdfWriter.freeReader(pdfReader);

            // set labels
            PdfPageLabels pageLabels = new PdfPageLabels();
            PageLabel[] labels = inputCommand.getLabels();
            // last step is creating the file
            int stepsNumber = labels.length + 1;
            for (int i = 0; i < labels.length; i++) {
                if (labels[i].getPageNumber() <= n) {
                    pageLabels.addPageLabel(labels[i].getPageNumber(), getPageLabelStyle(labels[i].getStyle()),
                            labels[i].getPrefix(), labels[i].getLogicalPageNumber());
                } else {
                    LOG.warn("Page number out of range, label starting at page " + labels[i].getPageNumber()
                            + " will be ignored");
                }
                setPercentageOfWorkDone((i * WorkDoneDataModel.MAX_PERGENTAGE) / stepsNumber);
            }
            pdfWriter.setPageLabels(pageLabels);

            currentDocument.close();
            pdfWriter.close();
            FileUtility.renameTemporaryFile(tmpFile, inputCommand.getOutputFile(), inputCommand.isOverwrite());
            LOG.debug("Page labels set on file " + inputCommand.getOutputFile());
        } catch (Exception e) {
            throw new PageLabelsException(e);
        } finally {
            setWorkCompleted();
        }
    } else {
        throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND);
    }

}

From source file:org.sejda.impl.itext.util.PageLabelUtils.java

License:Apache License

/**
 * Maps a map of {@link PdfPageLabel} to a {@link com.lowagie.text.pdf.PdfPageLabels} instance that can be used as input for the PdfCopy.
 * /*from   w w w  .  j  av a2s.  co m*/
 * @param labels
 * @param totalPages
 * @return the resulting {@link PdfPageLabels}
 */
public static PdfPageLabels getLabels(Map<Integer, PdfPageLabel> labels, int totalPages) {
    PdfPageLabels retVal = new PdfPageLabels();
    for (Entry<Integer, PdfPageLabel> entry : labels.entrySet()) {
        PdfPageLabel label;
        if (entry.getKey() <= totalPages) {
            label = entry.getValue();
            retVal.addPageLabel(entry.getKey(), PAGE_NUMBERS_STYLES.get(label.getNumberingStyle()),
                    label.getLabelPrefix(), label.getLogicalPageNumber());
        } else {
            LOG.warn("Page number out of rage, {} will be ignored.", entry.getValue());
        }
    }
    return retVal;
}