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();
}
}
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;
}
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();
}
}
}
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());
}
}
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());
}
Aggregations