Example usage for com.lowagie.text.pdf PdfReader consolidateNamedDestinations

List of usage examples for com.lowagie.text.pdf PdfReader consolidateNamedDestinations

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfReader consolidateNamedDestinations.

Prototype

boolean consolidateNamedDestinations

To view the source code for com.lowagie.text.pdf PdfReader consolidateNamedDestinations.

Click Source Link

Usage

From source file:org.sejda.impl.itext.component.ITextOutlineLevelsHandler.java

License:Apache License

@SuppressWarnings({ "cast", "unchecked" })
public ITextOutlineLevelsHandler(PdfReader reader, String matchingTitleRegEx) {
    reader.consolidateNamedDestinations();
    this.bookmarks = (List<Map<String, Object>>) SimpleBookmark.getBookmark(reader);
    if (isNotBlank(matchingTitleRegEx)) {
        titleMatchingPattern = Pattern.compile(matchingTitleRegEx);
    }//  w ww. ja va 2s. com
}

From source file:org.silverpeas.core.importexport.control.ImportExport.java

License:Open Source License

/**
 * @param userDetail/*from w w w. j  a  v  a2s. co m*/
 * @param itemsToExport
 * @return
 * @throws ImportExportException
 */
public ExportPDFReport processExportPDF(UserDetail userDetail, List<WAAttributeValuePair> itemsToExport,
        NodePK rootPK) throws ImportExportException {
    ExportPDFReport report = new ExportPDFReport();
    report.setDateDebut(new Date());

    PublicationsTypeManager pubTypeManager = getPublicationsTypeManager();

    String fileExportName = generateExportDirName(userDetail, "fusion");
    String tempDir = FileRepositoryManager.getTemporaryPath();

    File fileExportDir = new File(tempDir + fileExportName);
    if (!fileExportDir.exists()) {
        try {
            FileFolderManager.createFolder(fileExportDir);
        } catch (org.silverpeas.core.util.UtilException ex) {
            throw new ImportExportException("ImportExport", "importExport.EX_CANT_CREATE_FOLDER", ex);
        }
    }

    File pdfFileName = new File(tempDir + fileExportName + ".pdf");
    try {
        // cration des rpertoires avec le nom des thmes et des publications
        List<AttachmentDetail> pdfList = pubTypeManager.processPDFExport(report, userDetail, itemsToExport,
                fileExportDir.getPath(), true, rootPK);

        try {
            int pageOffset = 0;
            List master = new ArrayList();
            Document document = null;
            PdfCopy writer = null;

            if (!pdfList.isEmpty()) {
                boolean firstPage = true;
                for (AttachmentDetail attDetail : pdfList) {
                    PdfReader reader = null;
                    try {
                        reader = new PdfReader(
                                fileExportDir.getPath() + File.separatorChar + attDetail.getLogicalName());
                    } catch (IOException ioe) {
                        // Attached file is not physically present on disk, ignore it and log event
                        SilverLogger.getLogger(this).error("Cannot find PDF {0}",
                                new String[] { attDetail.getLogicalName() }, ioe);
                    }
                    if (reader != null) {
                        reader.consolidateNamedDestinations();
                        int nbPages = reader.getNumberOfPages();
                        List bookmarks = SimpleBookmark.getBookmark(reader);
                        if (bookmarks != null) {
                            if (pageOffset != 0) {
                                SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                            }
                            master.addAll(bookmarks);
                        }
                        pageOffset += nbPages;

                        if (firstPage) {
                            document = new Document(reader.getPageSizeWithRotation(1));
                            writer = new PdfCopy(document, new FileOutputStream(pdfFileName));
                            document.open();
                            firstPage = false;
                        }

                        for (int i = 1; i <= nbPages; i++) {
                            try {
                                PdfImportedPage page = writer.getImportedPage(reader, i);
                                writer.addPage(page);
                            } catch (Exception e) {
                                // Can't import PDF file, ignore it and log event
                                SilverLogger.getLogger(this).error("Cannot merge PDF {0}",
                                        new String[] { attDetail.getLogicalName() }, e);
                            }
                        }

                        PRAcroForm form = reader.getAcroForm();
                        if (form != null) {
                            writer.copyAcroForm(reader);
                        }
                    }
                }

                if (!master.isEmpty()) {
                    writer.setOutlines(master);
                }
                writer.flush();
                document.close();
            } else {
                return null;
            }

        } catch (DocumentException e) {
            // Impossible de copier le document
            throw new ImportExportException("ImportExport", "root.EX_CANT_WRITE_FILE", e);
        }

    } catch (IOException e) {
        // Pb avec le rpertoire de destination
        throw new ImportExportException("ImportExport", "root.EX_CANT_WRITE_FILE", e);
    }

    report.setPdfFileName(pdfFileName.getName());
    report.setPdfFileSize(pdfFileName.length());
    report.setPdfFilePath(FileServerUtils.getUrlToTempDir(pdfFileName.getName()));

    report.setDateFin(new Date());

    return report;
}

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

License:Open Source License

/**
 * merge/*  w  w w  .ja v  a  2 s.c  o 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: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
 *///from w  w w . j a  va 2 s  .  com
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);
    }
}