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:FormFiller.java

private static void fillPdf(HashMap dealerTrackData, String inputFileName, String outputDir,
        String outputFormType) {/*from  ww w  .  ja v  a2s.  c  o m*/
    try {
        PDDocument pdfTemplate = PDDocument.load(new File(inputFileName));

        PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();

        List<PDField> fieldList = acroForm.getFields();

        String[] fieldArray = new String[fieldList.size()];
        int i = 0;
        for (PDField sField : fieldList) {
            fieldArray[i] = sField.getFullyQualifiedName();
            i++;
        }

        for (String f : fieldArray) {
            PDField field = acroForm.getField(f);
            String value = (String) dealerTrackData.get(f);
            if (value != null) {
                try {
                    field.setValue(value);
                } catch (IllegalArgumentException e) {
                    System.err.println("Could not insert: " + f + ".");
                }
            }
        }

        pdfTemplate.save(outputDir + "/" + dealerTrackData.get("fullName") + " " + outputFormType + ".pdf");

        // printing - need to look into the long form stuff!
        if (print && !inputFileName.contains("Title Guarantee"))
            printPdf(pdfTemplate, dealerTrackData, inputFileName,
                    inputFileName.contains("Purchase Contract") ? 2 : 1);

        pdfTemplate.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:PrintImageLocations.java

License:Apache License

/**
 * This will print the documents data.//from  w  w  w  . jav  a2  s .  c o  m
 *
 * @param args The command line arguments.
 *
 * @throws Exception If there is an error parsing the document.
 */
public static void main2() throws Exception {
    if (flag) {
        usage();
    } else {
        PDDocument document = null;
        try {
            document = PDDocument.load(PrintTextLocations.INPUTFILE);
            if (document.isEncrypted()) {
                try {
                    document.decrypt("");
                } catch (InvalidPasswordException e) {
                    System.err.println("Error: Document is encrypted with a password.");
                    System.exit(1);
                }
            }
            PrintImageLocations printer = new PrintImageLocations();
            List allPages = document.getDocumentCatalog().getAllPages();
            for (int i = 0; i < allPages.size(); i++) {
                PDPage page = (PDPage) allPages.get(i);
                System.out.println("Processing page: " + i);
                printer.processStream(page, page.findResources(), page.getContents().getStream());
            }
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}

From source file:PDFExtractMetadata.java

License:Apache License

/**
 * This is the main method.//from   w w w  . j  a v a 2s .  co m
 *
 * @param args The command line arguments.
 *
 * @throws IOException If there is an error parsing the document.
 * @throws XmpParsingException
 */
public static void main(String[] args) throws IOException, XmpParsingException {
    if (args.length != 1) {
        usage();
        System.exit(1);
    } else {
        PDDocument document = null;
        try {
            document = PDDocument.load(new File(args[0]));
            PDDocumentCatalog catalog = document.getDocumentCatalog();
            PDMetadata meta = catalog.getMetadata();
            if (meta != null) {
                DomXmpParser xmpParser = new DomXmpParser();
                try {
                    XMPMetadata metadata = xmpParser.parse(meta.createInputStream());

                    DublinCoreSchema dc = metadata.getDublinCoreSchema();
                    if (dc != null) {
                        display("Title:", dc.getTitle());
                        display("Description:", dc.getDescription());
                        listString("Creators: ", dc.getCreators());
                        listCalendar("Dates:", dc.getDates());
                        listString("Subjects:", dc.getSubjects());
                    }

                    AdobePDFSchema pdf = metadata.getAdobePDFSchema();
                    if (pdf != null) {
                        display("Keywords:", pdf.getKeywords());
                        display("PDF Version:", pdf.getPDFVersion());
                        display("PDF Producer:", pdf.getProducer());
                    }

                    XMPBasicSchema basic = metadata.getXMPBasicSchema();
                    if (basic != null) {
                        display("Create Date:", basic.getCreateDate());
                        display("Modify Date:", basic.getModifyDate());
                        display("Creator Tool:", basic.getCreatorTool());
                    }
                } catch (XmpParsingException e) {
                    System.err.println("An error ouccred when parsing the meta data: " + e.getMessage());
                }
            } else {
                // The pdf doesn't contain any metadata, try to use the
                // document information instead
                PDDocumentInformation information = document.getDocumentInformation();
                if (information != null) {
                    showDocumentInformation(information);
                }
            }

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

}

From source file:DecodePlate.java

License:Open Source License

public static void ProcessPlateWork() throws Exception {
    // Filter out fixes more than 50nm away from airport, no chart goes that far.
    // This helps us from trying to decode spurious strings as fix names and
    // helps us avoid duplicate name problems.

    nearDBFixes.clear();//from  w  w  w. j a v a  2 s.  c  o m
    for (DBFix dbfix : allDBFixes) {
        if (Lib.LatLonDist(dbfix.lat, dbfix.lon, airport.lat, airport.lon) <= maxFixDistNM) {
            dbfix.mentioned = false;
            nearDBFixes.put(dbfix.name, dbfix);
        }
    }

    // Also add in runways as fixes cuz some plates use them for fixes.

    for (Runway rwy : airport.runways.values()) {
        nearDBFixes.put(rwy.name, rwy);
    }

    // Open PDF and scan it.

    PDDocument pddoc = PDDocument.load(pdfName);
    PDDocumentCatalog doccat = pddoc.getDocumentCatalog();
    PDPageNode pages = doccat.getPages();
    List kids = new LinkedList();
    pages.getAllKids(kids);
    if (kids.size() != 1)
        throw new Exception("pdf not a single Page");
    Object kid = kids.get(0);
    PDPage page = (PDPage) kid;
    int imgWidth = (int) (page.getMediaBox().getWidth() / pdfDpi * csvDpi + 0.5F);
    int imgHeight = (int) (page.getMediaBox().getHeight() / pdfDpi * csvDpi + 0.5F);
    BufferedImage bi = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    PagePanel pagepanel = new PagePanel(page);
    pagepanel.paintComponent(g2d);
    pagepanel.resolveFixes(g2d);
    if (markedpngname != null) {
        if (!ImageIO.write(bi, "png", new File(markedpngname))) {
            throw new IOException("ImageIO.write(" + markedpngname + ") failed");
        }
    }
    pddoc.close();
}

From source file:ReducePDFSize.java

License:Apache License

public static void main(String[] args) throws IOException {
    if (2 != args.length) {
        throw new RuntimeException("arg0 must be input file, org1 must be output file");
    }/*from   w  w w . j  a v  a  2  s .c om*/
    String in = args[0];
    String out = args[1];
    PDDocument doc = null;

    try {
        doc = PDDocument.load(new File(in));
        doc.setAllSecurityToBeRemoved(true);
        for (COSObject cosObject : doc.getDocument().getObjects()) {
            COSBase base = cosObject.getObject();
            // if it's a stream: decode it, then re-write it using FLATE_DECODE
            if (base instanceof COSStream) {
                COSStream stream = (COSStream) base;
                byte[] bytes;
                try {
                    bytes = new PDStream(stream).toByteArray();
                } catch (IOException ex) {
                    // NOTE: original example code from PDFBox just logged & "continue;"d here, 'skipping' this stream.
                    // If this type of failure ever happens, we can (perhaps) consider (re)ignoring this type of failure?
                    //
                    // IIUC then that will leave the original (non-decoded / non-flated) stream in place?
                    throw new RuntimeException("can't serialize byte[] from: " + cosObject.getObjectNumber()
                            + " " + cosObject.getGenerationNumber() + " obj: " + ex.getMessage(), ex);
                }
                stream.removeItem(COSName.FILTER);
                OutputStream streamOut = stream.createOutputStream(COSName.FLATE_DECODE);
                streamOut.write(bytes);
                streamOut.close();
            }
        }
        doc.getDocumentCatalog();
        doc.save(out);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:SetField.java

License:Apache License

/**
 * This will set a single field in the document.
 *
 * @param pdfDocument The PDF to set the field in.
 * @param name The name of the field to set.
 * @param value The new value of the field.
 *
 * @throws IOException If there is an error setting the field.
 */// w  ww .j  a  va  2s .c o m
public void setField(PDDocument pdfDocument, String[] args) throws IOException {
    PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();
    for (int i = 0; i < args.length - 1; i = i + 2) {
        PDField field = acroForm.getField(args[i]);
        if (field != null) {
            field.setValue(args[i + 1]);
        } else {
            System.err.println("No field found with name:" + args[i]);
        }
    }
}

From source file:algorithm.PDFFileAttacher.java

License:Apache License

private void attachAll(File outputFile, List<File> payloadList) throws IOException {
    PDDocument document = PDDocument.load(outputFile);
    List<PDComplexFileSpecification> fileSpecifications = getFileSpecifications(document, payloadList);
    PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(document.getDocumentCatalog());
    PDEmbeddedFilesNameTreeNode filesTree = namesDictionary.getEmbeddedFiles();
    filesTree = new PDEmbeddedFilesNameTreeNode();
    Map<String, COSObjectable> fileMap = new HashMap<String, COSObjectable>();
    for (int i = 0; i < fileSpecifications.size(); i++) {
        fileMap.put("PericlesMetadata-" + i, fileSpecifications.get(i));
    }/*from  w w  w .j a v  a 2s  .  c o m*/
    filesTree.setNames(fileMap);
    namesDictionary.setEmbeddedFiles(filesTree);
    document.getDocumentCatalog().setNames(namesDictionary);
    try {
        document.save(outputFile);
    } catch (COSVisitorException e) {
    }
    document.close();
}

From source file:algorithm.PDFFileAttacher.java

License:Apache License

@Override
public List<RestoredFile> restore(File originalPdf) throws IOException {
    RestoredFile copiedPdf = getRestoredCarrier(originalPdf);
    List<RestoredFile> restoredFiles = new ArrayList<RestoredFile>();
    PDDocument document = PDDocument.load(copiedPdf);
    PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(document.getDocumentCatalog());
    PDEmbeddedFilesNameTreeNode filesTree = namesDictionary.getEmbeddedFiles();
    if (filesTree != null) {
        int i = 0;
        while (true) {
            PDComplexFileSpecification fileSpecification = (PDComplexFileSpecification) filesTree
                    .getValue("PericlesMetadata-" + i);
            if (fileSpecification == null) {
                break;
            }/* www.j  a  v a2s  .  co m*/
            File oldAttachedFile = new File(fileSpecification.getFile());
            RestoredFile restoredPayload = new RestoredFile(RESTORED_DIRECTORY + oldAttachedFile.getName());
            PDEmbeddedFile embeddedFile = fileSpecification.getEmbeddedFile();
            InputStream inputStream = embeddedFile.createInputStream();
            FileOutputStream outputStream = new FileOutputStream(restoredPayload);
            IOUtils.copy(inputStream, outputStream);
            removeBuggyLineEnding(restoredPayload);
            restoredPayload.wasPayload = true;
            restoredPayload.checksumValid = true;
            restoredPayload.restorationNote = "Checksum wasn't calculated, because this algorithm isn't using restoration metadata. The original payload file survives the encapsulation with this algorithm.";
            restoredFiles.add(restoredPayload);
            i++;
        }
    }
    document.close();
    copiedPdf.wasCarrier = true;
    copiedPdf.checksumValid = false;
    copiedPdf.restorationNote = "Checksum can't be valid, because attached payload files can't be removed from carrier.";
    restoredFiles.add(copiedPdf);
    for (RestoredFile file : restoredFiles) {
        file.algorithm = this;
        for (RestoredFile relatedFile : restoredFiles) {
            if (file != relatedFile) {
                file.relatedFiles.add(relatedFile);
            }
        }
    }
    return restoredFiles;
}

From source file:architecture.ee.web.attachment.DefaultAttachmentManager.java

License:Apache License

protected File getThumbnailFromCacheIfExist(Attachment attach, int width, int height) throws IOException {

    log.debug("thumbnail generation " + width + "x" + height);
    File dir = getAttachmentCacheDir();
    File file = new File(dir, toThumbnailFilename(attach, width, height));
    File originalFile = getAttachmentFromCacheIfExist(attach);
    log.debug("source: " + originalFile.getAbsoluteFile() + ", " + originalFile.length());
    log.debug("thumbnail:" + file.getAbsoluteFile());

    if (file.exists() && file.length() > 0) {
        attach.setThumbnailSize((int) file.length());
        return file;
    }/*from   ww w .  j a v  a2s .c  om*/

    if (StringUtils.endsWithIgnoreCase(attach.getContentType(), "pdf")) {
        PDDocument document = PDDocument.load(originalFile);
        List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        PDPage page = pages.get(0);
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 72);
        ImageIO.write(Thumbnails.of(image).size(width, height).asBufferedImage(), "png", file);
        attach.setThumbnailSize((int) file.length());
        return file;
    } else if (StringUtils.startsWithIgnoreCase(attach.getContentType(), "image")) {
        BufferedImage originalImage = ImageIO.read(originalFile);
        if (originalImage.getHeight() < height || originalImage.getWidth() < width) {
            attach.setThumbnailSize(0);
            return originalFile;
        }
        BufferedImage thumbnail = Thumbnails.of(originalImage).size(width, height).asBufferedImage();
        ImageIO.write(thumbnail, "png", file);
        attach.setThumbnailSize((int) file.length());
        return file;
    }

    return null;
}

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox.placeholder.SignaturePlaceholderExtractor.java

License:EUPL

/**
 * Extracts all placeholders (with placeholder identifier
 * {@linkplain at.gv.egiz.pdfas.lib.impl.placeholder.PlaceholderExtractorConstants#QR_PLACEHOLDER_IDENTIFIER
 * QR_PLACEHOLDER_IDENTIFIER})./* w ww  .  j av a2 s.c o  m*/
 * 
 * @param doc
 *            The pdfbox document object.
 * @return A (unmodifiable) list of signature place holders (never {@code null}).
 * @throws IOException
 *             Thrown in case of I/O error reading/parsing the pdf document.
 */
@SuppressWarnings("unchecked")
public static List<SignaturePlaceholderData> extract(PDDocument doc) throws IOException {
    Objects.requireNonNull(doc, "Pdfbox document must not be null.");

    SignaturePlaceholderExtractor extractor = new SignaturePlaceholderExtractor(QR_PLACEHOLDER_IDENTIFIER, // is ignored anyway
            PLACEHOLDER_MATCH_MODE_MODERATE // is ignored anyway
            , doc);

    int pageNr = 0;
    for (PDPage page : (Iterable<PDPage>) doc.getDocumentCatalog().getAllPages()) {
        extractor.setCurrentPage(++pageNr);
        PDStream contents;
        PDResources resources;
        if ((contents = page.getContents()) != null && contents.getStream() != null
                && (resources = page.findResources()) != null) {
            extractor.processStream(page, resources, contents.getStream());
        }
    }

    return ListUtils.unmodifiableList(new ArrayList<SignaturePlaceholderData>(extractor.placeholders));
}