Java Code Examples for com.lowagie.text.Document#compress()
The following examples show how to use
com.lowagie.text.Document#compress() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PRStream.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Creates a new PDF stream object that will replace a stream in a existing PDF file. * * @param reader the reader that holds the existing PDF * @param conts the new content * @param compressionLevel the compression level for the content * @since 2.1.3 (replacing the existing constructor without param compressionLevel) */ public PRStream(PdfReader reader, byte[] conts, int compressionLevel) { this.reader = reader; offset = -1; if (Document.compress) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(compressionLevel); DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater); zip.write(conts); zip.close(); deflater.end(); bytes = stream.toByteArray(); } catch (IOException ioe) { throw new ExceptionConverter(ioe); } put(PdfName.FILTER, PdfName.FLATEDECODE); } else { bytes = conts; } setLength(bytes.length); }
Example 2
Source File: PRStream.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Sets the data associated with the stream, either compressed or uncompressed. Note that the * data will never be compressed if Document.compress is set to false. * * @param data raw data, decrypted and uncompressed. * @param compress true if you want the stream to be compressed. * @param compressionLevel a value between -1 and 9 (ignored if compress == false) * @since iText 2.1.3 */ public void setData(byte[] data, boolean compress, int compressionLevel) { remove(PdfName.FILTER); offset = -1; if (Document.compress && compress) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(compressionLevel); DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater); zip.write(data); zip.close(); deflater.end(); bytes = stream.toByteArray(); this.compressionLevel = compressionLevel; } catch (IOException ioe) { throw new ExceptionConverter(ioe); } put(PdfName.FILTER, PdfName.FLATEDECODE); } else { bytes = data; } setLength(bytes.length); }
Example 3
Source File: PRStream.java From itext2 with GNU Lesser General Public License v3.0 | 6 votes |
/** * Creates a new PDF stream object that will replace a stream * in a existing PDF file. * @param reader the reader that holds the existing PDF * @param conts the new content * @param compressionLevel the compression level for the content * @since 2.1.3 (replacing the existing constructor without param compressionLevel) */ public PRStream(PdfReader reader, byte[] conts, int compressionLevel) { this.reader = reader; this.offset = -1; if (Document.compress) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(compressionLevel); DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater); zip.write(conts); zip.close(); deflater.end(); bytes = stream.toByteArray(); } catch (IOException ioe) { throw new ExceptionConverter(ioe); } put(PdfName.FILTER, PdfName.FLATEDECODE); } else bytes = conts; setLength(bytes.length); }
Example 4
Source File: PRStream.java From itext2 with GNU Lesser General Public License v3.0 | 6 votes |
/** * Sets the data associated with the stream, either compressed or * uncompressed. Note that the data will never be compressed if * Document.compress is set to false. * * @param data raw data, decrypted and uncompressed. * @param compress true if you want the stream to be compressed. * @param compressionLevel a value between -1 and 9 (ignored if compress == false) * @since iText 2.1.3 */ public void setData(byte[] data, boolean compress, int compressionLevel) { remove(PdfName.FILTER); this.offset = -1; if (Document.compress && compress) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(compressionLevel); DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater); zip.write(data); zip.close(); deflater.end(); bytes = stream.toByteArray(); this.compressionLevel = compressionLevel; } catch (IOException ioe) { throw new ExceptionConverter(ioe); } put(PdfName.FILTER, PdfName.FLATEDECODE); } else bytes = data; setLength(bytes.length); }
Example 5
Source File: PRStream.java From MesquiteCore with GNU Lesser General Public License v3.0 | 6 votes |
public PRStream(PdfReader reader, byte conts[]) { this.reader = reader; this.offset = -1; if (Document.compress) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); DeflaterOutputStream zip = new DeflaterOutputStream(stream); zip.write(conts); zip.close(); bytes = stream.toByteArray(); } catch (IOException ioe) { throw new ExceptionConverter(ioe); } put(PdfName.FILTER, PdfName.FLATEDECODE); } else bytes = conts; setLength(bytes.length); }
Example 6
Source File: PRStream.java From MesquiteCore with GNU Lesser General Public License v3.0 | 6 votes |
/**Sets the data associated with the stream * @param data raw data, decrypted and uncompressed. */ public void setData(byte[] data) { remove(PdfName.FILTER); this.offset = -1; if (Document.compress) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); DeflaterOutputStream zip = new DeflaterOutputStream(stream); zip.write(data); zip.close(); bytes = stream.toByteArray(); } catch (IOException ioe) { throw new ExceptionConverter(ioe); } put(PdfName.FILTER, PdfName.FLATEDECODE); } else bytes = data; setLength(bytes.length); }
Example 7
Source File: TransformImageTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * Add an image using different transformation matrices. */ @Test public void main() throws Exception { Document.compress = false; // step 1: creation of a document-object Document document = new Document(PageSize.A4); try { // step 2: creation of the writer PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "transformimage.pdf")); // step 3: we open the document document.open(); // step 4: PdfContentByte cb = writer.getDirectContent(); Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "hitchcock.png"); cb.addImage(img, 271, -50, -30, 550, 100, 100); cb.sanityCheck(); } catch(DocumentException de) { System.err.println(de.getMessage()); } catch(IOException ioe) { System.err.println(ioe.getMessage()); } // step 5: we close the document document.close(); }
Example 8
Source File: FormPushButtonTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * Generates an Acroform with a PushButton */ @Test public void main() throws Exception { Document.compress = false; // step 1: creation of a document-object Document document = new Document(PageSize.A4); // step 2: PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("pushbutton.pdf")); // step 3: we open the document document.open(); // step 4: PdfFormField pushbutton = PdfFormField.createPushButton(writer); PdfContentByte cb = writer.getDirectContent(); cb.moveTo(0, 0); PdfAppearance normal = cb.createAppearance(100, 50); normal.setColorFill(Color.GRAY); normal.rectangle(5, 5, 90, 40); normal.fill(); PdfAppearance rollover = cb.createAppearance(100, 50); rollover.setColorFill(Color.RED); rollover.rectangle(5, 5, 90, 40); rollover.fill(); PdfAppearance down = cb.createAppearance(100, 50); down.setColorFill(Color.BLUE); down.rectangle(5, 5, 90, 40); down.fill(); pushbutton.setFieldName("PushMe"); pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, normal); pushbutton.setAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, rollover); pushbutton.setAppearance(PdfAnnotation.APPEARANCE_DOWN, down); pushbutton.setWidget(new Rectangle(100, 700, 200, 750), PdfAnnotation.HIGHLIGHT_PUSH); writer.addAnnotation(pushbutton); // step 5: we close the document document.close(); }
Example 9
Source File: PdfStream.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * Compresses the stream. * * @param compressionLevel the compression level (0 = best speed, 9 = best compression, -1 is * default) * @since 2.1.3 */ public void flateCompress(int compressionLevel) { if (!Document.compress) { return; } // check if the flateCompress-method has already been if (compressed) { return; } this.compressionLevel = compressionLevel; if (inputStream != null) { compressed = true; return; } // check if a filter already exists PdfObject filter = PdfReader.getPdfObject(get(PdfName.FILTER)); if (filter != null) { if (filter.isName()) { if (PdfName.FLATEDECODE.equals(filter)) { return; } } else if (filter.isArray()) { if (((PdfArray) filter).contains(PdfName.FLATEDECODE)) { return; } } else { throw new RuntimeException("Stream could not be compressed: filter is not a name or array."); } } try { // compress ByteArrayOutputStream stream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(compressionLevel); DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater); if (streamBytes != null) { streamBytes.writeTo(zip); } else { zip.write(bytes); } zip.close(); deflater.end(); // update the object streamBytes = stream; bytes = null; put(PdfName.LENGTH, new PdfNumber(streamBytes.size())); if (filter == null) { put(PdfName.FILTER, PdfName.FLATEDECODE); } else { PdfArray filters = new PdfArray(filter); filters.add(PdfName.FLATEDECODE); put(PdfName.FILTER, filters); } compressed = true; } catch (IOException ioe) { throw new ExceptionConverter(ioe); } }
Example 10
Source File: PdfStream.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * Compresses the stream. * @param compressionLevel the compression level (0 = best speed, 9 = best compression, -1 is default) * @since 2.1.3 */ public void flateCompress(int compressionLevel) { if (!Document.compress) return; // check if the flateCompress-method has already been if (compressed) { return; } this.compressionLevel = compressionLevel; if (inputStream != null) { compressed = true; return; } // check if a filter already exists PdfObject filter = PdfReader.getPdfObject(get(PdfName.FILTER)); if (filter != null) { if (filter.isName()) { if (PdfName.FLATEDECODE.equals(filter)) return; } else if (filter.isArray()) { if (((PdfArray) filter).contains(PdfName.FLATEDECODE)) return; } else { throw new RuntimeException("Stream could not be compressed: filter is not a name or array."); } } try { // compress ByteArrayOutputStream stream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(compressionLevel); DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater); if (streamBytes != null) streamBytes.writeTo(zip); else zip.write(bytes); zip.close(); deflater.end(); // update the object streamBytes = stream; bytes = null; put(PdfName.LENGTH, new PdfNumber(streamBytes.size())); if (filter == null) { put(PdfName.FILTER, PdfName.FLATEDECODE); } else { PdfArray filters = new PdfArray(filter); filters.add(PdfName.FLATEDECODE); put(PdfName.FILTER, filters); } compressed = true; } catch(IOException ioe) { throw new ExceptionConverter(ioe); } }
Example 11
Source File: LiteralTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
@Test public void main() throws Exception { Document.compress = false; // step 1: creation of a document-object Document document = new Document(); try { // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "literal.pdf")); // step 3: we open the document document.open(); // step 4: we grab the ContentByte and do some stuff with it PdfContentByte cb = writer.getDirectContent(); String star = "0.3 g\n15.000 27.000 m\n" + "7.947 5.292 l\n26.413 18.708 l\n" + "3.587 18.708 l\n22.053 5.292 l\nf\n" + "45.000 57.000 m\n37.947 35.292 l\n" + "56.413 48.708 l\n33.587 48.708 l\n" + "52.053 35.292 l\nf\n" + "0.7 g\n15.000 57.000 m\n" + "7.947 35.292 l\n26.413 48.708 l\n" + "3.587 48.708 l\n22.053 35.292 l\nf\n" + "45.000 27.000 m\n37.947 5.292 l\n" + "56.413 18.708 l\n33.587 18.708 l\n" + "52.053 5.292 l\nf"; cb.setLiteral(star); // sanityCheck doesn't check literals. //cb.sanityCheck(); } catch(DocumentException de) { System.err.println(de.getMessage()); } catch(IOException ioe) { System.err.println(ioe.getMessage()); } // step 5: we close the document document.close(); }
Example 12
Source File: PdfStream.java From MesquiteCore with GNU Lesser General Public License v3.0 | 4 votes |
/** * Compresses the stream. */ public void flateCompress() { if (!Document.compress) return; // check if the flateCompress-method has already been if (compressed) { return; } if (inputStream != null) { compressed = true; return; } // check if a filter already exists PdfObject filter = get(PdfName.FILTER); if (filter != null) { if (filter.isName() && ((PdfName) filter).compareTo(PdfName.FLATEDECODE) == 0) { return; } else if (filter.isArray() && ((PdfArray) filter).contains(PdfName.FLATEDECODE)) { return; } else { throw new RuntimeException("Stream could not be compressed: filter is not a name or array."); } } try { // compress ByteArrayOutputStream stream = new ByteArrayOutputStream(); DeflaterOutputStream zip = new DeflaterOutputStream(stream); if (streamBytes != null) streamBytes.writeTo(zip); else zip.write(bytes); zip.close(); // update the object streamBytes = stream; bytes = null; put(PdfName.LENGTH, new PdfNumber(streamBytes.size())); if (filter == null) { put(PdfName.FILTER, PdfName.FLATEDECODE); } else { PdfArray filters = new PdfArray(filter); filters.add(PdfName.FLATEDECODE); put(PdfName.FILTER, filters); } compressed = true; } catch(IOException ioe) { throw new ExceptionConverter(ioe); } }
Example 13
Source File: PdfContents.java From MesquiteCore with GNU Lesser General Public License v3.0 | 4 votes |
/** * Constructs a <CODE>PdfContents</CODE>-object, containing text and general graphics. * * @param under the direct content that is under all others * @param content the graphics in a page * @param text the text in a page * @param secondContent the direct content that is over all others * @throws BadPdfFormatException on error */ PdfContents(PdfContentByte under, PdfContentByte content, PdfContentByte text, PdfContentByte secondContent, Rectangle page) throws BadPdfFormatException { super(); try { OutputStream out = null; streamBytes = new ByteArrayOutputStream(); if (Document.compress) { compressed = true; out = new DeflaterOutputStream(streamBytes); } else out = streamBytes; int rotation = page.getRotation(); switch (rotation) { case 90: out.write(ROTATE90); out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.top()))); out.write(' '); out.write('0'); out.write(ROTATEFINAL); break; case 180: out.write(ROTATE180); out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.right()))); out.write(' '); out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.top()))); out.write(ROTATEFINAL); break; case 270: out.write(ROTATE270); out.write('0'); out.write(' '); out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.right()))); out.write(ROTATEFINAL); break; } if (under.size() > 0) { out.write(SAVESTATE); under.getInternalBuffer().writeTo(out); out.write(RESTORESTATE); } if (content.size() > 0) { out.write(SAVESTATE); content.getInternalBuffer().writeTo(out); out.write(RESTORESTATE); } if (text != null) { out.write(SAVESTATE); text.getInternalBuffer().writeTo(out); out.write(RESTORESTATE); } if (secondContent.size() > 0) { secondContent.getInternalBuffer().writeTo(out); } out.close(); } catch (Exception e) { throw new BadPdfFormatException(e.getMessage()); } put(PdfName.LENGTH, new PdfNumber(streamBytes.size())); if (compressed) put(PdfName.FILTER, PdfName.FLATEDECODE); }