Hello, I thought it would take changes in about 20 iText classes to implement zlib compression levels. I've spent a sunny Sunday afternoon writing some code and I ended up by changing 26 classes...
If people want to change the compression level, they usually want to differentiate between the types of content, so I decided to provide different places where you can change the compression level: - PdfWriter: the content streams of the pages, JS actions,... - Image: some images are compressed with iText, you can now define the level of compression. - BaseFont: you can define the compression level for the stream containing the font program. Still to do: allow people to fine tune the compression level for embedded files. For the moment, some files are compressed using the best compression (9); others using the compression defined in PdfWriter. I need some extra time to do this. I've tested the functionality and... I'm not convinced that my changes made sense, but now your customer doesn't have any excuse anymore to use another PDF library ;-) best regards, Bruno [EMAIL PROTECTED] wrote: > Revision: 3527 > http://itext.svn.sourceforge.net/itext/?rev=3527&view=rev > Author: blowagie > Date: 2008-07-06 08:34:38 -0700 (Sun, 06 Jul 2008) > > Log Message: > ----------- > Support for setting the compression level (zlib). > Works for streams created by PdfWriter, for the image types where FlateDecode > is done by iText, and for font streams. > Still some work to do to support this for embedded files. > > Modified Paths: > -------------- > trunk/src/core/com/lowagie/text/Image.java > trunk/src/core/com/lowagie/text/pdf/BaseFont.java > trunk/src/core/com/lowagie/text/pdf/PRStream.java > trunk/src/core/com/lowagie/text/pdf/PdfAction.java > trunk/src/core/com/lowagie/text/pdf/PdfContents.java > trunk/src/core/com/lowagie/text/pdf/PdfCopy.java > trunk/src/core/com/lowagie/text/pdf/PdfFileSpecification.java > trunk/src/core/com/lowagie/text/pdf/PdfFormXObject.java > trunk/src/core/com/lowagie/text/pdf/PdfFunction.java > trunk/src/core/com/lowagie/text/pdf/PdfICCBased.java > trunk/src/core/com/lowagie/text/pdf/PdfImage.java > trunk/src/core/com/lowagie/text/pdf/PdfImportedPage.java > trunk/src/core/com/lowagie/text/pdf/PdfPSXObject.java > trunk/src/core/com/lowagie/text/pdf/PdfPattern.java > trunk/src/core/com/lowagie/text/pdf/PdfPatternPainter.java > trunk/src/core/com/lowagie/text/pdf/PdfReader.java > trunk/src/core/com/lowagie/text/pdf/PdfReaderInstance.java > trunk/src/core/com/lowagie/text/pdf/PdfStamperImp.java > trunk/src/core/com/lowagie/text/pdf/PdfStream.java > trunk/src/core/com/lowagie/text/pdf/PdfTemplate.java > trunk/src/core/com/lowagie/text/pdf/PdfWriter.java > trunk/src/core/com/lowagie/text/pdf/TrueTypeFont.java > trunk/src/core/com/lowagie/text/pdf/TrueTypeFontUnicode.java > trunk/src/core/com/lowagie/text/pdf/Type1Font.java > trunk/src/core/com/lowagie/text/pdf/Type3Font.java > trunk/src/core/com/lowagie/text/pdf/XfaForm.java > > Modified: trunk/src/core/com/lowagie/text/Image.java > =================================================================== > --- trunk/src/core/com/lowagie/text/Image.java 2008-07-06 06:54:15 UTC > (rev 3526) > +++ trunk/src/core/com/lowagie/text/Image.java 2008-07-06 15:34:38 UTC > (rev 3527) > @@ -199,6 +199,12 @@ > > /** This is the original height of the image taking rotation into > account. */ > protected float scaledHeight; > + > + /** > + * The compression level of the content streams. > + * @since 2.1.3 > + */ > + protected int compressionLevel = -1; > > /** an iText attributed unique id for this image. */ > protected Long mySerialId = getSerialId(); > @@ -1936,4 +1942,25 @@ > this.transparency = transparency; > } > > + > + /** > + * Returns the compression level used for images written as a > compressed stream. > + * @return the compression level (0 = best speed, 9 = best compression, > -1 is default) > + * @since 2.1.3 > + */ > + public int getCompressionLevel() { > + return compressionLevel; > + } > + > + /** > + * Sets the compression level to be used if the image is written as a > compressed stream. > + * @param compression_level a value between 0 (best speed) and 9 (best > compression) > + * @since 2.1.3 > + */ > + public void setCompressionLevel(int compressionLevel) { > + if (compressionLevel < -1 || compressionLevel > 9) > + this.compressionLevel = -1; > + else > + this.compressionLevel = compressionLevel; > + } > } > \ No newline at end of file > > Modified: trunk/src/core/com/lowagie/text/pdf/BaseFont.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/BaseFont.java 2008-07-06 06:54:15 UTC > (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/BaseFont.java 2008-07-06 15:34:38 UTC > (rev 3527) > @@ -259,6 +259,12 @@ > /** true if the font is to be embedded in the PDF */ > protected boolean embedded; > > + /** > + * The compression level for the font stream. > + * @since 2.1.3 > + */ > + protected int compressionLevel = -1; > + > /** > * true if the font must use it's built in encoding. In that case the > * <CODE>encoding</CODE> is only used to map a char to the position inside > @@ -321,16 +327,18 @@ > * a PdfStream. > * @param contents the content of the stream > * @param lengths an array of int that describes the several lengths > of each part of the font > + * @param compressionLevel the compression level of the Stream > * @throws DocumentException error in the stream compression > + * @since 2.1.3 (replaces the constructor without param > compressionLevel) > */ > - public StreamFont(byte contents[], int lengths[]) throws > DocumentException { > + public StreamFont(byte contents[], int lengths[], int > compressionLevel) throws DocumentException { > try { > bytes = contents; > put(PdfName.LENGTH, new PdfNumber(bytes.length)); > for (int k = 0; k < lengths.length; ++k) { > put(new PdfName("Length" + (k + 1)), new > PdfNumber(lengths[k])); > } > - flateCompress(); > + flateCompress(compressionLevel); > } > catch (Exception e) { > throw new DocumentException(e); > @@ -341,15 +349,17 @@ > * Generates the PDF stream for a font. > * @param contents the content of a stream > * @param subType the subtype of the font. > - * @throws DocumentException > + * @param compressionLevel the compression level of the > Stream > + * @throws DocumentException error in the stream compression > + * @since 2.1.3 (replaces the constructor without param > compressionLevel) > */ > - public StreamFont(byte contents[], String subType) throws > DocumentException { > + public StreamFont(byte contents[], String subType, int > compressionLevel) throws DocumentException { > try { > bytes = contents; > put(PdfName.LENGTH, new PdfNumber(bytes.length)); > if (subType != null) > put(PdfName.SUBTYPE, new PdfName(subType)); > - flateCompress(); > + flateCompress(compressionLevel); > } > catch (Exception e) { > throw new DocumentException(e); > @@ -1415,4 +1425,25 @@ > subsetRanges = new ArrayList(); > subsetRanges.add(range); > } > + > + /** > + * Returns the compression level used for the font streams. > + * @return the compression level (0 = best speed, 9 = best compression, > -1 is default) > + * @since 2.1.3 > + */ > + public int getCompressionLevel() { > + return compressionLevel; > + } > + > + /** > + * Sets the compression level to be used for the font streams. > + * @param compression_level a value between 0 (best speed) and 9 (best > compression) > + * @since 2.1.3 > + */ > + public void setCompressionLevel(int compressionLevel) { > + if (compressionLevel < -1 || compressionLevel > 9) > + this.compressionLevel = -1; > + else > + this.compressionLevel = compressionLevel; > + } > } > > Modified: trunk/src/core/com/lowagie/text/pdf/PRStream.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PRStream.java 2008-07-06 06:54:15 UTC > (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PRStream.java 2008-07-06 15:34:38 UTC > (rev 3527) > @@ -52,6 +52,7 @@ > import java.io.ByteArrayOutputStream; > import java.io.IOException; > import java.io.OutputStream; > +import java.util.zip.Deflater; > import java.util.zip.DeflaterOutputStream; > > import com.lowagie.text.Document; > @@ -91,14 +92,26 @@ > this.reader = reader; > this.offset = offset; > } > - > + > public PRStream(PdfReader reader, byte conts[]) { > + this(reader, conts, -1); > + } > + > + /** > + * 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(); > - DeflaterOutputStream zip = new DeflaterOutputStream(stream); > + DeflaterOutputStream zip = new DeflaterOutputStream(stream, > new Deflater(compressionLevel)); > zip.write(conts); > zip.close(); > bytes = stream.toByteArray(); > @@ -123,12 +136,26 @@ > * @since iText 2.1.1 > */ > public void setData(byte[] data, boolean compress) { > + setData(data, compress, -1); > + } > + > + /** > + * 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 compresssed. > + * @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(); > - DeflaterOutputStream zip = new DeflaterOutputStream(stream); > + DeflaterOutputStream zip = new DeflaterOutputStream(stream, > new Deflater(compressionLevel)); > zip.write(data); > zip.close(); > bytes = stream.toByteArray(); > @@ -147,7 +174,7 @@ > * @param data raw data, decrypted and uncompressed. > */ > public void setData(byte[] data) { > - setData(data, true); > + setData(data, true, -1); > } > > public void setLength(int length) { > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfAction.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfAction.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfAction.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -303,7 +303,7 @@ > try { > byte b[] = PdfEncodings.convertToBytes(code, unicode ? > PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING); > PdfStream stream = new PdfStream(b); > - stream.flateCompress(); > + stream.flateCompress(writer.getCompressionLevel()); > js.put(PdfName.JS, > writer.addToBody(stream).getIndirectReference()); > } > catch (Exception e) { > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfContents.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfContents.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfContents.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -51,6 +51,7 @@ > > import java.io.ByteArrayOutputStream; > import java.io.OutputStream; > +import java.util.zip.Deflater; > import java.util.zip.DeflaterOutputStream; > > import com.lowagie.text.DocWriter; > @@ -89,7 +90,7 @@ > if (Document.compress) > { > compressed = true; > - out = new DeflaterOutputStream(streamBytes); > + out = new DeflaterOutputStream(streamBytes, new > Deflater(text.getPdfWriter().getCompressionLevel())); > } > else > out = streamBytes; > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfCopy.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfCopy.java 2008-07-06 06:54:15 UTC > (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfCopy.java 2008-07-06 15:34:38 UTC > (rev 3527) > @@ -598,7 +598,7 @@ > if (over != null) > out.append(PdfContents.SAVESTATE); > PdfStream stream = new PdfStream(out.toByteArray()); > - stream.flateCompress(); > + stream.flateCompress(cstp.getCompressionLevel()); > PdfIndirectReference ref1 = > cstp.addToBody(stream).getIndirectReference(); > ar.addFirst(ref1); > out.reset(); > @@ -610,7 +610,7 @@ > out.append(over.getInternalBuffer()); > out.append(PdfContents.RESTORESTATE); > stream = new PdfStream(out.toByteArray()); > - stream.flateCompress(); > + stream.flateCompress(cstp.getCompressionLevel()); > ar.add(cstp.addToBody(stream).getIndirectReference()); > } > pageN.put(PdfName.RESOURCES, pageResources.getResources()); > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfFileSpecification.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfFileSpecification.java > 2008-07-06 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfFileSpecification.java > 2008-07-06 15:34:38 UTC (rev 3527) > @@ -130,6 +130,27 @@ > * @return the file specification > */ > public static PdfFileSpecification fileEmbedded(PdfWriter writer, String > filePath, String fileDisplay, byte fileStore[], boolean compress, String > mimeType, PdfDictionary fileParameter) throws IOException { > + return fileEmbedded(writer, filePath, fileDisplay, fileStore, compress, > null, null, writer.getCompressionLevel()); > + } > + > + /** > + * Creates a file specification with the file embedded. The file may > + * come from the file system or from a byte array. > + * @param writer the <CODE>PdfWriter</CODE> > + * @param filePath the file path > + * @param fileDisplay the file information that is presented to the user > + * @param fileStore the byte array with the file. If it is not > <CODE>null</CODE> > + * it takes precedence over <CODE>filePath</CODE> > + * @param compress sets the compression on the data. Multimedia content > will benefit little > + * from compression > + * @param mimeType the optional mimeType > + * @param fileParameter the optional extra file parameters such as the > creation or modification date > + * @param compressionLevel the level of compression > + * @throws IOException on error > + * @return the file specification > + * @since 2.1.3 > + */ > + public static PdfFileSpecification fileEmbedded(PdfWriter writer, String > filePath, String fileDisplay, byte fileStore[], boolean compress, String > mimeType, PdfDictionary fileParameter, int compressionLevel) throws > IOException { > PdfFileSpecification fs = new PdfFileSpecification(); > fs.writer = writer; > fs.put(PdfName.F, new PdfString(fileDisplay)); > @@ -161,7 +182,7 @@ > stream = new PdfStream(fileStore); > stream.put(PdfName.TYPE, PdfName.EMBEDDEDFILE); > if (compress) > - stream.flateCompress(); > + stream.flateCompress(compressionLevel); > stream.put(PdfName.PARAMS, refFileLength); > if (mimeType != null) > stream.put(PdfName.SUBTYPE, new PdfName(mimeType)); > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfFormXObject.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfFormXObject.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfFormXObject.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -66,18 +66,15 @@ > /** This is the 1 - matrix. */ > public static final PdfLiteral MATRIX = new PdfLiteral("[1 0 0 1 0 0]"); > > - // membervariables > - > - > - // constructor > - > /** > * Constructs a <CODE>PdfFormXObject</CODE>-object. > * > - * @param template the template > + * @param template the template > + * @param compressionLevel the compression level for the stream > + * @since 2.1.3 (Replacing the existing constructor with param > compressionLevel) > */ > > - PdfFormXObject(PdfTemplate template) // throws BadPdfFormatException > + PdfFormXObject(PdfTemplate template, int compressionLevel) // throws > BadPdfFormatException > { > super(); > put(PdfName.TYPE, PdfName.XOBJECT); > @@ -96,7 +93,7 @@ > put(PdfName.MATRIX, matrix); > bytes = template.toPdf(null); > put(PdfName.LENGTH, new PdfNumber(bytes.length)); > - flateCompress(); > + flateCompress(compressionLevel); > } > > } > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfFunction.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfFunction.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfFunction.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -82,7 +82,7 @@ > int bitsPerSample, int order, float encode[], float decode[], byte > stream[]) { > PdfFunction func = new PdfFunction(writer); > func.dictionary = new PdfStream(stream); > - ((PdfStream)func.dictionary).flateCompress(); > + > ((PdfStream)func.dictionary).flateCompress(writer.getCompressionLevel()); > func.dictionary.put(PdfName.FUNCTIONTYPE, new PdfNumber(0)); > func.dictionary.put(PdfName.DOMAIN, new PdfArray(domain)); > func.dictionary.put(PdfName.RANGE, new PdfArray(range)); > @@ -134,7 +134,7 @@ > b[k] = (byte)postscript.charAt(k); > PdfFunction func = new PdfFunction(writer); > func.dictionary = new PdfStream(b); > - ((PdfStream)func.dictionary).flateCompress(); > + > ((PdfStream)func.dictionary).flateCompress(writer.getCompressionLevel()); > func.dictionary.put(PdfName.FUNCTIONTYPE, new PdfNumber(4)); > func.dictionary.put(PdfName.DOMAIN, new PdfArray(domain)); > func.dictionary.put(PdfName.RANGE, new PdfArray(range)); > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfICCBased.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfICCBased.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfICCBased.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -57,8 +57,15 @@ > */ > > public class PdfICCBased extends PdfStream { > - > - public PdfICCBased(ICC_Profile profile) { > + > + /** > + * Creates an ICC stream. > + * > + * @param compressionLevel the compressionLevel > + * > + * @since 2.1.3 (replacing the constructor without param > compressionLevel) > + */ > + public PdfICCBased(ICC_Profile profile, int compressionLevel) { > super(); > try { > int numberOfComponents = profile.getNumComponents(); > @@ -77,7 +84,7 @@ > } > put(PdfName.N, new PdfNumber(numberOfComponents)); > bytes = profile.getData(); > - flateCompress(); > + flateCompress(compressionLevel); > } catch (Exception e) { > throw new ExceptionConverter(e); > } > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfImage.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfImage.java 2008-07-06 06:54:15 UTC > (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfImage.java 2008-07-06 15:34:38 UTC > (rev 3527) > @@ -165,7 +165,7 @@ > if (image.isDeflated()) > put(PdfName.FILTER, PdfName.FLATEDECODE); > else { > - flateCompress(); > + flateCompress(image.getCompressionLevel()); > } > } > return; > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfImportedPage.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfImportedPage.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfImportedPage.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -116,9 +116,16 @@ > throwError(); > return null; > } > - > - PdfStream getFormXObject() throws IOException { > - return readerInstance.getFormXObject(pageNumber); > + > + /** > + * Gets the stream representing this page. > + * > + * @param compressionLevel the compressionLevel > + * @return the stream representing this page > + * @since 2.1.3 (replacing the method without param > compressionLevel) > + */ > + PdfStream getFormXObject(int compressionLevel) throws IOException { > + return readerInstance.getFormXObject(pageNumber, compressionLevel); > } > > public void setColorFill(PdfSpotColor sp, float tint) { > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfPSXObject.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfPSXObject.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfPSXObject.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -69,15 +69,17 @@ > /** > * Gets the stream representing this object. > * > - * @return the stream representing this object > + * @param compressionLevel the compressionLevel > + * @return the stream representing this template > + * @since 2.1.3 (replacing the method without param > compressionLevel) > * @throws IOException > */ > > - PdfStream getFormXObject() throws IOException { > + PdfStream getFormXObject(int compressionLevel) throws IOException { > PdfStream s = new PdfStream(content.toByteArray()); > s.put(PdfName.TYPE, PdfName.XOBJECT); > s.put(PdfName.SUBTYPE, PdfName.PS); > - s.flateCompress(); > + s.flateCompress(compressionLevel); > return s; > } > > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfPattern.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfPattern.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfPattern.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -56,7 +56,21 @@ > > public class PdfPattern extends PdfStream { > > - PdfPattern(PdfPatternPainter painter) { > + /** > + * Creates a PdfPattern object. > + * @param painter a pattern painter instance > + */ > + PdfPattern(PdfPatternPainter painter) { > + this(painter, -1); > + } > + > + /** > + * Creates a PdfPattern object. > + * @param painter a pattern painter instance > + * @param compressionLevel the compressionLevel for the stream > + * @since 2.1.3 > + */ > + PdfPattern(PdfPatternPainter painter, int compressionLevel) { > super(); > PdfNumber one = new PdfNumber(1); > PdfArray matrix = painter.getMatrix(); > @@ -77,7 +91,7 @@ > bytes = painter.toPdf(null); > put(PdfName.LENGTH, new PdfNumber(bytes.length)); > try { > - flateCompress(); > + flateCompress(compressionLevel); > } catch (Exception e) { > throw new ExceptionConverter(e); > } > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfPatternPainter.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfPatternPainter.java > 2008-07-06 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfPatternPainter.java > 2008-07-06 15:34:38 UTC (rev 3527) > @@ -149,15 +149,23 @@ > } > /** > * Gets the stream representing this pattern > - * > * @return the stream representing this pattern > */ > - > PdfPattern getPattern() { > return new PdfPattern(this); > } > > /** > + * Gets the stream representing this pattern > + * @param compressionLevel the compression level of the > stream > + * @return the stream representing this pattern > + * @since 2.1.3 > + */ > + PdfPattern getPattern(int compressionLevel) { > + return new PdfPattern(this, compressionLevel); > + } > + > + /** > * Gets a duplicate of this <CODE>PdfPatternPainter</CODE>. All > * the members are copied by reference but the buffer stays different. > * @return a copy of this <CODE>PdfPatternPainter</CODE> > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfReader.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfReader.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfReader.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -1997,6 +1997,14 @@ > * @param pageNum the page number. 1 is the first > */ > public void setPageContent(int pageNum, byte content[]) { > + setPageContent(pageNum, content, -1); > + } > + /** Sets the contents of the page. > + * @param content the new page content > + * @param pageNum the page number. 1 is the first > + * @since 2.1.3 (the method already existed without param > compressionLevel) > + */ > + public void setPageContent(int pageNum, byte content[], int > compressionLevel) { > PdfDictionary page = getPageN(pageNum); > if (page == null) > return; > @@ -2008,7 +2016,7 @@ > freeXref = xrefObj.size() - 1; > } > page.put(PdfName.CONTENTS, new PRIndirectReference(this, freeXref)); > - xrefObj.set(freeXref, new PRStream(this, content)); > + xrefObj.set(freeXref, new PRStream(this, content, compressionLevel)); > } > > /** Get the content from a stream applying the required filters. > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfReaderInstance.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfReaderInstance.java > 2008-07-06 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfReaderInstance.java > 2008-07-06 15:34:38 UTC (rev 3527) > @@ -110,8 +110,14 @@ > return obj; > } > > - > - PdfStream getFormXObject(int pageNumber) throws IOException { > + /** > + * Gets the content stream of a page as a PdfStream object. > + * @param pageNumber the page of which you > want the stream > + * @param compressionLevel the compression level you want > to apply to the stream > + * @return a PdfStream object > + * @since 2.1.3 (the method already existed without param > compressionLevel) > + */ > + PdfStream getFormXObject(int pageNumber, int compressionLevel) throws > IOException { > PdfDictionary page = reader.getPageNRelease(pageNumber); > PdfObject contents = > PdfReader.getPdfObjectRelease(page.get(PdfName.CONTENTS)); > PdfDictionary dic = new PdfDictionary(); > @@ -140,7 +146,7 @@ > stream = new PRStream((PRStream)contents, dic); > } > else { > - stream = new PRStream(reader, bout); > + stream = new PRStream(reader, bout, compressionLevel); > stream.putAll(dic); > } > return stream; > @@ -166,7 +172,7 @@ > file.reOpen(); > for (Iterator it = importedPages.values().iterator(); > it.hasNext();) { > PdfImportedPage ip = (PdfImportedPage)it.next(); > - writer.addToBody(ip.getFormXObject(), > ip.getIndirectReference()); > + > writer.addToBody(ip.getFormXObject(writer.getCompressionLevel()), > ip.getIndirectReference()); > } > writeAllVisited(); > } > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfStamperImp.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfStamperImp.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfStamperImp.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -396,7 +396,7 @@ > if (ps.over != null) > out.append(PdfContents.SAVESTATE); > PdfStream stream = new PdfStream(out.toByteArray()); > - stream.flateCompress(); > + stream.flateCompress(compressionLevel); > ar.addFirst(addToBody(stream).getIndirectReference()); > out.reset(); > if (ps.over != null) { > @@ -409,7 +409,7 @@ > out.append(buf.getBuffer(), ps.replacePoint, buf.size() - > ps.replacePoint); > out.append(PdfContents.RESTORESTATE); > stream = new PdfStream(out.toByteArray()); > - stream.flateCompress(); > + stream.flateCompress(compressionLevel); > ar.add(addToBody(stream).getIndirectReference()); > } > alterResources(ps); > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfStream.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfStream.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfStream.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -176,8 +176,16 @@ > /** > * Compresses the stream. > */ > + public void flateCompress() { > + flateCompress(-1); > + } > > - public void flateCompress() { > + /** > + * 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 > @@ -206,7 +214,7 @@ > try { > // compress > ByteArrayOutputStream stream = new ByteArrayOutputStream(); > - DeflaterOutputStream zip = new DeflaterOutputStream(stream); > + DeflaterOutputStream zip = new DeflaterOutputStream(stream, new > Deflater(compressionLevel)); > if (streamBytes != null) > streamBytes.writeTo(zip); > else > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfTemplate.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfTemplate.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfTemplate.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -235,11 +235,12 @@ > /** > * Gets the stream representing this template. > * > + * @param compressionLevel the compressionLevel > * @return the stream representing this template > + * @since 2.1.3 (replacing the method without param > compressionLevel) > */ > - > - PdfStream getFormXObject() throws IOException { > - return new PdfFormXObject(this); > + PdfStream getFormXObject(int compressionLevel) throws IOException { > + return new PdfFormXObject(this, compressionLevel); > } > > /** > > Modified: trunk/src/core/com/lowagie/text/pdf/PdfWriter.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/PdfWriter.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/PdfWriter.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -296,7 +296,7 @@ > int first = index.size(); > index.append(streamObjects); > PdfStream stream = new PdfStream(index.toByteArray()); > - stream.flateCompress(); > + stream.flateCompress(writer.getCompressionLevel()); > stream.put(PdfName.TYPE, PdfName.OBJSTM); > stream.put(PdfName.N, new PdfNumber(numObj)); > stream.put(PdfName.FIRST, new PdfNumber(first)); > @@ -465,7 +465,7 @@ > } > PdfStream xr = new PdfStream(buf.toByteArray()); > buf = null; > - xr.flateCompress(); > + xr.flateCompress(writer.getCompressionLevel()); > xr.put(PdfName.SIZE, new PdfNumber(size())); > xr.put(PdfName.ROOT, root); > if (info != null) { > @@ -1208,7 +1208,7 @@ > if (template != null && template.getIndirectReference() > instanceof PRIndirectReference) > continue; > if (template != null && template.getType() == > PdfTemplate.TYPE_TEMPLATE) { > - addToBody(template.getFormXObject(), > template.getIndirectReference()); > + addToBody(template.getFormXObject(compressionLevel), > template.getIndirectReference()); > } > } > // [F5] add all the dependencies in the imported pages > @@ -1225,7 +1225,7 @@ > // [F7] add the pattern > for (Iterator it = documentPatterns.keySet().iterator(); > it.hasNext();) { > PdfPatternPainter pat = (PdfPatternPainter)it.next(); > - addToBody(pat.getPattern(), pat.getIndirectReference()); > + addToBody(pat.getPattern(compressionLevel), > pat.getIndirectReference()); > } > // [F8] add the shading patterns > for (Iterator it = documentShadingPatterns.keySet().iterator(); > it.hasNext();) { > @@ -1719,7 +1719,7 @@ > out.put(PdfName.INFO, new PdfString(info, > PdfObject.TEXT_UNICODE)); > if (destOutputProfile != null) { > PdfStream stream = new PdfStream(destOutputProfile); > - stream.flateCompress(); > + stream.flateCompress(compressionLevel); > out.put(PdfName.DESTOUTPUTPROFILE, > addToBody(stream).getIndirectReference()); > } > out.put(PdfName.S, PdfName.GTS_PDFX); > @@ -1966,6 +1966,33 @@ > setAtLeastPdfVersion(VERSION_1_5); > } > > + /** > + * The compression level of the content streams. > + * @since 2.1.3 > + */ > + protected int compressionLevel = -1; > + > + /** > + * Returns the compression level used for streams written by this > writer. > + * @return the compression level (0 = best speed, 9 = best compression, > -1 is default) > + * @since 2.1.3 > + */ > + public int getCompressionLevel() { > + return compressionLevel; > + } > + > + /** > + * Sets the compression level to be used for streams written by this > writer. > + * @param compression_level a value between 0 (best speed) and 9 (best > compression) > + * @since 2.1.3 > + */ > + public void setCompressionLevel(int compressionLevel) { > + if (compressionLevel < -1 || compressionLevel > 9) > + this.compressionLevel = -1; > + else > + this.compressionLevel = compressionLevel; > + } > + > // [F3] adding fonts > > /** The fonts of this document */ > @@ -2068,7 +2095,7 @@ > if (template.getIndirectReference() instanceof PRIndirectReference) > return; > if (template.getType() == PdfTemplate.TYPE_TEMPLATE) { > - addToBody(template.getFormXObject(), > template.getIndirectReference()); > + addToBody(template.getFormXObject(compressionLevel), > template.getIndirectReference()); > objs[1] = null; > } > } > @@ -2839,7 +2866,7 @@ > } > PdfImage i = new PdfImage(image, "img" + images.size(), > maskRef); > if (image.hasICCProfile()) { > - PdfICCBased icc = new PdfICCBased(image.getICCProfile()); > + PdfICCBased icc = new PdfICCBased(image.getICCProfile(), > image.getCompressionLevel()); > PdfIndirectReference iccRef = add(icc); > PdfArray iccArray = new PdfArray(); > iccArray.add(PdfName.ICCBASED); > > Modified: trunk/src/core/com/lowagie/text/pdf/TrueTypeFont.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/TrueTypeFont.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/TrueTypeFont.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -1253,7 +1253,7 @@ > String subsetPrefix = ""; > if (embedded) { > if (cff) { > - pobj = new StreamFont(readCffFont(), "Type1C"); > + pobj = new StreamFont(readCffFont(), "Type1C", > compressionLevel); > obj = writer.addToBody(pobj); > ind_font = obj.getIndirectReference(); > } > @@ -1289,7 +1289,7 @@ > b = getFullFont(); > } > int lengths[] = new int[]{b.length}; > - pobj = new StreamFont(b, lengths); > + pobj = new StreamFont(b, lengths, compressionLevel); > obj = writer.addToBody(pobj); > ind_font = obj.getIndirectReference(); > } > @@ -1336,12 +1336,12 @@ > */ > public PdfStream getFullFontStream() throws IOException, > DocumentException { > if (cff) { > - return new StreamFont(readCffFont(), "Type1C"); > + return new StreamFont(readCffFont(), "Type1C", compressionLevel); > } > else { > byte[] b = getFullFont(); > int lengths[] = new int[]{b.length}; > - return new StreamFont(b, lengths); > + return new StreamFont(b, lengths, compressionLevel); > } > } > > > Modified: trunk/src/core/com/lowagie/text/pdf/TrueTypeFontUnicode.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/TrueTypeFontUnicode.java > 2008-07-06 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/TrueTypeFontUnicode.java > 2008-07-06 15:34:38 UTC (rev 3527) > @@ -209,7 +209,7 @@ > "end end\n"); > String s = buf.toString(); > PdfStream stream = new PdfStream(PdfEncodings.convertToBytes(s, > null)); > - stream.flateCompress(); > + stream.flateCompress(compressionLevel); > return stream; > } > > @@ -356,7 +356,7 @@ > bt[v / 8] |= rotbits[v % 8]; > } > stream = new PdfStream(bt); > - stream.flateCompress(); > + stream.flateCompress(compressionLevel); > } > cidset = writer.addToBody(stream).getIndirectReference(); > } > @@ -367,7 +367,7 @@ > CFFFontSubset cff = new CFFFontSubset(new > RandomAccessFileOrArray(b),longTag); > b = cff.Process(cff.getNames()[0]); > } > - pobj = new StreamFont(b, "CIDFontType0C"); > + pobj = new StreamFont(b, "CIDFontType0C", > compressionLevel); > obj = writer.addToBody(pobj); > ind_font = obj.getIndirectReference(); > } else { > @@ -380,7 +380,7 @@ > b = getFullFont(); > } > int lengths[] = new int[]{b.length}; > - pobj = new StreamFont(b, lengths); > + pobj = new StreamFont(b, lengths, compressionLevel); > obj = writer.addToBody(pobj); > ind_font = obj.getIndirectReference(); > } > @@ -414,7 +414,7 @@ > */ > public PdfStream getFullFontStream() throws IOException, > DocumentException { > if (cff) { > - return new StreamFont(readCffFont(), "CIDFontType0C"); > + return new StreamFont(readCffFont(), "CIDFontType0C", > compressionLevel); > } > return super.getFullFontStream(); > } > > Modified: trunk/src/core/com/lowagie/text/pdf/Type1Font.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/Type1Font.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/Type1Font.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -529,7 +529,7 @@ > size -= got; > } > } > - return new StreamFont(st, lengths); > + return new StreamFont(st, lengths, compressionLevel); > } > catch (Exception e) { > throw new DocumentException(e); > > Modified: trunk/src/core/com/lowagie/text/pdf/Type3Font.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/Type3Font.java 2008-07-06 > 06:54:15 UTC (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/Type3Font.java 2008-07-06 > 15:34:38 UTC (rev 3527) > @@ -239,7 +239,7 @@ > diffs.add(n); > Type3Glyph glyph = (Type3Glyph)char2glyph.get(new Integer(c2)); > PdfStream stream = new PdfStream(glyph.toPdf(null)); > - stream.flateCompress(); > + stream.flateCompress(compressionLevel); > PdfIndirectReference refp = > writer.addToBody(stream).getIndirectReference(); > charprocs.put(n, refp); > } > > Modified: trunk/src/core/com/lowagie/text/pdf/XfaForm.java > =================================================================== > --- trunk/src/core/com/lowagie/text/pdf/XfaForm.java 2008-07-06 06:54:15 UTC > (rev 3526) > +++ trunk/src/core/com/lowagie/text/pdf/XfaForm.java 2008-07-06 15:34:38 UTC > (rev 3527) > @@ -192,10 +192,10 @@ > reader.killXref((PdfIndirectReference)ar.get(t)); > reader.killXref((PdfIndirectReference)ar.get(d)); > PdfStream tStream = new > PdfStream(serializeDoc(form.templateNode)); > - tStream.flateCompress(); > + tStream.flateCompress(writer.getCompressionLevel()); > ar.set(t, writer.addToBody(tStream).getIndirectReference()); > PdfStream dStream = new > PdfStream(serializeDoc(form.datasetsNode)); > - dStream.flateCompress(); > + dStream.flateCompress(writer.getCompressionLevel()); > ar.set(d, writer.addToBody(dStream).getIndirectReference()); > af.put(PdfName.XFA, new PdfArray(ar)); > return; > @@ -203,7 +203,7 @@ > } > reader.killXref(af.get(PdfName.XFA)); > PdfStream str = new PdfStream(serializeDoc(form.domDocument)); > - str.flateCompress(); > + str.flateCompress(writer.getCompressionLevel()); > PdfIndirectReference ref = > writer.addToBody(str).getIndirectReference(); > af.put(PdfName.XFA, ref); > } > > > This was sent by the SourceForge.net collaborative development platform, the > world's largest Open Source development site. > > ------------------------------------------------------------------------- > Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! > Studies have shown that voting for your favorite open source project, > along with a healthy diet, reduces your potential for chronic lameness > and boredom. Vote Now at http://www.sourceforge.net/community/cca08 > _______________________________________________ > Itext-svn mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/itext-svn > ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08 _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions Do you like iText? Buy the iText book: http://www.1t3xt.com/docs/book.php Or leave a tip: https://tipit.to/itexttipjar