Example usage for org.apache.pdfbox.pdmodel PDDocument save

List of usage examples for org.apache.pdfbox.pdmodel PDDocument save

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument save.

Prototype

public void save(OutputStream output) throws IOException 

Source Link

Document

This will save the document to an output stream.

Usage

From source file:mil.tatrc.physiology.utilities.Excel2PDF.java

License:Apache License

public static void convert(String from, String to) throws IOException {
    FileInputStream xlFile = new FileInputStream(new File(from));
    // Read workbook into HSSFWorkbook
    XSSFWorkbook xlWBook = new XSSFWorkbook(xlFile);
    //We will create output PDF document objects at this point
    PDDocument pdf = new PDDocument();

    //pdf.addTitle();
    for (int s = 0; s < xlWBook.getNumberOfSheets(); s++) {
        XSSFSheet xlSheet = xlWBook.getSheetAt(s);
        Log.info("Processing Sheet : " + xlSheet.getSheetName());
        PDPage page = new PDPage(PDRectangle.A4);
        page.setRotation(90);//from   w w w  .  j a va 2 s .c o m
        pdf.addPage(page);
        PDRectangle pageSize = page.getMediaBox();
        PDPageContentStream contents = new PDPageContentStream(pdf, page);
        contents.transform(new Matrix(0, 1, -1, 0, pageSize.getWidth(), 0));// including a translation of pageWidth to use the lower left corner as 0,0 reference
        contents.setFont(PDType1Font.HELVETICA_BOLD, 16);
        contents.beginText();
        contents.newLineAtOffset(50, pageSize.getWidth() - 50);
        contents.showText(xlSheet.getSheetName());
        contents.endText();
        contents.close();

        int rows = xlSheet.getPhysicalNumberOfRows();
        for (int r = 0; r < rows; r++) {
            XSSFRow row = xlSheet.getRow(r);
            if (row == null)
                continue;
            int cells = row.getPhysicalNumberOfCells();
            if (cells == 0)
                continue;// Add an empty Roe

        }
    }

    /*    
        //We will use the object below to dynamically add new data to the table
        PdfPCell table_cell;
        //Loop through rows.
        while(rowIterator.hasNext()) 
        {
          Row row = rowIterator.next(); 
          Iterator<Cell> cellIterator = row.cellIterator();
          while(cellIterator.hasNext()) 
          {
            Cell cell = cellIterator.next(); //Fetch CELL
            switch(cell.getCellType()) 
            { //Identify CELL type
              //you need to add more code here based on
              //your requirement / transformations
              case Cell.CELL_TYPE_STRING:
    //Push the data from Excel to PDF Cell
    table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));
    //feel free to move the code below to suit to your needs
    my_table.addCell(table_cell);
    break;
            }
            //next line
          }
        }
    */
    pdf.save(new File(to));
    pdf.close();
    xlWBook.close();
    xlFile.close(); //close xls
}

From source file:model.objects.Project.java

License:Apache License

public void savePDF(final String firstPath) throws IOException {

    PDDocument doc = document.getPDDocument();
    System.out.println(doc);/*from  www .j  a  va2 s. co  m*/
    try {

        final boolean newlyCreated = (doc == null);

        if (newlyCreated) {

            // create new document and insert empty page to the document.
            doc = new PDDocument();
        } else {
            // reset wrong settings.
            State.setBorderBottomPercentExport(0);
            State.setBorderRightPercentExport(0);
            State.setBorderLeftPercentExport(0);
            State.setBorderTopPercentExport(0);
            State.setExportAlpha(true);
        }

        for (int i = 0; i < pictures.length; i++) {
            int index = i + 0;
            if (newlyCreated) {
                index = -1;
            }

            attatchToPDF(doc, pictures[i].getBufferedImage(0, 0, false), index);
        }

        //save and close
        doc.save(firstPath);
    } finally {
        if (doc != null) {
            //                doc.close();
        }
    }
}

From source file:model.util.pdf.PDFUtils.java

License:Apache License

/**
 * Test: Create pdf from image and pdf file.
 *
 * @param _inputFile    pdf input/*from   ww w .  j av a  2s  .  c o m*/
 * @param _imagePath    image input
 * @param _outputFile    pdf output
 *
 * @throws IOException occurs if reading the data fails.
 */
