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:pdfcompressor.PDFCompressor.java

public BufferedImage getBytePreview(int pageNum) throws IOException {
    PageExtractor extractor = new PageExtractor(sourceDocument, pageNum, pageNum + 1);
    PDDocument extractedDoc = extractor.extract();
    PDPage page = (PDPage) extractedDoc.getDocumentCatalog().getAllPages().get(0);
    extractedDoc.close();/*from   w w w .ja v a  2  s  .  com*/
    return ImageIO.read(new ByteArrayInputStream(
            getImageByteArray(page.convertToImage(BufferedImage.TYPE_INT_BGR, dpi), compressRate)));
}

From source file:pdfpicmangler.PDFPicMangler.java

License:Open Source License

private PDDocument process(PDDocument doc, Map<String, Float> resolutions) throws IOException {
    this.resolutions = resolutions;

    List<?> pages = doc.getDocumentCatalog().getAllPages();
    for (int i = 0; i < pages.size(); i++) {
        if (!(pages.get(i) instanceof PDPage))
            continue;
        PDPage page = (PDPage) pages.get(i);
        currentPage = i + 1;/*from ww  w. j  ava 2s  .  c  o m*/
        scanResources(page.getResources(), doc);
    }
    return doc;
}

From source file:pdfpicmangler.ResolutionAnalyzer.java

License:Open Source License

public Map<String, Float> analyze(PDDocument document) throws IOException {
    resolutions.clear();//from   w  w  w . j a  v a 2  s  .  co  m

    List<?> allPages = document.getDocumentCatalog().getAllPages();
    for (int i = 0; i < allPages.size(); i++) {
        PDPage page = (PDPage) allPages.get(i);
        currentPage = i + 1;
        System.out.println("Processing page: " + i);
        processStream(page, page.findResources(), page.getContents().getStream());
    }

    return resolutions;
}

From source file:pdfpositional.PdfPositional.java

/**
 * @param args the command line arguments
 *///from w w  w.  j  av a 2s  .c  om
public static void main(String[] args) {
    try {
        // check file param
        if (args.length == 0) {
            throw new ParameterException("No file parameter specified");
        }

        String file = args[args.length - 1];
        Pattern patternFile = Pattern.compile("(?i)^[\\w,\\s-()/]+\\.pdf$");
        Matcher matcherFile = patternFile.matcher(file);

        // check file is valid format
        if (!matcherFile.find()) {
            throw new ParameterException("File parameter invalid: " + file);
        }

        // check if file exists
        File input = new File(file);
        if (!input.exists()) {
            throw new ParameterException("File does not exist: " + file);
        }

        // ensure it isnt a directory
        if (input.isDirectory()) {
            throw new ParameterException("File is a directory: " + file);
        }

        PdfPositional pdfPositional = new PdfPositional(input);
        pdfPositional.setConversion(new Float(1.388888888889));

        pdfPositional.processFileArgument(args[args.length - 1]);
        Pattern patternArgument = Pattern.compile("^-{2}([^=]+)[=]([\\s\\S]+)$");
        Matcher matcher;

        for (int i = 0; i < args.length - 1; i++) {
            matcher = patternArgument.matcher(args[i]);
            while (matcher.find()) {
                switch (matcher.group(1)) {
                case "page":
                    pdfPositional.setPageNumber(Integer.parseInt(matcher.group(2)));
                    break;
                case "output":
                    pdfPositional.setOutputFile(matcher.group(2));
                    break;
                }
            }
        }

        PDDocument document;
        document = PDDocument.load(pdfPositional.getInputFile());

        // check for encrypted document
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (CryptographyException | IOException e) {
                document.close();
                throw new EncryptedDocumentException();
            }
        }

        List allPages = document.getDocumentCatalog().getAllPages();
        if (pdfPositional.hasPageNumber()) {
            if (document.getNumberOfPages() < pdfPositional.getPageNumber()) {
                throw new ParameterException("illegal page number");
            }
            PDPage page = (PDPage) allPages.get(pdfPositional.getPageNumber() - 1);
            PDStream contents = page.getContents();
            if (contents != null) {
                pdfPositional.processStream(page, page.findResources(), page.getContents().getStream());
                pdfPositional.addPageDataToPdfData();
                pdfPositional.writeJSONToOutputStream();
            }
        } else {
            for (int i = 0; i < allPages.size(); i++) {
                pdfPositional.setPageNumber(i + 1);
                PDPage page = (PDPage) allPages.get(i);
                PDStream contents = page.getContents();

                if (contents != null) {
                    pdfPositional.processStream(page, page.findResources(), page.getContents().getStream());
                    pdfPositional.addPageDataToPdfData();
                    pdfPositional.writeJSONToOutputStream();
                }

                page.clear();
            }
        }

        pdfPositional.destroyOutputStream();
        document.close();

        System.exit(0);
    } catch (ParameterException ex) {
        System.out.println("Parameter Error: " + ex.getMessage());
        System.exit(1);
    } catch (EncryptedDocumentException ex) {
        System.out.println("Encrypted Document Error");
        System.exit(1);
    } catch (IOException | NumberFormatException ex) {
        System.out.println("General Error");
        System.exit(1);
    }

}

