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

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

Introduction

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

Prototype

public static PDDocument load(byte[] input) throws IOException 

Source Link

Document

Parses a PDF.

Usage

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");
    }/*w ww  .ja  va2s . c  o m*/
    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

private void setField(String[] args) throws IOException, COSVisitorException {
    PDDocument pdf = null;/*  ww  w. ja v a  2s . c  o  m*/
    try {
        if (args.length < 3) {
            usage();
        } else {
            SetField example = new SetField();

            pdf = PDDocument.load(args[0]);
            example.setField(pdf, Arrays.copyOfRange(args, 1, args.length));
            pdf.save(args[0] + ".computed.pdf");
        }
    } finally {
        if (pdf != null) {
            pdf.close();
        }
    }
}

From source file:CreateSignature.java

License:Apache License

/**
 * Signs the given PDF file.//from   w  ww.jav  a2  s. c o  m
 * @param inFile input PDF file
 * @param outFile output PDF file
 * @param tsaClient optional TSA client
 * @throws IOException if the input file could not be read
 */
public void signDetached(File inFile, File outFile, TSAClient tsaClient) throws IOException {
    if (inFile == null || !inFile.exists()) {
        throw new FileNotFoundException("Document for signing does not exist");
    }

    FileOutputStream fos = new FileOutputStream(outFile);

    // sign
    PDDocument doc = PDDocument.load(inFile);
    signDetached(doc, fos, tsaClient);
    doc.close();
}

From source file:CreateVisibleSignature.java

License:Apache License

/**
 * Sign pdf file and create new file that ends with "_signed.pdf".
 *
 * @param inputFile The source pdf document file.
 * @param signedFile The file to be signed.
 * @throws IOException//from  w w  w  .j a  v a  2  s.  com
 */
public void signPDF(File inputFile, File signedFile) throws IOException {
    if (inputFile == null || !inputFile.exists()) {
        throw new IOException("Document for signing does not exist");
    }

    // creating output document and prepare the IO streams.
    FileOutputStream fos = new FileOutputStream(signedFile);

    // load document
    PDDocument doc = PDDocument.load(inputFile);

    // create signature dictionary
    PDSignature signature = new PDSignature();
    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
    // subfilter for basic and PAdES Part 2 signatures
    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
    signature.setName("signer name");
    signature.setLocation("signer location");
    signature.setReason("reason for signature");

    // the signing date, needed for valid signature
    signature.setSignDate(Calendar.getInstance());

    // register signature dictionary and sign interface
    if (visibleSignatureProperties != null && visibleSignatureProperties.isVisualSignEnabled()) {
        options = new SignatureOptions();
        options.setVisualSignature(visibleSignatureProperties);
        options.setPage(visibleSignatureProperties.getPage() - 1);
        doc.addSignature(signature, this, options);
    } else {
        doc.addSignature(signature, this);
    }

    // write incremental (only for signing purpose)
    doc.saveIncremental(fos);
    doc.close();

    // do not close options before saving, because some COSStream objects within options
    // are transferred to the signed document.
    IOUtils.closeQuietly(options);
}

From source file:adams.core.io.PDFBox.java

License:Open Source License

/**
 * Loads the PDF file.//from   w  ww. jav  a2 s  .co m
 *
 * @param file   the PDF file
 * @return      the PDF document, null if failed to load
 */
public static PDDocument load(File file) {
    try {
        return PDDocument.load(file.getAbsoluteFile());
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to load PDF: " + file, e);
        return null;
    }
}

From source file:adams.flow.transformer.PDFMetaData.java

License:Open Source License