public void saveAsPdf(String _imagePath, String _outputFile) throws IOException {

    PDDocument doc = null;
    try {

        // create new document and insert empty page to the document.
        doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);

        // createFromFile is the easiest way with an image file
        // if you already have the image in a BufferedImage, 
        // call LosslessFactory.createFromImage() instead
        PDImageXObject pdImage = PDImageXObject.createFromFile(_imagePath, doc);
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);

        // contentStream.drawImage(ximage, 20, 20 );
        // better method inspired by http://stackoverflow.com/a/22318681/535646
        // reduce this value if the image is too large
        float scale = 1f;
        contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth() * scale, pdImage.getHeight() * scale);

        contentStream.close();
        doc.save(_outputFile);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:name.marcelomorales.siqisiqi.pdfbox.CoordinatesGenerator.java

License:Apache License

public void generarPdf(OutputStream os, String template, Map<String, Object> m, String path, String coordenates,
        float fontSize, float ancho) throws IOException {
    long t = System.currentTimeMillis();
    PDDocument doc = null;
    try {//from   ww w . j a  v a 2 s. c o  m
        doc = PDDocument.load(new File(path));

        List pages = doc.getDocumentCatalog().getAllPages();

        PDPage sourcePage = (PDPage) pages.get(0);

        boolean append = sourcePage.getContents() != null;
        PDPageContentStream contentStream = new PDPageContentStream(doc, sourcePage, append, true);

        StringReader fileReader = null;
        try {

            fileReader = new StringReader(template);
            List<String> list = CharStreams.readLines(fileReader);
            boolean textHasBegun = false;
            float currentOffset = 0f;
            for (String line : list) {

                if (line == null) {
                    continue;
                }

                if (line.startsWith("#")) {
                    continue;
                }

                final Iterable<String> str = Splitter.on(',').omitEmptyStrings().trimResults().split(line);
                final String[] split = Iterables.toArray(str, String.class);
                if (split == null || split.length < 4) {
                    continue;
                }

                if (Character.isDigit(split[0].charAt(0))) {
                    if (textHasBegun) {
                        contentStream.endText();
                    }
                    contentStream.beginText();
                    textHasBegun = true;
                    contentStream.moveTextPositionByAmount(parseFloat(split[0]), parseFloat(split[1]));
                } else {
                    contentStream.moveTextPositionByAmount(currentOffset, 0);
                }

                if (!textHasBegun) {
                    LOGGER.warn("Hay un posible mal uso de un .ree", new Throwable());
                    contentStream.beginText();
                    textHasBegun = true;
                }

                PDType1Font font;
                if ("b".equals(split[2])) {
                    font = HELVETICA_BOLD;
                } else {
                    font = HELVETICA;
                }
                contentStream.setFont(font, fontSize);

                Object text = null;
                if (split[3].startsWith("\"")) {
                    // TODO: text = substring(split[3], 1, -1);
                } else {
                    // TODO: text = new PropertyModel(m, split[3]).getObject();
                }

                if (text == null) {
                    LOGGER.warn("Propiedad {} no se encuentra", split[3]);
                    //contentStream.drawString("ERROR: propiedad no encontrada");
                    contentStream.drawString(" ");
                } else {
                    String string = text.toString();
                    currentOffset = font.getStringWidth(string) * ancho;
                    contentStream.drawString(string);
                }
            }

            if (textHasBegun) {
                contentStream.endText();
            }
        } finally {
            Closeables.closeQuietly(fileReader);
        }

        contentStream.close();

        try {
            doc.save(os);
        } catch (COSVisitorException e) {
            throw new IOException("Ha ocurrido un error al escribir en el Os", e);
        }
    } finally {
        if (doc != null) {
            doc.close();
        }
        LOGGER.info("Me ha tomado {} milisegundos hacer el pdf", System.currentTimeMillis() - t);
    }
}

From source file:net.bookinaction.ExtractAnnotations.java

License:Apache License