From source file:pdfreader.ColorScheme.java

public void getColor() throws IOException {
    PDDocument doc = null;
    try {//from www . j ava 2  s  . c om
        doc = PDDocument.load("D://My.pdf");
        PDFStreamEngine engine = new PDFStreamEngine(
                ResourceLoader.loadProperties("org//apache//pdfbox//resources//PageDrawer.properties", true));
        PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
        engine.processStream(page, page.findResources(), page.getContents().getStream());
        PDGraphicsState graphicState = engine.getGraphicsState();
        System.out.println(graphicState.getStrokingColor().getColorSpace().getName());
        float colorSpaceValues[] = graphicState.getStrokingColor().getColorSpaceValue();
        for (float c : colorSpaceValues) {
            System.out.println(c * 255);
        }
    } finally {
        if (doc != null) {
            doc.close();
        }

    }
}

From source file:pdfviewer.Pdfviewer.java

public static void generatePDFFile(String date, String pdfFileName, Boolean[][] statusArray, String cycle)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    //String[] names = pdfFileName.split("\\.");
    String targetFile = "";
    if (pdfFileName.contains(".pdf")) { //doesn't contain surfix
        targetFile = pdfFileName.replace(".pdf", "_forAudit.pdf");
    } else {/*from  w ww. j av a 2 s  .  co  m*/
        targetFile = pdfFileName + "_forAudit.pdf";
    }

    // == prepare for void mark
    String imageName = "void.jpg";
    BufferedImage buffered = ImageIO.read(new File(imageName));
    PDJpeg voidMark = new PDJpeg(pdf, buffered);
    // == end of preparing for void mark

    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();
        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, 100, 200);
        }
        // == 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 + "/" + globalCounterGenerator(sequenceNum));
            sequenceNum++;
            stream.endText();
        }
        // == end of seq stamp

        stream.close();
        pageNum++;
    }
    pdf.save(targetFile);
    pdf.close();

}

From source file:pdfviewer.Pdfviewer.java

public static String dateStampPDFFile(String date, String pdfFileName) throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    //String[] names = pdfFileName.split("\\.");
    String targetFile = "";
    if (pdfFileName.contains(".pdf")) { //doesn't contain surfix
        targetFile = pdfFileName.replace(".pdf", "_DS.pdf");
    } else {/*  www  .j  a va 2s.c  o  m*/
        targetFile = pdfFileName + "_DS.pdf";
    }
    //        String imageName = "void.jpg";
    //        String fileName = "res.pdf"     

    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();
    int pageNum = 0;
    while (iter.hasNext()) {
        PDPage page = iter.next();
        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);

        // == date stamp
        stream.beginText();
        stream.setFont(PDType1Font.HELVETICA, 24);
        stream.moveTextPositionByAmount(100, 300);
        stream.drawString(date);
        stream.endText();
        // == end of date stamp

        stream.close();
    }
    pdf.save(targetFile);
    pdf.close();
    return targetFile;
}