/**
 * Executes the flow item./* w  w  w.  j  ava  2 s.  c  om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    File file;
    SpreadSheet sheet;
    PDDocument document;
    PDDocumentInformation info;
    Row row;
    Set<String> keys;

    result = null;

    // get file
    if (m_InputToken.getPayload() instanceof File)
        file = (File) m_InputToken.getPayload();
    else
        file = new PlaceholderFile((String) m_InputToken.getPayload());

    sheet = new DefaultSpreadSheet();
    sheet.setDataRowClass(SparseDataRow.class);
    sheet.setName("Meta-Data: " + file.getAbsolutePath());

    try {
        row = sheet.addRow();
        document = PDDocument.load(file.getAbsoluteFile());
        info = document.getDocumentInformation();

        addCell(row, "Title", info.getTitle());
        addCell(row, "Subject", info.getSubject());
        addCell(row, "Author", info.getAuthor());
        addCell(row, "Keywords", info.getKeywords());
        addCell(row, "Producer", info.getProducer());
        addCell(row, "Creation Date", info.getCreationDate());
        addCell(row, "Modification Date", info.getModificationDate());
        addCell(row, "Creator", info.getCreator());
        addCell(row, "Trapped", info.getTrapped());
        keys = info.getMetadataKeys();
        for (String key : keys)
            addCell(row, "Meta-" + key, info.getCustomMetadataValue(key));
    } catch (Exception e) {
        result = handleException("Failed to extract meta-data: ", e);
    }

    if (result == null)
        m_OutputToken = new Token(sheet);

    return result;
}

From source file:airviewer.AIRViewerModel.java

License:Apache License

/**
 * Constructor: Loads the PDF document at the path (file system path).
 *
 * @param path A file system path to a PDF file.
 * @throws IOException If the PDF file cannot be read or does not contain
 * valid PDF data.// ww w.j  a va2 s  .co m
 */
AIRViewerModel(Path path) throws IOException {
    super(PDDocument.load(path.toFile()), "");
    renderer = new PDFRenderer(wrappedDocument);
    AbstractDocumentCommandWrapper.registerCommandFactoryWithName(
            (AbstractDocumentCommandWrapper owner,
                    ArrayList<String> args) -> new MoveSelectedAnnotationDocumentCommand(owner, args),
            "MoveSelectedAnnotation");
    AbstractDocumentCommandWrapper.registerCommandFactoryWithName(
            (AbstractDocumentCommandWrapper owner,
                    ArrayList<String> args) -> new DeleteSelectedAnnotationDocumentCommand(owner, args),
            "DeleteSelectedAnnotation");
    AbstractDocumentCommandWrapper.registerCommandFactoryWithName(
            (AbstractDocumentCommandWrapper owner,
                    ArrayList<String> args) -> new ChangeSelectedTextAnnotationDocumentCommand(owner, args),
            "ChangeSelectedAnnotationText");
}

From source file:airviewer.DocumentCommandWrapper.java

License:Apache License

/**
 * Reads information from the valid PDF file if any at aPath and returns a
 * DocumentCommandWrapper instance ready to add or delete annotations to the
 * loaded information. Trace: REQ-041000, REQ-041060
 *
 * @param aPath A valid file system path to a valid PDF document.
 * @return An instance of DocumentCommandWrapper configured to apply
 * Commands to the document at aPath./*from w  w  w  .  j a  va  2 s. c om*/
 * @throws IOException
 */
/*@   requires aPath is valid 
  @   assignable \nothing;
  @   ensures wrappedDocument.nonAnnotationContent == \old(<wrappedDocument>).nonAnnotationContent
  @   ensures wrappedDocument.annotationContent ==  \old(<wrappedDocument>).annotationContent
  @   ensures [file at path].nonAnnotationContent == \old(<[file at path]>).nonAnnotationContent
  @   ensures [file at path].annotationContent == \old(<[file at path]>).annotationContent
 */
public static DocumentCommandWrapper loadDocumentAtPath(String aPath) throws IOException {
    assert aPath != null;
    DocumentCommandWrapper result = null;
    PDDocument document = PDDocument.load(new File(aPath));

    if (null != document) {
        result = new DocumentCommandWrapper(document, aPath);
    }

    return result;
}

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));
    }// www .  j a  va 2  s  . com
    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;
            }//from   ww  w  .  j a  v a 2s  .  c  om
            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;
}