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

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

Introduction

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

Prototype

public PDDocumentCatalog getDocumentCatalog() 

Source Link

Document

This will get the document CATALOG.

Usage

From source file:com.shmsoft.dmass.ocr.PDFImageExtractor.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override// w w  w.ja va2  s .  co m
public List<String> extractImages() {
    File extractionDir = new File(conf.getPdfImageExtractionDir());
    extractionDir.mkdirs();

    List<String> result = new ArrayList<String>();

    PDDocument document = null;
    try {
        document = PDDocument.load(file);

        List pages = document.getDocumentCatalog().getAllPages();
        Iterator iter = pages.iterator();
        int i = 1;
        int maxNumberOfImages = Project.getProject().getOcrMaxImagesPerPDF();

        while (iter.hasNext()) {
            PDPage page = (PDPage) iter.next();
            PDResources resources = page.getResources();
            Map pageImages = resources.getImages();
            if (pageImages != null) {
                Iterator imageIter = pageImages.keySet().iterator();
                while (imageIter.hasNext()) {
                    if (i > maxNumberOfImages) {
                        return result;
                    }

                    String key = (String) imageIter.next();
                    PDXObjectImage image = (PDXObjectImage) pageImages.get(key);

                    String fileName = conf.getPdfImageExtractionDir() + OCRUtil.createUniqueFileName("image");
                    image.write2file(fileName);

                    result.add(fileName + "." + image.getSuffix());

                    i++;
                }
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return result;
}

From source file:com.sinefine.util.pdf.Pdfs.java

License:Apache License

/**
 * Returns a list of byte arrays representing the pages of the PDF document as images.
 *
 * @param pdfDocument the PDF document.// w  w w.  ja  v  a  2s  . c o  m
 * @return a list of byte arrays representing the pages of the PDF document as images.
 * @throws IOException if an I/O error occurs during the reading of the document or the creation
 *     of the page images.
 */
private static List<byte[]> toPageImages(final PDDocument pdfDocument) throws IOException {
    @SuppressWarnings("unchecked")
    final List<PDPage> pages = pdfDocument.getDocumentCatalog().getAllPages();
    final List<byte[]> pageImages = new ArrayList<>();
    for (PDPage page : pages) {
        try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            final BufferedImage image = page.convertToImage();
            boolean hasSucceeded = ImageIO.write(image, "png", byteArrayOutputStream);
            if (hasSucceeded) {
                pageImages.add(byteArrayOutputStream.toByteArray());
            }
        }
    }
    return pageImages;
}

From source file:com.verbox.PrintHtml.java

public static void PreImgPrint() throws IOException {

    int resolution = Toolkit.getDefaultToolkit().getScreenResolution();

    String pdfPath = "name_img";

    //load pdf document
    PDDocument document = PDDocument.load("pdf.pdf");

    List<PDPage> pages = document.getDocumentCatalog().getAllPages();

    //Read first page
    PDPage page = pages.get(0);//from  w w  w.  ja v  a  2  s . co m

    //Convert To Image          
    BufferedImage previewImage = page.convertToImage(BufferedImage.TYPE_INT_RGB, resolution);

    //Save to file
    ImageIO.write(previewImage, "png", new File(pdfPath + ".png"));

}

From source file:com.wintindustries.pdffilter.pdfcore.PDFTester.java

static public void printMetadata(PDDocument document) throws IOException {
    PDDocumentInformation info = document.getDocumentInformation();
    PDDocumentCatalog cat = document.getDocumentCatalog();
    PDMetadata metadata = cat.getMetadata();
    System.out.println("Page Count=" + document.getNumberOfPages());
    System.out.println("Title=" + info.getTitle());
    System.out.println("Author=" + info.getAuthor());
    System.out.println("Subject=" + info.getSubject());
    System.out.println("Keywords=" + info.getKeywords());
    System.out.println("Creator=" + info.getCreator());
    System.out.println("Producer=" + info.getProducer());
    System.out.println("Creation Date=" + formatDate(info.getCreationDate()));
    System.out.println("Modification Date=" + formatDate(info.getModificationDate()));
    System.out.println("Trapped=" + info.getTrapped());
    if (metadata != null) {
        System.out.println("Metadata=" + metadata.getInputStreamAsString());
    }/*from  ww w  . j  a v a 2 s.  co  m*/
}

From source file:compressor.Compressor.java

void extract_images(String src, String dest, String img_name) throws IOException {
    PDDocument document = null;
    try {//from  w w  w.  ja  v  a 2  s  .  c o  m
        document = PDDocument.load(src);
    } catch (IOException ex) {
        System.out.println("" + ex);
    }
    List pages = document.getDocumentCatalog().getAllPages();
    Iterator iter = pages.iterator();
    int i = 1;
    String name = null;

    File file = new File(dest + "img");
    if (!file.exists()) {
        if (file.mkdir()) {
            System.out.println("Directory is created!");
        } else {
            System.out.println("Failed to create directory!");
        }
    }
    dest = dest + "img/";

    while (iter.hasNext()) {
        PDPage page = (PDPage) iter.next();
        PDResources resources = page.getResources();
        Map pageImages = resources.getImages();
        if (pageImages != null) {
            Iterator imageIter = pageImages.keySet().iterator();
            while (imageIter.hasNext()) {
                String key = (String) imageIter.next();
                PDXObjectImage image = (PDXObjectImage) pageImages.get(key);

                image.write2file(dest + img_name + i);
                i++;
            }
        }
    }
    document.close();
}

From source file:correccioncolorpdfs.CorrectorColorUI.java

private void transformarPDF() {
    try {/*from   w ww .j a  v a2  s.co  m*/
        //@see http://stackoverflow.com/questions/18189314/convert-a-pdf-file-to-image
        String rutaPDFOriginal = rutaPDF + nombrePDF; // Pdf files are read from this folder
        //String destinationDir = "D:\\Desarrollo\\pruebas\\reportes_negros\\imagenes\\"; // converted images from pdf document are saved here

        File pdfOriginal = new File(rutaPDFOriginal);
        //            File destinationFile = new File(destinationDir);
        //            if (!destinationFile.exists()) {
        //                destinationFile.mkdir();
        //                System.out.println("Folder Created -> " + destinationFile.getAbsolutePath());
        //            }
        if (pdfOriginal.exists()) {
            //System.out.println("Images copied to Folder: " + destinationFile.getName());
            PDDocument document = PDDocument.load(rutaPDFOriginal);

            //Documento Fondo Blanco
            PDDocument documentoCool = new PDDocument();

            List<PDPage> list = document.getDocumentCatalog().getAllPages();
            System.out.println("Total files to be converted -> " + list.size());

            String nombrePDFOriginal = pdfOriginal.getName().replace(".pdf", "");
            int pageNumber = 1;
            for (PDPage page : list) {
                BufferedImage image = page.convertToImage();

                //Inviertiendo colores
                //@see http://stackoverflow.com/questions/8662349/convert-negative-image-to-positive
                for (int x = 0; x < image.getWidth(); x++) {
                    for (int y = 0; y < image.getHeight(); y++) {
                        int rgba = image.getRGB(x, y);
                        //Hexa a reemplazar e9e9e1 R=233|G=233|B=225
                        Color col = new Color(rgba, true);
                        col = new Color(255 - col.getRed(), 255 - col.getGreen(), 255 - col.getBlue());

                        //Si color es igual al invertido - cambiarlo a blanco
                        if (col.getRGB() == -1447455) {
                            col = new Color(255, 255, 255);
                        }
                        //System.out.println("col.getR = " + col.getRGB());
                        image.setRGB(x, y, col.getRGB());
                    }
                }
                //                    File outputfile = new File(destinationDir + fileName + "_" + pageNumber + ".png");
                //                    System.out.println("Image Created -> " + outputfile.getName());
                //                    ImageIO.write(image, "png", outputfile);
                pageNumber++;

                //Crear pagina nueva para el PDF Convertido
                float width = image.getWidth();
                float height = image.getHeight();
                PDPage paginaSinFondo = new PDPage(new PDRectangle(width, height));
                documentoCool.addPage(paginaSinFondo);
                PDXObjectImage img = new PDJpeg(documentoCool, image);
                PDPageContentStream contentStream = new PDPageContentStream(documentoCool, paginaSinFondo);
                contentStream.drawImage(img, 0, 0);
                contentStream.close();

            }
            document.close();
            rutaPDFImprimible = rutaPDF + nombrePDFOriginal + "_imprimible.pdf";
            documentoCool.save(rutaPDFImprimible);
            documentoCool.close();

            estadoConversion(true);
        } else {
            JOptionPane.showMessageDialog(this,
                    "No se logr identificar la ruta del archivo, por favor verifique que el archivo si existe o no halla sido movido durante el proceso.",
                    "Ruta de archivo no encontrada", JOptionPane.WARNING_MESSAGE);
        }

    } catch (IOException | COSVisitorException | HeadlessException e) {
        estadoConversion(false);
        JOptionPane.showMessageDialog(this, e.getMessage(), "Error durante el proceso de conversin",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:cr.ac.siua.tec.utils.impl.AssistancePDFGenerator.java

License:Open Source License

/**
 * Fills the PDF file (asistente.pdf) with the ticket values and returns base64 encoded string.
 *//*from  w w  w  . ja  v a  2s  .  c  o m*/
@Override
public String generate(HashMap<String, String> formValues) {
    String originalPdf = PDFGenerator.RESOURCES_PATH + "asistente.pdf";
    try {
        PDDocument _pdfDocument = PDDocument.load(originalPdf);
        PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();

        //Set some fields manually.
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        if (month > 10 || month < 5)
            acroForm.getField("Semestre").setValue("PRIMER");
        else
            acroForm.getField("Semestre").setValue("SEGUNDO");

        if (month > 10 && acroForm.getField("Semestre").getValue().equals("PRIMER"))
            year++;

        acroForm.getField("Ao").setValue(String.valueOf(year));
        formValues.remove("Queue");
        acroForm.getField(formValues.get("Banco")).setValue("x");
        formValues.remove("Banco");
        acroForm.getField(formValues.get("Tipo de asistencia")).setValue("x");
        formValues.remove("Tipo de asistencia");

        //Iterates through remaining custom fields.
        for (Map.Entry<String, String> entry : formValues.entrySet()) {
            acroForm.getField(entry.getKey()).setValue(entry.getValue());
        }
        return encodePDF(_pdfDocument);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Excepcin al llenar el PDF.");
        return null;
    }
}

From source file:cr.ac.siua.tec.utils.impl.CEInclusionPDFGenerator.java

License:Open Source License

/**
 * Fills the PDF file (inclusion.pdf) with the ticket values and returns base64 encoded string.
 *///  w ww .java 2s.c o  m
@Override
public String generate(HashMap<String, String> formValues) {
    String originalPdf = PDFGenerator.RESOURCES_PATH + "inclusion.pdf";
    try {
        PDDocument _pdfDocument = PDDocument.load(originalPdf);
        PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        formValues.remove("Queue");
        formValues.remove("Justificacin");

        //Set some fields manually.
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        if (month > 10 || month < 5)
            acroForm.getField("Semestre").setValue("I");
        else
            acroForm.getField("Semestre").setValue("II");
        if (month > 10 && acroForm.getField("Semestre").getValue().equals("I"))
            year++;
        acroForm.getField("Ao").setValue(String.valueOf(year));

        //Fills enrolled courses table.
        String enrolledCourses[] = formValues.get("Cursos matriculados").split("\n");
        for (int i = 0; i < enrolledCourses.length; i++) {
            String courseRow[] = enrolledCourses[i].split("-");
            acroForm.getField("Cdigo" + Integer.toString(i)).setValue(courseRow[0]);
            acroForm.getField("Curso" + Integer.toString(i)).setValue(courseRow[1]);
            acroForm.getField("Grupo" + Integer.toString(i)).setValue(courseRow[2]);
        }
        formValues.remove("Cursos matriculados");

        //Iterates through remaining custom fields.
        for (Map.Entry<String, String> entry : formValues.entrySet()) {
            acroForm.getField(entry.getKey()).setValue(entry.getValue());
        }

        return encodePDF(_pdfDocument);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Excepcin al llenar el PDF.");
        return null;
    }
}

From source file:cr.ac.siua.tec.utils.impl.ConstancyPDFGenerator.java

License:Open Source License

/**
 * Fills the PDF file (constancia.pdf) with the ticket values and returns base64 encoded string.
 *//*from   w  w  w .j  a va 2  s.c om*/
@Override
public String generate(HashMap<String, String> formValues) {
    String originalPdf = PDFGenerator.RESOURCES_PATH + "constancia.pdf";
    try {
        PDDocument _pdfDocument = PDDocument.load(originalPdf);
        PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();

        //Set some fields manually.
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        String date = String.valueOf(day) + " de " + monthsMap.get(month) + " del ao " + String.valueOf(year)
                + ".";
        acroForm.getField("Fecha").setValue(date);

        formValues.remove("Queue");
        formValues.remove("Motivo");
        formValues.remove("Requestors");

        //Iterates through remaining custom fields.
        for (Map.Entry<String, String> entry : formValues.entrySet()) {
            acroForm.getField(entry.getKey()).setValue(entry.getValue());
        }
        return encodePDF(_pdfDocument);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Excepcin al llenar el PDF.");
        return null;
    }
}

From source file:cr.ac.siua.tec.utils.impl.ProficiencyPDFGenerator.java

License:Open Source License

/**
 * Fills the PDF file (suficiencia.pdf) with the ticket values and returns base64 encoded string.
 *///from w  ww  .j  av  a  2  s  .  c o m
@Override
public String generate(HashMap<String, String> formValues) {
    String originalPdf = PDFGenerator.RESOURCES_PATH + "suficiencia.pdf";
    try {
        PDDocument _pdfDocument = PDDocument.load(originalPdf);
        PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        formValues.remove("Queue");

        //Iterates through all custom field values.
        for (Map.Entry<String, String> entry : formValues.entrySet()) {
            acroForm.getField(entry.getKey()).setValue(entry.getValue());
        }
        return encodePDF(_pdfDocument);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Excepcin al llenar el PDF.");
        return null;
    }
}