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:org.xwiki.test.misc.PDFTest.java

License:Open Source License

private Map<String, PDImageXObject> getImages(URL url) throws Exception {
    Map<String, PDImageXObject> results = new HashMap<>();

    PDDocument document = PDDocument.load(IOUtils.toByteArray(url));
    try {/* w  ww  .j av a  2  s. c  o m*/
        for (PDPage page : document.getDocumentCatalog().getPages()) {
            PDResources pdResources = page.getResources();
            for (COSName name : pdResources.getXObjectNames()) {
                if (pdResources.isImageXObject(name)) {
                    PDImageXObject pdxObjectImage = (PDImageXObject) pdResources.getXObject(name);
                    results.put(name.getName(), pdxObjectImage);
                }
            }
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }

    return results;
}

From source file:org.xwiki.test.misc.PDFTest.java

License:Open Source License

private Map<String, String> extractToLinks(URL url, int tocPageIndex) throws Exception {
    Map<String, String> internalLinks = new HashMap<String, String>();
    PDDocument document = null;
    try {//from  ww  w.  j  a v  a 2  s.  c o m
        document = PDDocument.load(IOUtils.toByteArray(url));
        PDPage tocPage = document.getDocumentCatalog().getPages().get(tocPageIndex);
        for (Map.Entry<String, PDAction> entry : extractLinks(tocPage).entrySet()) {
            if (entry.getValue() instanceof PDActionGoTo) {
                PDActionGoTo anchor = (PDActionGoTo) entry.getValue();
                internalLinks.put(entry.getKey(), getDestinationText(anchor.getDestination()));
            }
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
    return internalLinks;
}

From source file:org.xwiki.test.misc.PDFTest.java

License:Open Source License

private Map<String, PDAction> extractLinks(PDDocument document) throws Exception {
    Map<String, PDAction> links = new HashMap<String, PDAction>();
    for (PDPage page : document.getDocumentCatalog().getPages()) {
        links.putAll(extractLinks(page));
    }//from www .j a  v a 2 s . c o  m
    return links;
}

From source file:org.zorbaxquery.modules.readPdf.GetImages.java

License:Apache License

public void getXMPInformation(String path) {
    // Open PDF document
    PDDocument document = null;
    try {/*  w  w w. j  ava  2s  . c  o m*/
        document = PDDocument.load(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Get all pages and loop through them
    List pages = document.getDocumentCatalog().getAllPages();
    Iterator iter = pages.iterator();
    int pageNo = 1;
    while (iter.hasNext()) {
        System.out.println("Examining page " + pageNo++ + " :");
        PDPage page = (PDPage) iter.next();
        PDResources resources = page.getResources();
        Map images = null;
        // Get all Images on page
        try {
            images = resources.getImages();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (images != null) {
            // Check all images for metadata
            Iterator imageIter = images.keySet().iterator();
            while (imageIter.hasNext()) {
                String key = (String) imageIter.next();
                PDXObjectImage image = (PDXObjectImage) images.get(key);
                PDMetadata metadata = image.getMetadata();
                System.out.println("Found a image: Analyzing for Metadata");
                if (metadata == null) {
                    System.out.println("No Metadata found for this image.");
                    System.out.println(
                            "image: " + image.getWidth() + "x" + image.getHeight() + " " + image.getSuffix());
                    try {
                        System.out.println("       bitsPerComponent: " + image.getBitsPerComponent()
                                + "  colorSpace: " + image.getColorSpace().getName() + "  hasImageMask: "
                                + image.getImageMask());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    InputStream xmlInputStream = null;
                    try {
                        xmlInputStream = metadata.createInputStream();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        System.out.println(
                                "--------------------------------------------------------------------------------");
                        String mystring = convertStreamToString(xmlInputStream);
                        System.out.println(mystring);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                // Export the images
                String name = getUniqueFileName(key, image.getSuffix());
                System.out.println("Writing image:" + name);
                try {
                    //image.write2file(name);
                    File f = new File(name);
                    OutputStream os = new FileOutputStream(f);
                    image.write2OutputStream(os);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println(
                        "--------------------------------------------------------------------------------");
            }
        }
    }
}

From source file:org.zorbaxquery.modules.readPdf.ZorbaPdfImageWriter.java

License:Apache License

public boolean writeImages(PDDocument document, String imageFormat, int startPage, int endPage, int imageType,
        int resolution, OutputStreamCreator osCreator) throws IOException {
    boolean bSuccess = true;
    List pages = document.getDocumentCatalog().getAllPages();
    for (int i = startPage - 1; i < endPage && i < pages.size(); i++) {
        PDPage page = (PDPage) pages.get(i);
        BufferedImage image = page.convertToImage(imageType, resolution);

        OutputStream outputStream = osCreator.newOutputStream();
        images.add(outputStream);/*from ww w.  j av  a 2  s . c  o  m*/

        bSuccess &= writeImage(image, imageFormat, outputStream, resolution);
        outputStream.close();
    }
    return bSuccess;
}

From source file:paper2ebook.Transformer.java

License:Apache License

/**
 * Output a PDF with as many pages as there are interesting areas in the
 * input document/*from   w w  w . java  2 s .c  o m*/
 */
@Override
public PDDocument extract() throws IOException {
    PDDocument extractedDocument = new PDDocument();
    extractedDocument.setDocumentInformation(sourceDocument.getDocumentInformation());
    extractedDocument.getDocumentCatalog()
            .setViewerPreferences(sourceDocument.getDocumentCatalog().getViewerPreferences());

    @SuppressWarnings("unchecked")
    List<PDPage> pages = sourceDocument.getDocumentCatalog().getAllPages();
    int pageCounter = 1;
    for (PDPage page : pages) {
        if (pageCounter >= startPage && pageCounter <= endPage) {

            List<PDRectangle> zoomedFragments = getFragments(page);
            for (PDRectangle fragment : zoomedFragments) {
                PDPage outputPage = extractedDocument.importPage(page);
                outputPage.setCropBox(fragment);
                outputPage.setMediaBox(page.getMediaBox());
                outputPage.setResources(page.findResources());
                outputPage.setRotation(page.findRotation());

                // TODO: rotate the page in landscape mode is width > height
            }
        }
        pageCounter++;
    }
    return extractedDocument;
}

From source file:PDF.PDFDateStamp.java

public void generatePDFFile(String pdfFileName) throws IOException, COSVisitorException {

    PDDocument pdf = PDDocument.load(pdfFileName);

    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();

    while (iter.hasNext()) {
        PDPage page = iter.next();/*from   www  . java2 s  .  co  m*/
        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);

        // == date stamp
        stream.beginText();
        stream.setFont(PDType1Font.HELVETICA, GlobalVar.DATE_STAMP_FONT_SIZE);
        stream.moveTextPositionByAmount(GlobalVar.DATE_STAMP_X_POSITION, GlobalVar.DATE_STAMP_Y_POSITION);
        stream.drawString(DATE); //date stamp 
        stream.endText();
        // end of date stamp

        stream.close();

    }
    // out put two pdf files: one for audit, the other for reject
    pdfFileName = pdfFileName.replace(".pdf", "_DateStamped.pdf");
    pdf.save(pdfFileName);

    pdf.close();
}

From source file:PDF.PDFDateStampPartial.java

private void generatePDFFile(String pdfFileName, Boolean[][] statusArray, String date)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();
    int pageNum = 0; // 0 based
    //int sequenceNum = 1; // start from 0001
    PDDocument pdfBlank = new PDDocument();

    while (iter.hasNext()) {
        PDPage page = iter.next();//  w w  w.  j  a v  a  2 s  .co  m
        PDPage pageBlank = new PDPage();

        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
        PDPageContentStream streamBlank = new PDPageContentStream(pdfBlank, pageBlank, true, false);

        // == seq stamp
        if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) {
            pageWrite(stream, date);
            pageWrite(streamBlank, date);
        }
        // == end of seq stamp
        pdfBlank.addPage(pageBlank);
        stream.close();
        streamBlank.close();

        pageNum++;
    }

    // out put two pdf files: one is template for printer print hardcopies, the other is digital copy
    String suffix = "_P dateStamped.pdf";
    pdfFileName = pdfFileName.replace(".pdf", suffix);
    String blankPdfFileName = pdfFileName.replace(".pdf", "BLANK.pdf");

    pdf.save(pdfFileName);
    pdfBlank.save(blankPdfFileName);

    pdf.close();
    pdfBlank.close();
}

From source file:PDF.PDFEditor.java

public void generatePDFFile(String date, String pdfFileName, Boolean[][] statusArray, String cycle)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    PDDocument blanPdf = new PDDocument();

    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();
    int pageNum = 0; // 0 based
    int sequenceNum = 1; // start from 0001
    while (iter.hasNext()) {
        PDPage page = iter.next();//ww w .j a va  2  s.  c  om
        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);

        // == date stamp
        stream.beginText();
        stream.setFont(PDType1Font.HELVETICA, 20);
        stream.moveTextPositionByAmount(200, 20);
        stream.drawString(date); //date stamp 
        stream.endText();
        // == end of date stamp

        //             // == void stamp
        //            if (statusArray[GlobalVar.VOID_BUTTON_INDEX][pageNum]) {
        //                stream.drawImage(voidMark, 0, 0);
        //            }
        // == end of void stamp

        // == seq stamp
        if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) {
            stream.beginText();
            stream.setFont(PDType1Font.HELVETICA, 24);
            stream.moveTextPositionByAmount(600, 400);
            stream.setTextRotation(3.14 / 2, 600, 400); // rotate text 90 degree at x = 600, y = 400

            stream.drawString(cycle + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum));
            sequenceNum++;
            stream.endText();
        }
        // == end of seq stamp

        stream.close();
        pageNum++;
    }
    // out put two pdf files: one for audit, the other for reject
    String rejectPdfFileName = pdfFileName.replace(".pdf", "_forReject.pdf");
    String auditPdfFileName = pdfFileName.replace(".pdf", "_forAudit.pdf");
    extractGoodBadPdf(pdf, auditPdfFileName, rejectPdfFileName, statusArray);

    pdf.close();
}

From source file:PDF.PDFEditor.java

private void extractGoodBadPdf(PDDocument pdf, String auditPdfFileName, String rejectPdfFileName,
        Boolean[][] statusArray) throws COSVisitorException, IOException {
    PDDocument rejectPdf = new PDDocument();
    PDDocument auditPdf = new PDDocument();
    int pageNum = pdf.getNumberOfPages();

    // add reject page into rejectPdf
    for (int i = 0; i < pageNum; i++) {
        PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i);
        if (statusArray[GlobalVar.VOID_BUTTON_INDEX][i]) {
            rejectPdf.addPage(page);/*w  w w  .  ja va 2  s.c  om*/
        } else {
            auditPdf.addPage(page);
        }
    }

    rejectPdf.save(rejectPdfFileName);
    rejectPdf.close();

    auditPdf.save(auditPdfFileName);
    auditPdf.close();
}