Search in sources :

Example 1 with COSWriter

use of com.tom_roush.pdfbox.pdfwriter.COSWriter in project PdfBox-Android by TomRoush.

the class PDDocument method save.

/**
 * This will save the document to an output stream.
 *
 * @param output The stream to write to. It will be closed when done. It is recommended to wrap
 * it in a {@link java.io.BufferedOutputStream}, unless it is already buffered.
 *
 * @throws IOException if the output could not be written
 */
public void save(OutputStream output) throws IOException {
    if (document.isClosed()) {
        throw new IOException("Cannot save a document which has been closed");
    }
    // subset designated fonts
    for (PDFont font : fontsToSubset) {
        font.subset();
    }
    fontsToSubset.clear();
    // save PDF
    COSWriter writer = new COSWriter(output);
    try {
        writer.write(this);
    } finally {
        writer.close();
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) COSWriter(com.tom_roush.pdfbox.pdfwriter.COSWriter) IOException(java.io.IOException)

Example 2 with COSWriter

use of com.tom_roush.pdfbox.pdfwriter.COSWriter in project PdfBox-Android by TomRoush.

the class PDDocument method saveIncrementalForExternalSigning.

/**
 * <p>
 * <b>(This is a new feature for 2.0.3. The API for external signing might change based on feedback after release!)</b>
 * <p>
 * Save PDF incrementally without closing for external signature creation scenario. The general
 * sequence is:
 * <pre>
 *    PDDocument pdDocument = ...;
 *    OutputStream outputStream = ...;
 *    SignatureOptions signatureOptions = ...; // options to specify fine tuned signature options or null for defaults
 *    PDSignature pdSignature = ...;
 *
 *    // add signature parameters to be used when creating signature dictionary
 *    pdDocument.addSignature(pdSignature, signatureOptions);
 *    // prepare PDF for signing and obtain helper class to be used
 *    ExternalSigningSupport externalSigningSupport = pdDocument.saveIncrementalForExternalSigning(outputStream);
 *    // get data to be signed
 *    InputStream dataToBeSigned = externalSigningSupport.getContent();
 *    // invoke signature service
 *    byte[] signature = sign(dataToBeSigned);
 *    // set resulted CMS signature
 *    externalSigningSupport.setSignature(signature);
 *
 *    // last step is to close the document
 *    pdDocument.close();
 * </pre>
 * <p>
 * Note that after calling this method, only {@code close()} method may invoked for
 * {@code PDDocument} instance and only AFTER {@link ExternalSigningSupport} instance is used.
 * </p>
 *
 * @param output stream to write the final PDF. It will be closed when the
 * document is closed. It <i><b>must never</b></i> point to the source file
 * or that one will be harmed!
 * @return instance to be used for external signing and setting CMS signature
 * @throws IOException if the output could not be written
 * @throws IllegalStateException if the document was not loaded from a file or a stream or
 * signature options were not set.
 */
public ExternalSigningSupport saveIncrementalForExternalSigning(OutputStream output) throws IOException {
    if (pdfSource == null) {
        throw new IllegalStateException("document was not loaded from a file or a stream");
    }
    // PDFBOX-3978: getLastSignatureDictionary() not helpful if signing into a template
    // that is not the last signature. So give higher priority to signature with update flag.
    PDSignature foundSignature = null;
    for (PDSignature sig : getSignatureDictionaries()) {
        foundSignature = sig;
        if (sig.getCOSObject().isNeedToBeUpdated()) {
            break;
        }
    }
    int[] byteRange = foundSignature.getByteRange();
    if (!Arrays.equals(byteRange, RESERVE_BYTE_RANGE)) {
        throw new IllegalStateException("signature reserve byte range has been changed " + "after addSignature(), please set the byte range that existed after addSignature()");
    }
    COSWriter writer = new COSWriter(output, pdfSource);
    writer.write(this);
    signingSupport = new SigningSupport(writer);
    return signingSupport;
}
Also used : COSWriter(com.tom_roush.pdfbox.pdfwriter.COSWriter) ExternalSigningSupport(com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport) SigningSupport(com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.SigningSupport) PDSignature(com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.PDSignature)

Example 3 with COSWriter

use of com.tom_roush.pdfbox.pdfwriter.COSWriter in project PdfBox-Android by TomRoush.

the class FDFDocument method save.

/**
 * This will save the document to an output stream.
 *
 * @param output The stream to write to.
 *
 * @throws IOException If there is an error writing the document.
 */
public void save(OutputStream output) throws IOException {
    COSWriter writer = null;
    try {
        writer = new COSWriter(output);
        writer.write(this);
        writer.close();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : COSWriter(com.tom_roush.pdfbox.pdfwriter.COSWriter)

Example 4 with COSWriter

use of com.tom_roush.pdfbox.pdfwriter.COSWriter in project PdfBox-Android by TomRoush.

the class TestCOSInteger method testAccept.

@Override
public void testAccept() {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    COSWriter visitor = new COSWriter(outStream);
    int index = 0;
    try {
        for (int i = -1000; i < 3000; i += 200) {
            index = i;
            COSInteger cosInt = COSInteger.get(i);
            cosInt.accept(visitor);
            testByteArrays(String.valueOf(i).getBytes("ISO-8859-1"), outStream.toByteArray());
            outStream.reset();
        }
    } catch (Exception e) {
        fail("Failed to write " + index + " exception: " + e.getMessage());
    }
}
Also used : COSWriter(com.tom_roush.pdfbox.pdfwriter.COSWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 5 with COSWriter

use of com.tom_roush.pdfbox.pdfwriter.COSWriter in project PdfBox-Android by TomRoush.

the class PDFTemplateCreator method getVisualSignatureAsStream.

private InputStream getVisualSignatureAsStream(COSDocument visualSignature) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    COSWriter writer = new COSWriter(baos);
    writer.write(visualSignature);
    writer.close();
    return new ByteArrayInputStream(baos.toByteArray());
}
Also used : COSWriter(com.tom_roush.pdfbox.pdfwriter.COSWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

COSWriter (com.tom_roush.pdfbox.pdfwriter.COSWriter)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 COSDocument (com.tom_roush.pdfbox.cos.COSDocument)1 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)1 ExternalSigningSupport (com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport)1 PDSignature (com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.PDSignature)1 SigningSupport (com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.SigningSupport)1