public void doJob(String job, Float[] pA) throws IOException {

    PDDocument document = null;

    Stamper s = new Stamper(); // utility class

    final String job_file = job + ".pdf";
    final String dic_file = job + "-dict.txt";
    final String new_job = job + "-new.pdf";

    PrintWriter writer = new PrintWriter(dic_file);

    ImageLocationListener imageLocationsListener = new ImageLocationListener();
    AnnotationMaker annotMaker = new AnnotationMaker();

    try {/*from  ww w  . j  av  a2s.c  o  m*/
        document = PDDocument.load(new File(job_file));

        int pageNum = 0;
        for (PDPage page : document.getPages()) {
            pageNum++;

            PDRectangle cropBox = page.getCropBox();

            List<PDAnnotation> annotations = page.getAnnotations();

            // extract image locations
            List<Rectangle2D> imageRects = new ArrayList<Rectangle2D>();
            imageLocationsListener.setImageRects(imageRects);
            imageLocationsListener.processPage(page);

            int im = 0;
            for (Rectangle2D pdImageRect : imageRects) {
                s.recordImage(writer, pageNum, "[im" + im + "]", (Rectangle2D.Float) pdImageRect);
                annotations.add(annotMaker.squareAnnotation(Color.YELLOW, (Rectangle2D.Float) pdImageRect,
                        "[im" + im + "]"));
                im++;
            }

            PDFTextStripperByArea stripper = new PDFTextStripperByArea();

            int j = 0;
            List<PDAnnotation> viableAnnots = new ArrayList();

            for (PDAnnotation annot : annotations) {
                if (annot instanceof PDAnnotationTextMarkup || annot instanceof PDAnnotationLink) {

                    stripper.addRegion(Integer.toString(j++), s.getAwtRect(
                            s.adjustedRect(annot.getRectangle(), pA[0], pA[1], pA[2], pA[3]), cropBox));
                    viableAnnots.add(annot);

                } else if (annot instanceof PDAnnotationPopup || annot instanceof PDAnnotationText) {
                    viableAnnots.add(annot);

                }
            }

            stripper.extractRegions(page);

            List<PDRectangle> rects = new ArrayList<PDRectangle>();

            List<String> comments = new ArrayList<String>();
            List<String> highlightTexts = new ArrayList<String>();

            j = 0;
            for (PDAnnotation viableAnnot : viableAnnots) {

                if (viableAnnot instanceof PDAnnotationTextMarkup) {
                    String highlightText = stripper.getTextForRegion(Integer.toString(j++));
                    String withoutCR = highlightText.replace((char) 0x0A, '^');

                    String comment = viableAnnot.getContents();

                    String colorString = String.format("%06x", viableAnnot.getColor().toRGB());

                    PDRectangle aRect = s.adjustedRect(viableAnnot.getRectangle(), pA[4], pA[5], pA[6], pA[7]);
                    rects.add(aRect);
                    comments.add(comment);
                    highlightTexts.add(highlightText);

                    s.recordTextMarkup(writer, pageNum, comment, withoutCR, aRect, colorString);

                } else if (viableAnnot instanceof PDAnnotationText) {
                    String comment = viableAnnot.getContents();
                    String colorString = String.format("%06x", viableAnnot.getColor().toRGB());

                    for (Rectangle2D pdImageRect : imageRects) {
                        if (pdImageRect.contains(viableAnnot.getRectangle().getLowerLeftX(),
                                viableAnnot.getRectangle().getLowerLeftY())) {
                            s.recordTextMarkup(writer, pageNum, comment, "", (Rectangle2D.Float) pdImageRect,
                                    colorString);
                            annotations.add(annotMaker.squareAnnotation(Color.GREEN,
                                    (Rectangle2D.Float) pdImageRect, comment));
                        }
                        ;
                    }
                }
            }
            PDPageContentStream canvas = new PDPageContentStream(document, page, true, true, true);

            int i = 0;
            for (PDRectangle pdRect : rects) {
                String comment = comments.get(i);
                String highlightText = highlightTexts.get(i);
                //annotations.add(linkAnnotation(pdRect, comment, highlightText));
                //annotations.add(annotationSquareCircle(pdRect, BLUE));
                s.showBox(canvas, new Rectangle2D.Float(pdRect.getLowerLeftX(), pdRect.getUpperRightY(),
                        pdRect.getWidth(), pdRect.getHeight()), cropBox, Color.BLUE);

                i++;
            }
            canvas.close();
        }
        writer.close();
        document.save(new_job);

    } finally {
        if (document != null) {
            document.close();
        }

    }

}