From source file:pdfviewer.Pdfviewer.java

public static String voidStamp(List<Integer> pageNums, String pdfFileName)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    String targetFile = "";
    if (pdfFileName.contains(".pdf")) { //doesn't contain surfix
        targetFile = pdfFileName.replace(".pdf", "_VS.pdf");
    } else {/*from   w w w  .  j  a  va  2 s . co  m*/
        targetFile = pdfFileName + "_VS.pdf";
    }

    String imageName = "void.jpg";

    BufferedImage buffered = ImageIO.read(new File(imageName));
    PDJpeg voidMark = new PDJpeg(pdf, buffered);
    //        String imageName = "void.jpg";
    //        String fileName = "res.pdf"     

    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();
    int pageCount = 1; // page number is 1 based
    while (iter.hasNext()) {
        PDPage page = iter.next();
        if (pageNums.contains(pageCount)) {
            PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
            stream.drawImage(voidMark, 100, 200);
            stream.close();
        }
        pageCount++;
    }
    pdf.save(targetFile);
    pdf.close();
    return targetFile;

}

From source file:pdfviewer.Pdfviewer.java

public static String sequenceStampPDFFile(List<Integer> pageNums, String pdfFileName)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    String targetFile = "";
    if (pdfFileName.contains(".pdf")) { //doesn't contain surfix
        targetFile = pdfFileName.replace(".pdf", "_SS.pdf");
    } else {/*w ww  .  j a v  a2  s.  c  o m*/
        targetFile = pdfFileName + "_SS.pdf";
    }
    //        String imageName = "void.jpg";
    //        String fileName = "res.pdf"     

    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();
    int sequenceNum = 1;
    int pageCount = 1;
    while (iter.hasNext()) {
        PDPage page = iter.next();
        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
        if (!pageNums.contains(pageCount)) {
            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(globalCounterGenerator(sequenceNum));
            sequenceNum++;
            stream.endText();
        }
        stream.close();
        pageCount++;

    }
    pdf.save(targetFile);
    pdf.close();
    return targetFile;
}

From source file:pl.umk.mat.zawodyweb.pdf.PdfToImage.java

License:Open Source License

public static BufferedImage process(InputStream pdfFile) {
    PDDocument pdf = null;
    BufferedImage output = null;//w  ww.j a  v a  2s  .c  o  m
    try {
        pdf = PDDocument.load(pdfFile, true);
        if (pdf.isEncrypted()) {
            pdf.decrypt("");
        }

        List<PDPage> pdfPages = pdf.getDocumentCatalog().getAllPages();
        if (pdfPages.isEmpty() == false) {
            Iterator<PDPage> it = pdfPages.iterator();
            PDPage page = it.next();

            BufferedImage bi = page.convertToImage(BufferedImage.TYPE_USHORT_565_RGB, 72 * 2);
            if (pdfPages.size() == 1) {
                output = bi;
            } else {
                int width = bi.getWidth();
                int height = bi.getHeight();

                output = new BufferedImage(width, height * pdfPages.size(), BufferedImage.TYPE_USHORT_565_RGB);

                Graphics2D g = output.createGraphics();
                g.drawImage(bi, 0, 0, null);
                g.setColor(Color.red);

                int pageNo = 0;
                while (it.hasNext()) {
                    ++pageNo;

                    page = it.next();
                    bi = page.convertToImage(BufferedImage.TYPE_USHORT_565_RGB, 72 * 2);

                    g.drawImage(bi, 0, pageNo * height, null);
                    g.drawLine(0, pageNo * height, width, pageNo * height);
                }
                g.dispose();
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Exception converting pdf to image: ", ex);
    } finally {
        if (pdf != null) {
            try {
                pdf.close();
            } catch (IOException ex) {
                throw new RuntimeException("Exception when closing pdf: ", ex);
            }
        }
    }
    return output;
}