From source file:net.bookinaction.TextInfoExtractor.java

License:Apache License

public void doTextPosition(String source, String coord_text, StripperParam stripperParam) throws IOException {

    String source_pdf = source;//from   w ww  .j  a v a2 s. c  o  m
    String new_file = source.split("\\.")[0] + "-new.pdf";

    PDDocument document = PDDocument.load(new File(source_pdf));

    PrintWriter writer = new PrintWriter(new File(coord_text));

    //s.recordHeader(writer, source_pdf, document.getNumberOfPages(), sParam);

    for (int i = 0; i < document.getNumberOfPages(); i++) {
        getTextPositionFromPage(document, stripperParam, i + 1, writer, true);

    }

    if (document != null) {
        document.save(new_file);
        document.close();
    }

    if (writer != null)
        writer.close();

}

From source file:net.sf.jabref.plugins.pdftasks.PDFTaskSidePane.java

License:Open Source License

private void doTasks() {

    // PDF file type representation
    final ExternalFileType pdf_type = Globals.prefs.getExternalFileTypeByExt("pdf");

    // get Bibtex database associated with the current tab
    final BasePanel db_panel = frame.basePanel();
    final BibtexDatabase db = db_panel.database();
    final MetaData db_meta = db_panel.metaData();

    // get selected Bibtex entries from the current tab
    final BibtexEntry[] db_entries = db_panel.getSelectedEntries();

    // get Bibtex database file for current tab
    final File db_file = db_panel.getFile();
    if (db_file == null || db_file.getParentFile() == null) {
        JOptionPane.showMessageDialog(frame, "Bibtex database must be saved before performing PDF tasks.",
                title, JOptionPane.INFORMATION_MESSAGE);
        return;//w w w.j a v  a2  s  .  c o  m
    }

    // get array of directories that PDF files could possibly be in
    final List<File> db_dirs = new LinkedList<File>();
    for (String dir : db_meta.getFileDirectory(GUIGlobals.FILE_FIELD)) {
        db_dirs.add(new File(dir));
    }
    if (db_dirs.size() == 0 || !db_dirs.contains(db_file.getParentFile())) {
        db_dirs.add(db_file.getParentFile());
    }

    // return if no entries are selected
    if (db_entries.length == 0) {
        JOptionPane.showMessageDialog(frame, "No entries selected for PDF tasks.", title,
                JOptionPane.INFORMATION_MESSAGE);
        return;
    }

    // get PDF file directory
    final File pdf_dir = absoluteFile(pdf_dir_txt.getText(), db_file.getParentFile());

    // do tasks encapsulated in a worker-thread class
    AbstractWorker tasks = new AbstractWorker() {

        boolean cancelled = false;
        boolean confirmed = false;
        boolean erred = false;

        // get user confirmation for PDF file modifications
        private boolean getUserConfirmation() {
            if (!confirmed) {
                confirmed = JOptionPane.showConfirmDialog(frame,
                        "Are you sure you want to rename, move, and/or modify PDF files?\n"
                                + "This operations cannot be undone.",
                        title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
            }
            return confirmed;
        }

        public void init() {

            // block main window
            frame.block();

        }

        public void run() {

            // for debugging purposes
            final boolean modifyDatabase = true;

            // iterate over selected Bibtex entries
            int entry_count = 0;
            for (BibtexEntry entry : db_entries) {
                ++entry_count;

                // get Bibtex key, check is not null
                String key = entry.getCiteKey();
                if (key == null || key.length() == 0) {
                    JOptionPane.showMessageDialog(frame,
                            "BibTeX entry '" + entry.getId() + "' does not have a key!", title,
                            JOptionPane.ERROR_MESSAGE);
                    erred = true;
                    return;
                }

                // update status bar
                frame.output(String.format("Processing BibTeX entry: %s (%d of %d)...", key, entry_count,
                        db_entries.length));

                // get table of file links for this Bibtex entry
                FileListTableModel files = new FileListTableModel();
                files.setContent(entry.getField(GUIGlobals.FILE_FIELD));

                for (int fileindex = 0; fileindex < files.getRowCount(); ++fileindex) {
                    FileListEntry file_entry = files.getEntry(fileindex);

                    // skip if this is not a PDF file link
                    if (!file_entry.getType().equals(pdf_type))
                        continue;

                    // get PDF file
                    File pdf_file = null;
                    for (File db_dir : db_dirs) {
                        pdf_file = absoluteFile(file_entry.getLink(), db_dir);
                        if (pdf_file.isFile()) {
                            break;
                        }
                        pdf_file = null;
                    }
                    if (pdf_file == null) {
                        String errmsg = "Could not find PDF file '" + file_entry.getLink() + "' in '"
                                + db_dirs.get(0);
                        for (int i = 1; i < db_dirs.size(); ++i) {
                            errmsg += "', '" + db_dirs.get(i);
                        }
                        errmsg += "'!";
                        JOptionPane.showMessageDialog(frame, errmsg, title, JOptionPane.ERROR_MESSAGE);
                        erred = true;
                        return;
                    }

                    // get PDF file description
                    String pdf_desc = file_entry.getDescription();

                    // new PDF file
                    File new_pdf_file = pdf_file;

                    // rename PDF file
                    if (rename_pdfs_chk.isSelected()) {

                        // build new PDF name
                        String new_name = key;
                        if (!pdf_desc.isEmpty()) {
                            new_name += "_" + pdf_desc.replace(" ", "_");
                        }
                        new_name += "." + pdf_type.getExtension();

                        // set new PDF file
                        new_pdf_file = absoluteFile(new_name, new_pdf_file.getParentFile());

                    }

                    // move PDF file
                    if (move_to_pdf_dir_chk.isSelected()) {
                        new_pdf_file = absoluteFile(new_pdf_file.getName(), pdf_dir);
                    }

                    // if PDF file needs to be moved
                    if (!new_pdf_file.equals(pdf_file)) {

                        // get user confirmation
                        if (!getUserConfirmation()) {
                            cancelled = true;
                            return;
                        }

                        // perform move/rename operations
                        if (modifyDatabase) {
                            String errmsg = "";
                            try {

                                // create parent directories
                                File new_pdf_dir = new_pdf_file.getParentFile();
                                if (new_pdf_dir != null && !new_pdf_dir.isDirectory()) {
                                    errmsg = "Could not create directory '"
                                            + new_pdf_file.getParentFile().getPath() + "'";
                                    erred = !new_pdf_file.getParentFile().mkdirs();
                                }
                                if (!erred) {

                                    // check if PDF file already exists, and ask for confirmation to replace it
                                    if (new_pdf_file.isFile()) {
                                        switch (JOptionPane.showConfirmDialog(frame,
                                                "PDF file '" + new_pdf_file.getPath() + "' already exists.\n"
                                                        + "Are you sure you want to replace it with "
                                                        + "PDF file '" + pdf_file.getPath() + "'?\n"
                                                        + "This operation cannot be undone.",
                                                title, JOptionPane.YES_NO_CANCEL_OPTION)) {
                                        case JOptionPane.NO_OPTION:
                                            continue;
                                        case JOptionPane.CANCEL_OPTION:
                                            cancelled = true;
                                            return;
                                        case JOptionPane.YES_OPTION:
                                            errmsg = "Could not delete PDF file '" + new_pdf_file.getPath()
                                                    + "'";
                                            erred = !new_pdf_file.delete();
                                        }
                                    }
                                    // otherwise test that we can create the new PDF file
                                    else {
                                        errmsg = "Could not access PDF file '" + new_pdf_file.getPath() + "'";
                                        erred = !new_pdf_file.createNewFile() || !new_pdf_file.delete();
                                    }

                                    if (!erred) {

                                        // try to move/rename PDF file
                                        errmsg = "Could not rename PDF file '" + pdf_file.getPath() + "' to '"
                                                + new_pdf_file.getPath() + "'";
                                        erred = !pdf_file.renameTo(new_pdf_file);

                                    }

                                }
                            }

                            // possible exceptions
                            catch (SecurityException e) {
                                erred = true;
                                errmsg += ": insufficient permissions";
                            } catch (IOException e) {
                                e.printStackTrace();
                                erred = true;
                                errmsg += ": an I/O exception occurred";
                            }
                            if (erred) {
                                JOptionPane.showMessageDialog(frame, errmsg + ".", title,
                                        JOptionPane.ERROR_MESSAGE);
                                return;
                            }

                            // everything was successful
                            pdf_file = new_pdf_file;

                        }
                    }

                    // update file entry table and Bibtex entry
                    file_entry.setLink(relativePath(pdf_file, db_dirs.get(0)));
                    if (modifyDatabase) {
                        String new_files = files.getStringRepresentation();
                        if (!new_files.equals(entry.getField(GUIGlobals.FILE_FIELD))) {
                            entry.setField(GUIGlobals.FILE_FIELD, new_files);
                            db_panel.markNonUndoableBaseChanged();
                        }
                    }

                    // perform operations on PDF file contents
                    if (write_pdf_docinfo_chk.isSelected()) {

                        if (erase_pdf_docinfo_chk.isSelected()) {

                            // get user confirmation
                            if (!getUserConfirmation()) {
                                cancelled = true;
                                return;
                            }

                            // open PDF file
                            PDDocument document = null;
                            try {
                                document = PDDocument.load(pdf_file);
                            } catch (IOException e) {
                                e.printStackTrace();
                                erred = true;
                                JOptionPane.showMessageDialog(frame,
                                        "Could not open PDF file '" + pdf_file.getPath()
                                                + "': an I/O exception occurred.",
                                        title, JOptionPane.ERROR_MESSAGE);
                                return;
                            }

                            // erase document information
                            document.setDocumentInformation(new PDDocumentInformation());

                            // erase XML metadata
                            document.getDocumentCatalog().setMetadata(null);

                            // save and close PDF file
                            try {
                                document.save(pdf_file.getPath());
                                document.close();
                            } catch (COSVisitorException e) {
                                e.printStackTrace();
                                erred = true;
                                JOptionPane.showMessageDialog(frame,
                                        "Could not save PDF file '" + pdf_file.getPath()
                                                + "': an exception occurred.",
                                        title, JOptionPane.ERROR_MESSAGE);
                                return;
                            } catch (IOException e) {
                                e.printStackTrace();
                                erred = true;
                                JOptionPane.showMessageDialog(frame,
                                        "Could not save/close PDF file '" + pdf_file.getPath()
                                                + "': an I/O exception occurred.",
                                        title, JOptionPane.ERROR_MESSAGE);
                                return;
                            }

                        }

                        // write XMP / PDF document catalog metadata
                        try {
                            XMPUtil.writeXMP(pdf_file, entry, db);
                        } catch (IOException e) {
                            e.printStackTrace();
                            erred = true;
                            JOptionPane.showMessageDialog(frame,
                                    "Could not write XMP to PDF file '" + pdf_file.getPath()
                                            + "': an I/O exception occurred.",
                                    title, JOptionPane.ERROR_MESSAGE);
                            return;
                        } catch (TransformerException e) {
                            e.printStackTrace();
                            erred = true;
                            JOptionPane
                                    .showMessageDialog(frame,
                                            "Could not write XMP to PDF file '" + pdf_file.getPath()
                                                    + "': an exception occurred.",
                                            title, JOptionPane.ERROR_MESSAGE);
                            return;
                        }

                    }

                }

            }

        }

        public void update() {

            // unblock main window
            frame.unblock();

            // print to status bar
            if (erred) {
                frame.output("An error occurred during PDF Tasks");
            } else if (cancelled) {
                frame.output("Cancelled PDF Tasks");
            } else {
                frame.output("Completed PDF Tasks");
            }

        }

    };

    // run task thread (based on code in BasePanel.runCommand())
    try {
        tasks.init();
        tasks.getWorker().run();
        tasks.getCallBack().update();
    } catch (Throwable e) {
        frame.unblock();
        e.printStackTrace();
    }

}

From source file:net.sf.jabref.util.XMPUtil.java

License:Open Source License

/**
 * Try to write the given BibTexEntry in the XMP-stream of the given
 * PDF-file./*w ww.  j ava 2  s . co  m*/
 * 
 * Throws an IOException if the file cannot be read or written, so the user
 * can remove a lock or cancel the operation.
 * 
 * The method will overwrite existing BibTeX-XMP-data, but keep other
 * existing metadata.
 * 
 * @param file
 *            The file to write the entries to.
 * @param bibtexEntries
 *            The entries to write to the file. *
 * @param database
 *            maybenull An optional database which the given bibtex entries
 *            belong to, which will be used to resolve strings. If the
 *            database is null the strings will not be resolved.
 * @param writePDFInfo
 *            Write information also in PDF document properties
 * @throws TransformerException
 *             If the entry was malformed or unsupported.
 * @throws IOException
 *             If the file could not be written to or could not be found.
 */
@SuppressWarnings("unchecked")
public static void writeXMP(File file, Collection<BibtexEntry> bibtexEntries, BibtexDatabase database,
        boolean writePDFInfo) throws IOException, TransformerException {

    if (database != null) {
        bibtexEntries = database.resolveForStrings(bibtexEntries, false);
    }

    PDDocument document = null;

    try {
        document = PDDocument.load(file.getAbsoluteFile());
        if (document.isEncrypted()) {
            throw new EncryptionNotSupportedException("Error: Cannot add metadata to encrypted document.");
        }

        if (writePDFInfo && (bibtexEntries.size() == 1)) {
            XMPUtil.writeDocumentInformation(document, bibtexEntries.iterator().next(), null);
            XMPUtil.writeDublinCore(document, bibtexEntries, null);
        }

        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDMetadata metaRaw = catalog.getMetadata();

        XMPMetadata meta;
        if (metaRaw != null) {
            meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
        } else {
            meta = new XMPMetadata();
        }
        meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class);

        // Remove all current Bibtex-schemas
        List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
        for (XMPSchema schema : schemas) {
            XMPSchemaBibtex bib = (XMPSchemaBibtex) schema;
            bib.getElement().getParentNode().removeChild(bib.getElement());
        }

        for (BibtexEntry e : bibtexEntries) {
            XMPSchemaBibtex bibtex = new XMPSchemaBibtex(meta);
            meta.addSchema(bibtex);
            bibtex.setBibtexEntry(e, null);
        }

        // Save to stream and then input that stream to the PDF
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        meta.save(os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        PDMetadata metadataStream = new PDMetadata(document, is, false);
        catalog.setMetadata(metadataStream);

        // Save
        try {
            document.save(file.getAbsolutePath());
        } catch (COSVisitorException e) {
            throw new TransformerException("Could not write XMP-metadata: " + e.getLocalizedMessage());
        }

    } finally {
        if (document != null) {
            document.close();
        }
    }
}

From source file:nominas.sei.form.Principal.java

private void ordenaNominas(String rutaEntrada, String rutaSalida) {
    ArrayList<PaginaNomina> paginasNomina = new ArrayList<PaginaNomina>();

    for (int x = 0; x < 1; x++) {//RECORREMOS EL ARREGLO CON LOS NOMBRES DE ARCHIVO

        try {//w  w  w.ja  v a 2  s  .  c  o  m
            PDDocument pd = PDDocument.load(rutaEntrada); //CARGAR EL PDF
            List l = pd.getDocumentCatalog().getAllPages();//NUMERO LAS PAGINAS DEL ARCHIVO
            Object[] obj = l.toArray();//METO EN UN OBJETO LA LISTA DE PAGINAS PARA MANIPULARLA
            for (int i = 0; i < l.size(); i++) {
                PDPage page = (PDPage) obj[i];//PAGE ES LA PAGINA 1 DE LA QUE CONSTA EL ARCHIVO
                PageFormat pageFormat = pd.getPageFormat(0);//PROPIEDADES DE LA PAGINA (FORMATO)
                Double d1 = new Double(pageFormat.getHeight());//ALTO
                Double d2 = new Double(pageFormat.getWidth());//ANCHO
                int width = d1.intValue();//ANCHO
                int eigth = 1024;//ALTO

                PDFTextStripperByArea stripper = new PDFTextStripperByArea();//COMPONENTE PARA ACCESO AL TEXTO
                Rectangle rect = new Rectangle(0, 0, width, eigth);//DEFNIR AREA DONDE SE BUSCARA EL TEXTO
                stripper.addRegion("area1", rect);//REGISTRAMOS LA REGION CON UN NOMBRE
                stripper.extractRegions(page);//EXTRAE TEXTO DEL AREA

                String contenido = new String();//CONTENIDO = A LO QUE CONTENGA EL AREA O REGION
                contenido = (stripper.getTextForRegion("area1"));
                String[] lines = contenido.split("[\\r\\n]+");
                String nombre = lines[1].substring(28, lines[1].length() - 10);//Separamos el nombre
                PaginaNomina nomina = new PaginaNomina(page, nombre);
                paginasNomina.add(nomina);
            }
            Collections.sort(paginasNomina);
            // Create a new empty document
            PDDocument document = new PDDocument();

            for (int i = 0; i < paginasNomina.size(); i++) {
                System.out.println(paginasNomina.get(i).getNombre());
                document.addPage(paginasNomina.get(i).getPagina());
            }
            // Save the newly created document
            document.save(rutaSalida);

            // finally make sure that the document is properly
            // closed.
            document.close();
            pd.close();//CERRAMOS OBJETO ACROBAT
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } //CATCH
    } //FOR
}

From source file:nominas.sei.NominasSEI.java

/**
 * @param args the command line arguments
 *///from   w  w w.  java 2s.  c om
public static void main(String[] args) {

    ArrayList<PaginaNomina> paginasNomina = new ArrayList<PaginaNomina>();

    for (int x = 0; x < 1; x++) {//RECORREMOS EL ARREGLO CON LOS NOMBRES DE ARCHIVO
        String ruta = new String();//VARIABLE QUE DETERMINARA LA RUTA DEL ARCHIVO A LEER.
        ruta = (".\\NOMINAS.pdf"); //SE ALMACENA LA RUTA DEL ARCHIVO A LEER. 

        try {
            PDDocument pd = PDDocument.load(ruta); //CARGAR EL PDF
            List l = pd.getDocumentCatalog().getAllPages();//NUMERO LAS PAGINAS DEL ARCHIVO
            Object[] obj = l.toArray();//METO EN UN OBJETO LA LISTA DE PAGINAS PARA MANIPULARLA
            for (int i = 0; i < l.size(); i++) {
                PDPage page = (PDPage) obj[i];//PAGE ES LA PAGINA 1 DE LA QUE CONSTA EL ARCHIVO
                PageFormat pageFormat = pd.getPageFormat(0);//PROPIEDADES DE LA PAGINA (FORMATO)
                Double d1 = new Double(pageFormat.getHeight());//ALTO
                Double d2 = new Double(pageFormat.getWidth());//ANCHO
                int width = d1.intValue();//ANCHO
                int eigth = 1024;//ALTO

                PDFTextStripperByArea stripper = new PDFTextStripperByArea();//COMPONENTE PARA ACCESO AL TEXTO
                Rectangle rect = new Rectangle(0, 0, width, eigth);//DEFNIR AREA DONDE SE BUSCARA EL TEXTO
                stripper.addRegion("area1", rect);//REGISTRAMOS LA REGION CON UN NOMBRE
                stripper.extractRegions(page);//EXTRAE TEXTO DEL AREA

                String contenido = new String();//CONTENIDO = A LO QUE CONTENGA EL AREA O REGION
                contenido = (stripper.getTextForRegion("area1"));
                String[] lines = contenido.split("[\\r\\n]+");
                String nombre = lines[1].substring(28, lines[1].length() - 10);
                PaginaNomina nomina = new PaginaNomina(page, nombre);
                paginasNomina.add(nomina);
            }
            Collections.sort(paginasNomina);
            // Create a new empty document
            PDDocument document = new PDDocument();

            for (int i = 0; i < paginasNomina.size(); i++) {
                System.out.println(paginasNomina.get(i).getNombre());
                document.addPage(paginasNomina.get(i).getPagina());
            }
            // Save the newly created document
            document.save("NominasOrdenadas.pdf");

            // finally make sure that the document is properly
            // closed.
            document.close();
            pd.close();//CERRAMOS OBJETO ACROBAT
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } //CATCH
    } //FOR

}